argument parsing: handle --help option in order
authorSven Verdoolaege <skimo@kotnet.org>
Wed, 20 Feb 2013 15:44:13 +0000 (16:44 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Wed, 20 Feb 2013 15:51:53 +0000 (16:51 +0100)
In the original code, we would first scan for any --help option
before processing any other options.  Now we first consider the options
before --help.  If any of these options change the defaults for any other
options, then those changed defaults will be reflected in the --help output.

Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
isl_arg.c

index f2025ee..78f12b4 100644 (file)
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -1144,21 +1144,17 @@ static int next_arg(struct isl_arg *arg, int a)
        return -1;
 }
 
-/* Unless ISL_ARG_SKIP_HELP is set, check if any of the arguments is
+/* Unless ISL_ARG_SKIP_HELP is set, check if "arg" is
  * equal to "--help" and if so call print_help_and_exit.
  */
-static void check_help(struct isl_args *args, int argc, char **argv, void *opt,
+static void check_help(struct isl_args *args, char *arg, char *prog, void *opt,
        unsigned flags)
 {
-       int i;
-
        if (ISL_FL_ISSET(flags, ISL_ARG_SKIP_HELP))
                return;
 
-       for (i = 1; i < argc; ++i) {
-               if (strcmp(argv[i], "--help") == 0)
-                       print_help_and_exit(args->args, argv[0], opt);
-       }
+       if (strcmp(arg, "--help") == 0)
+               print_help_and_exit(args->args, prog, opt);
 }
 
 int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
@@ -1171,8 +1167,6 @@ int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
 
        n = n_arg(args->args);
 
-       check_help(args, argc, argv, opt, flags);
-
        for (i = 1; i < argc; ++i) {
                if ((strcmp(argv[i], "--version") == 0 ||
                     strcmp(argv[i], "-V") == 0) && any_version(args->args))
@@ -1198,6 +1192,7 @@ int isl_args_parse(struct isl_args *args, int argc, char **argv, void *opt,
                                ++skip;
                        continue;
                }
+               check_help(args, argv[1 + skip], argv[0], opt, flags);
                parsed = parse_option(args->args, &argv[1 + skip], NULL, opt);
                if (parsed)
                        argc = drop_argument(argc, argv, 1 + skip, parsed);