charon-cmd: Handle simple command line arguments like --help before the others

This commit is contained in:
Tobias Brunner
2013-06-21 15:51:42 +02:00
parent 0d25c4ef87
commit 4049ec42bf
+14 -3
View File
@@ -249,9 +249,10 @@ static void usage(FILE *out, char *msg, char *binary)
} }
/** /**
* Handle command line options * Handle command line options, if simple is TRUE only arguments like --help
* and --version are handled.
*/ */
static void handle_arguments(int argc, char *argv[]) static void handle_arguments(int argc, char *argv[], bool simple)
{ {
struct option long_opts[CMD_OPT_COUNT + 1] = {}; struct option long_opts[CMD_OPT_COUNT + 1] = {};
int i, opt; int i, opt;
@@ -262,6 +263,8 @@ static void handle_arguments(int argc, char *argv[])
long_opts[i].val = cmd_options[i].id; long_opts[i].val = cmd_options[i].id;
long_opts[i].has_arg = cmd_options[i].has_arg; long_opts[i].has_arg = cmd_options[i].has_arg;
} }
/* reset option parser */
optind = 1;
while (TRUE) while (TRUE)
{ {
bool handled = FALSE; bool handled = FALSE;
@@ -278,6 +281,10 @@ static void handle_arguments(int argc, char *argv[])
printf("%s, strongSwan %s\n", "charon-cmd", VERSION); printf("%s, strongSwan %s\n", "charon-cmd", VERSION);
exit(0); exit(0);
default: default:
if (simple)
{
continue;
}
handled |= conn->handle(conn, opt, optarg); handled |= conn->handle(conn, opt, optarg);
handled |= creds->handle(creds, opt, optarg); handled |= creds->handle(creds, opt, optarg);
if (handled) if (handled)
@@ -303,6 +310,9 @@ int main(int argc, char *argv[])
struct utsname utsname; struct utsname utsname;
int group; int group;
/* handle simple arguments */
handle_arguments(argc, argv, TRUE);
dbg = dbg_stderr; dbg = dbg_stderr;
atexit(library_deinit); atexit(library_deinit);
if (!library_init(NULL)) if (!library_init(NULL))
@@ -353,7 +363,8 @@ int main(int argc, char *argv[])
creds = cmd_creds_create(); creds = cmd_creds_create();
atexit(cleanup_creds); atexit(cleanup_creds);
handle_arguments(argc, argv); /* handle all arguments */
handle_arguments(argc, argv, FALSE);
if (uname(&utsname) != 0) if (uname(&utsname) != 0)
{ {