GIT-VERSION-GEN: do not fail if a 'HEAD' file exists in the working copy
[gitweb.git] / parse-options.c
index be35785ce88e03d23ca5b0e5ffdc6142fb931060..b8bde2b04a14936787392babf5e0ec0a9e3ef1a7 100644 (file)
@@ -6,7 +6,8 @@
 
 struct optparse_t {
        const char **argv;
-       int argc;
+       const char **out;
+       int argc, cpidx;
        const char *opt;
 };
 
@@ -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)) {
@@ -242,8 +253,7 @@ static NORETURN void usage_with_options_internal(const char * const *,
 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];
@@ -251,7 +261,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
                if (*arg != '-' || !arg[1]) {
                        if (flags & PARSE_OPT_STOP_AT_NON_OPTION)
                                break;
-                       argv[j++] = args.argv[0];
+                       args.out[args.cpidx++] = args.argv[0];
                        continue;
                }
 
@@ -288,9 +298,9 @@ int parse_options(int argc, const char **argv, const struct option *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
@@ -302,8 +312,12 @@ void usage_with_options_internal(const char * const *usagestr,
        fprintf(stderr, "usage: %s\n", *usagestr++);
        while (*usagestr && **usagestr)
                fprintf(stderr, "   or: %s\n", *usagestr++);
-       while (*usagestr)
-               fprintf(stderr, "    %s\n", *usagestr++);
+       while (*usagestr) {
+               fprintf(stderr, "%s%s\n",
+                               **usagestr ? "    " : "",
+                               *usagestr);
+               usagestr++;
+       }
 
        if (opts->type != OPTION_GROUP)
                fputc('\n', stderr);
@@ -330,9 +344,14 @@ void usage_with_options_internal(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>]");
+                               if (opts->long_name)
+                                       pos += fprintf(stderr, "[=<n>]");
+                               else
+                                       pos += fprintf(stderr, "[<n>]");
                        else
                                pos += fprintf(stderr, " <n>");
                        break;
@@ -343,12 +362,18 @@ void usage_with_options_internal(const char * const *usagestr,
                case OPTION_STRING:
                        if (opts->argh) {
                                if (opts->flags & PARSE_OPT_OPTARG)
-                                       pos += fprintf(stderr, " [<%s>]", opts->argh);
+                                       if (opts->long_name)
+                                               pos += fprintf(stderr, "[=<%s>]", opts->argh);
+                                       else
+                                               pos += fprintf(stderr, "[<%s>]", opts->argh);
                                else
                                        pos += fprintf(stderr, " <%s>", opts->argh);
                        } else {
                                if (opts->flags & PARSE_OPT_OPTARG)
-                                       pos += fprintf(stderr, " [...]");
+                                       if (opts->long_name)
+                                               pos += fprintf(stderr, "[=...]");
+                                       else
+                                               pos += fprintf(stderr, "[...]");
                                else
                                        pos += fprintf(stderr, " ...");
                        }
@@ -397,3 +422,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;
+}