Merge branch 'sb/parse-options-integer'
authorJunio C Hamano <gitster@pobox.com>
Thu, 18 Jun 2009 17:36:22 +0000 (10:36 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Jun 2009 17:36:22 +0000 (10:36 -0700)
* sb/parse-options-integer:
parse-options: simplify usage argh handling
parse-options: make OPT_INTEGER's argh explicit

1  2 
parse-options.c
parse-options.h
diff --combined parse-options.c
index 79de18b37eb1aa96d2f25963b7f7cc2a08977846,e469fc06faef288e9684ec2a975a944eb7268e0c..34282adde5a2fd8ebb1413aee6742633b9656db7
@@@ -306,28 -306,6 +306,28 @@@ static void check_typos(const char *arg
        }
  }
  
 +static void parse_options_check(const struct option *opts)
 +{
 +      int err = 0;
 +
 +      for (; opts->type != OPTION_END; opts++) {
 +              if ((opts->flags & PARSE_OPT_LASTARG_DEFAULT) &&
 +                  (opts->flags & PARSE_OPT_OPTARG)) {
 +                      if (opts->long_name) {
 +                              error("`--%s` uses incompatible flags "
 +                                    "LASTARG_DEFAULT and OPTARG", opts->long_name);
 +                      } else {
 +                              error("`-%c` uses incompatible flags "
 +                                    "LASTARG_DEFAULT and OPTARG", opts->short_name);
 +                      }
 +                      err |= 1;
 +              }
 +      }
 +
 +      if (err)
 +              exit(129);
 +}
 +
  void parse_options_start(struct parse_opt_ctx_t *ctx,
                         int argc, const char **argv, const char *prefix,
                         int flags)
@@@ -353,8 -331,6 +353,8 @@@ int parse_options_step(struct parse_opt
  {
        int internal_help = !(ctx->flags & PARSE_OPT_NO_INTERNAL_HELP);
  
 +      parse_options_check(options);
 +
        /* we must reset ->opt, unknown short option leave it dangling */
        ctx->opt = NULL;
  
@@@ -464,7 -440,7 +464,7 @@@ int parse_options(int argc, const char 
  static int usage_argh(const struct option *opts)
  {
        const char *s;
-       int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
+       int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
        if (opts->flags & PARSE_OPT_OPTARG)
                if (opts->long_name)
                        s = literal ? "[=%s]" : "[=<%s>]";
                        s = literal ? "[%s]" : "[<%s>]";
        else
                s = literal ? " %s" : " <%s>";
-       return fprintf(stderr, s, opts->argh);
+       return fprintf(stderr, s, opts->argh ? opts->argh : "...");
  }
  
  #define USAGE_OPTS_WIDTH 24
@@@ -524,40 -500,8 +524,8 @@@ int usage_with_options_internal(const c
                if (opts->type == OPTION_NUMBER)
                        pos += fprintf(stderr, "-NUM");
  
-               switch (opts->type) {
-               case OPTION_ARGUMENT:
-                       break;
-               case OPTION_INTEGER:
-                       if (opts->flags & PARSE_OPT_OPTARG)
-                               if (opts->long_name)
-                                       pos += fprintf(stderr, "[=<n>]");
-                               else
-                                       pos += fprintf(stderr, "[<n>]");
-                       else
-                               pos += fprintf(stderr, " <n>");
-                       break;
-               case OPTION_CALLBACK:
-                       if (opts->flags & PARSE_OPT_NOARG)
-                               break;
-                       /* FALLTHROUGH */
-               case OPTION_FILENAME:
-                       /* FALLTHROUGH */
-               case OPTION_STRING:
-                       if (opts->argh)
-                               pos += usage_argh(opts);
-                       else {
-                               if (opts->flags & PARSE_OPT_OPTARG)
-                                       if (opts->long_name)
-                                               pos += fprintf(stderr, "[=...]");
-                                       else
-                                               pos += fprintf(stderr, "[...]");
-                               else
-                                       pos += fprintf(stderr, " ...");
-                       }
-                       break;
-               default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
-                       break;
-               }
+               if (!(opts->flags & PARSE_OPT_NOARG))
+                       pos += usage_argh(opts);
  
                if (pos <= USAGE_OPTS_WIDTH)
                        pad = USAGE_OPTS_WIDTH - pos;
diff --combined parse-options.h
index 5653dbab87a7d27b10e1513c14ec6d0ec3464325,f65aca3a9452f1f7fc635fb901422fa0bf27f9b1..aba30671dccd9e90686363a766df3eb25d4a8e20
@@@ -67,15 -67,12 +67,15 @@@ typedef int parse_opt_cb(const struct o
   * `flags`::
   *   mask of parse_opt_option_flags.
   *   PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
-  *   PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
+  *   PARSE_OPT_NOARG: says that this option takes no argument
   *   PARSE_OPT_NONEG: says that this option cannot be negated
   *   PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
   *                     shown only in the full usage.
 - *   PARSE_OPT_LASTARG_DEFAULT: if no argument is given, the default value
 - *                              is used.
 + *   PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
 + *                            value if no argument is given when the option
 + *                            is last on the command line. If the option is
 + *                            not last it will require an argument.
 + *                            Should not be used with PARSE_OPT_OPTARG.
   *   PARSE_OPT_NODASH: this option doesn't start with a dash.
   *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
   *                            (i.e. '<argh>') in the help message.
@@@ -104,14 -101,20 +104,20 @@@ struct option 
  };
  
  #define OPT_END()                   { OPTION_END }
- #define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
+ #define OPT_ARGUMENT(l, h)          { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
+                                     (h), PARSE_OPT_NOARG}
  #define OPT_GROUP(h)                { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
- #define OPT_BIT(s, l, v, h, b)      { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
- #define OPT_NEGBIT(s, l, v, h, b)   { OPTION_NEGBIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
- #define OPT_BOOLEAN(s, l, v, h)     { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
- #define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }
- #define OPT_SET_PTR(s, l, v, h, p)  { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) }
- #define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), NULL, (h) }
+ #define OPT_BIT(s, l, v, h, b)      { OPTION_BIT, (s), (l), (v), NULL, (h), \
+                                     PARSE_OPT_NOARG, NULL, (b) }
+ #define OPT_NEGBIT(s, l, v, h, b)   { OPTION_NEGBIT, (s), (l), (v), NULL, \
+                                     (h), PARSE_OPT_NOARG, NULL, (b) }
+ #define OPT_BOOLEAN(s, l, v, h)     { OPTION_BOOLEAN, (s), (l), (v), NULL, \
+                                     (h), PARSE_OPT_NOARG }
+ #define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, \
+                                     (h), PARSE_OPT_NOARG, NULL, (i) }
+ #define OPT_SET_PTR(s, l, v, h, p)  { OPTION_SET_PTR, (s), (l), (v), NULL, \
+                                     (h), PARSE_OPT_NOARG, NULL, (p) }
+ #define OPT_INTEGER(s, l, v, h)     { OPTION_INTEGER, (s), (l), (v), "n", (h) }
  #define OPT_STRING(s, l, v, a, h)   { OPTION_STRING,  (s), (l), (v), (a), (h) }
  #define OPT_DATE(s, l, v, h) \
        { OPTION_CALLBACK, (s), (l), (v), "time",(h), 0, \