t0050: Fix merge test on case sensitive file systems
[gitweb.git] / parse-options.c
index d3e608ac45e2a0c6accb9d27ed8d407619cb70d2..acf3fe3a1a82cd99e7bd6e0ff9d6d3d7e344a155 100644 (file)
@@ -6,7 +6,8 @@
 
 struct optparse_t {
        const char **argv;
-       int argc;
+       const char **out;
+       int argc, cpidx;
        const char *opt;
 };
 
@@ -89,7 +90,7 @@ static int get_value(struct optparse_t *p,
                        *(const char **)opt->value = NULL;
                        return 0;
                }
-               if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-')) {
+               if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
                        *(const char **)opt->value = (const char *)opt->defval;
                        return 0;
                }
@@ -103,7 +104,7 @@ static int get_value(struct optparse_t *p,
                        return (*opt->callback)(opt, NULL, 1);
                if (opt->flags & PARSE_OPT_NOARG)
                        return (*opt->callback)(opt, NULL, 0);
-               if (opt->flags & PARSE_OPT_OPTARG && (!arg || *arg == '-'))
+               if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
                        return (*opt->callback)(opt, NULL, 0);
                if (!arg)
                        return opterror(opt, "requires a value", flags);
@@ -114,7 +115,7 @@ static int get_value(struct optparse_t *p,
                        *(int *)opt->value = 0;
                        return 0;
                }
-               if (opt->flags & PARSE_OPT_OPTARG && (!arg || !isdigit(*arg))) {
+               if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
                        *(int *)opt->value = opt->defval;
                        return 0;
                }
@@ -159,6 +160,16 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
                        continue;
 
                rest = skip_prefix(arg, options->long_name);
+               if (options->type == OPTION_ARGUMENT) {
+                       if (!rest)
+                               continue;
+                       if (*rest == '=')
+                               return opterror(options, "takes no value", flags);
+                       if (*rest)
+                               continue;
+                       p->out[p->cpidx++] = arg - 2;
+                       return 0;
+               }
                if (!rest) {
                        /* abbreviated? */
                        if (!strncmp(options->long_name, arg, arg_end - arg)) {
@@ -216,28 +227,58 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
        return error("unknown option `%s'", arg);
 }
 
+void check_typos(const char *arg, const struct option *options)
+{
+       if (strlen(arg) < 3)
+               return;
+
+       if (!prefixcmp(arg, "no-")) {
+               error ("did you mean `--%s` (with two dashes ?)", arg);
+               exit(129);
+       }
+
+       for (; options->type != OPTION_END; options++) {
+               if (!options->long_name)
+                       continue;
+               if (!prefixcmp(options->long_name, arg)) {
+                       error ("did you mean `--%s` (with two dashes ?)", arg);
+                       exit(129);
+               }
+       }
+}
+
+static NORETURN void usage_with_options_internal(const char * const *,
+                                                 const struct option *, int);
+
 int parse_options(int argc, const char **argv, const struct option *options,
                   const char * const usagestr[], int flags)
 {
-       struct optparse_t args = { argv + 1, argc - 1, NULL };
-       int j = 0;
+       struct optparse_t args = { argv + 1, argv, argc - 1, 0, NULL };
 
        for (; args.argc; args.argc--, args.argv++) {
                const char *arg = args.argv[0];
 
                if (*arg != '-' || !arg[1]) {
-                       argv[j++] = args.argv[0];
+                       if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
+                               break;
+                       args.out[args.cpidx++] = args.argv[0];
                        continue;
                }
 
                if (arg[1] != '-') {
                        args.opt = arg + 1;
-                       do {
+                       if (*args.opt == 'h')
+                               usage_with_options(usagestr, options);
+                       if (parse_short_opt(&args, options) < 0)
+                               usage_with_options(usagestr, options);
+                       if (args.opt)
+                               check_typos(arg + 1, options);
+                       while (args.opt) {
                                if (*args.opt == 'h')
                                        usage_with_options(usagestr, options);
                                if (parse_short_opt(&args, options) < 0)
                                        usage_with_options(usagestr, options);
-                       } while (args.opt);
+                       }
                        continue;
                }
 
@@ -249,22 +290,24 @@ int parse_options(int argc, const char **argv, const struct option *options,
                        break;
                }
 
+               if (!strcmp(arg + 2, "help-all"))
+                       usage_with_options_internal(usagestr, options, 1);
                if (!strcmp(arg + 2, "help"))
                        usage_with_options(usagestr, options);
                if (parse_long_opt(&args, arg + 2, options))
                        usage_with_options(usagestr, options);
        }
 
-       memmove(argv + j, args.argv, args.argc * sizeof(*argv));
-       argv[j + args.argc] = NULL;
-       return j + args.argc;
+       memmove(args.out + args.cpidx, args.argv, args.argc * sizeof(*args.out));
+       args.out[args.cpidx + args.argc] = NULL;
+       return args.cpidx + args.argc;
 }
 
 #define USAGE_OPTS_WIDTH 24
 #define USAGE_GAP         2
 
-void usage_with_options(const char * const *usagestr,
-                        const struct option *opts)
+void usage_with_options_internal(const char * const *usagestr,
+                                 const struct option *opts, int full)
 {
        fprintf(stderr, "usage: %s\n", *usagestr++);
        while (*usagestr && **usagestr)
@@ -285,6 +328,8 @@ void usage_with_options(const char * const *usagestr,
                                fprintf(stderr, "%s\n", opts->help);
                        continue;
                }
+               if (!full && (opts->flags & PARSE_OPT_HIDDEN))
+                       continue;
 
                pos = fprintf(stderr, "    ");
                if (opts->short_name)
@@ -295,9 +340,11 @@ void usage_with_options(const char * const *usagestr,
                        pos += fprintf(stderr, "--%s", opts->long_name);
 
                switch (opts->type) {
+               case OPTION_ARGUMENT:
+                       break;
                case OPTION_INTEGER:
                        if (opts->flags & PARSE_OPT_OPTARG)
-                               pos += fprintf(stderr, " [<n>]");
+                               pos += fprintf(stderr, "[<n>]");
                        else
                                pos += fprintf(stderr, " <n>");
                        break;
@@ -335,6 +382,12 @@ void usage_with_options(const char * const *usagestr,
        exit(129);
 }
 
+void usage_with_options(const char * const *usagestr,
+                        const struct option *opts)
+{
+       usage_with_options_internal(usagestr, opts, 0);
+}
+
 /*----- some often used options -----*/
 #include "cache.h"
 
@@ -356,3 +409,10 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset)
        *(int *)(opt->value) = v;
        return 0;
 }
+
+int parse_opt_approxidate_cb(const struct option *opt, const char *arg,
+                            int unset)
+{
+       *(unsigned long *)(opt->value) = approxidate(arg);
+       return 0;
+}