]> git.ipfire.org Git - thirdparty/git.git/blobdiff - parse-options.c
parse-opt: have parse_options_{start,end}.
[thirdparty/git.git] / parse-options.c
index 7a08a0c64f9f447e09ef6771d83dbf95f8f3545d..9964877edfc1d7b4e5344e04c152f1274699fe64 100644 (file)
@@ -4,13 +4,7 @@
 #define OPT_SHORT 1
 #define OPT_UNSET 2
 
-struct optparse_t {
-       const char **argv;
-       int argc;
-       const char *opt;
-};
-
-static inline const char *get_arg(struct optparse_t *p)
+static inline const char *get_arg(struct parse_opt_ctx_t *p)
 {
        if (p->opt) {
                const char *res = p->opt;
@@ -36,7 +30,7 @@ static int opterror(const struct option *opt, const char *reason, int flags)
        return error("option `%s' %s", opt->long_name, reason);
 }
 
-static int get_value(struct optparse_t *p,
+static int get_value(struct parse_opt_ctx_t *p,
                      const struct option *opt, int flags)
 {
        const char *s, *arg;
@@ -130,7 +124,7 @@ static int get_value(struct optparse_t *p,
        }
 }
 
-static int parse_short_opt(struct optparse_t *p, const struct option *options)
+static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *options)
 {
        for (; options->type != OPTION_END; options++) {
                if (options->short_name == *p->opt) {
@@ -141,7 +135,7 @@ static int parse_short_opt(struct optparse_t *p, const struct option *options)
        return error("unknown switch `%c'", *p->opt);
 }
 
-static int parse_long_opt(struct optparse_t *p, const char *arg,
+static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
                           const struct option *options)
 {
        const char *arg_end = strchr(arg, '=');
@@ -159,6 +153,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,38 +220,83 @@ is_abbreviated:
        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);
+               }
+       }
+}
+
+void parse_options_start(struct parse_opt_ctx_t *ctx,
+                        int argc, const char **argv, int flags)
+{
+       memset(ctx, 0, sizeof(*ctx));
+       ctx->argc = argc - 1;
+       ctx->argv = argv + 1;
+       ctx->out  = argv;
+       ctx->flags = flags;
+}
+
+int parse_options_end(struct parse_opt_ctx_t *ctx)
+{
+       memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
+       ctx->out[ctx->cpidx + ctx->argc] = NULL;
+       return ctx->cpidx + ctx->argc;
+}
+
 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 parse_opt_ctx_t ctx;
 
-       for (; args.argc; args.argc--, args.argv++) {
-               const char *arg = args.argv[0];
+       parse_options_start(&ctx, argc, argv, flags);
+       for (; ctx.argc; ctx.argc--, ctx.argv++) {
+               const char *arg = ctx.argv[0];
 
                if (*arg != '-' || !arg[1]) {
-                       argv[j++] = args.argv[0];
+                       if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
+                               break;
+                       ctx.out[ctx.cpidx++] = ctx.argv[0];
                        continue;
                }
 
                if (arg[1] != '-') {
-                       args.opt = arg + 1;
-                       do {
-                               if (*args.opt == 'h')
+                       ctx.opt = arg + 1;
+                       if (*ctx.opt == 'h')
+                               usage_with_options(usagestr, options);
+                       if (parse_short_opt(&ctx, options) < 0)
+                               usage_with_options(usagestr, options);
+                       if (ctx.opt)
+                               check_typos(arg + 1, options);
+                       while (ctx.opt) {
+                               if (*ctx.opt == 'h')
                                        usage_with_options(usagestr, options);
-                               if (parse_short_opt(&args, options) < 0)
+                               if (parse_short_opt(&ctx, options) < 0)
                                        usage_with_options(usagestr, options);
-                       } while (args.opt);
+                       }
                        continue;
                }
 
                if (!arg[2]) { /* "--" */
-                       if (!(flags & PARSE_OPT_KEEP_DASHDASH)) {
-                               args.argc--;
-                               args.argv++;
+                       if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
+                               ctx.argc--;
+                               ctx.argv++;
                        }
                        break;
                }
@@ -256,13 +305,11 @@ int parse_options(int argc, const char **argv, const struct option *options,
                        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))
+               if (parse_long_opt(&ctx, 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;
+       return parse_options_end(&ctx);
 }
 
 #define USAGE_OPTS_WIDTH 24
@@ -274,8 +321,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);
@@ -302,9 +353,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;
@@ -315,12 +371,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, " ...");
                        }
@@ -369,3 +431,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;
+}