@matheusbsbs / matheus-bagliano / commits / 34aec9f573
Fork 0

parse-options: simplify usage argh handling

Simplify the argh printing by simply calling usage_argh() if the option can take an argument. Update macros defined in parse-options.h to set the PARSE_OPT_NOARG flag. The only other user of custom non-argument taking options is git-apply (in this case OPTION_BOOLEAN for deprecated options). Update it to set the PARSE_OPT_NOARG flag. Thanks to Ren辿 Scharfe for the suggestion and starter patch. Signed-off-by: Stephen Boyd <bebarino@gmail.com> Reviewd-by: René Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed Jun 4, 2009 at 16:43 UTC 34aec9f573af8e6fbe06285d660d9e1a84950e4c
3 files changed +21 -38
builtin-apply.c
+4 -2
@@ -3277,9 +3277,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
3277 OPT_BOOLEAN(0, "stat", &diffstat,
3278 "instead of applying the patch, output diffstat for the input"),
3279 { OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
3280 - NULL, "old option, now no-op", PARSE_OPT_HIDDEN },
3280 + NULL, "old option, now no-op",
3281 + PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3282 { OPTION_BOOLEAN, 0, "binary", &binary,
3282 - NULL, "old option, now no-op", PARSE_OPT_HIDDEN },
3283 + NULL, "old option, now no-op",
3284 + PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3285 OPT_BOOLEAN(0, "numstat", &numstat,
3286 "shows number of added and deleted lines in decimal notation"),
3287 OPT_BOOLEAN(0, "summary", &summary,
parse-options.c
+4 -29
@@ -440,7 +440,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
440 static int usage_argh(const struct option *opts)
441 {
442 const char *s;
443 - int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
443 + int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
444 if (opts->flags & PARSE_OPT_OPTARG)
445 if (opts->long_name)
446 s = literal ? "[=%s]" : "[=<%s>]";
@@ -448,7 +448,7 @@ static int usage_argh(const struct option *opts)
448 s = literal ? "[%s]" : "[<%s>]";
449 else
450 s = literal ? " %s" : " <%s>";
451 - return fprintf(stderr, s, opts->argh);
451 + return fprintf(stderr, s, opts->argh ? opts->argh : "...");
452 }
453
454 #define USAGE_OPTS_WIDTH 24
@@ -500,33 +500,8 @@ int usage_with_options_internal(const char * const *usagestr,
500 if (opts->type == OPTION_NUMBER)
501 pos += fprintf(stderr, "-NUM");
502
503 - switch (opts->type) {
504 - case OPTION_ARGUMENT:
505 - break;
506 - case OPTION_CALLBACK:
507 - if (opts->flags & PARSE_OPT_NOARG)
508 - break;
509 - /* FALLTHROUGH */
510 - case OPTION_INTEGER:
511 - /* FALLTHROUGH */
512 - case OPTION_FILENAME:
513 - /* FALLTHROUGH */
514 - case OPTION_STRING:
515 - if (opts->argh)
516 - pos += usage_argh(opts);
517 - else {
518 - if (opts->flags & PARSE_OPT_OPTARG)
519 - if (opts->long_name)
520 - pos += fprintf(stderr, "[=...]");
521 - else
522 - pos += fprintf(stderr, "[...]");
523 - else
524 - pos += fprintf(stderr, " ...");
525 - }
526 - break;
527 - default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
528 - break;
529 - }
503 + if (!(opts->flags & PARSE_OPT_NOARG))
504 + pos += usage_argh(opts);
505
506 if (pos <= USAGE_OPTS_WIDTH)
507 pad = USAGE_OPTS_WIDTH - pos;
parse-options.h
+13 -7
@@ -67,7 +67,7 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
67 * `flags`::
68 * mask of parse_opt_option_flags.
69 * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
70 - * PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
70 + * PARSE_OPT_NOARG: says that this option takes no argument
71 * PARSE_OPT_NONEG: says that this option cannot be negated
72 * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
73 * shown only in the full usage.
@@ -101,13 +101,19 @@ struct option {
101 };
102
103 #define OPT_END() { OPTION_END }
104 -#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
104 +#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
105 + (h), PARSE_OPT_NOARG}
106 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
106 -#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
107 -#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
108 -#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
109 -#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }
110 -#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) }
107 +#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), \
108 + PARSE_OPT_NOARG, NULL, (b) }
109 +#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
110 + (h), PARSE_OPT_NOARG, NULL, (b) }
111 +#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, \
112 + (h), PARSE_OPT_NOARG }
113 +#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
114 + (h), PARSE_OPT_NOARG, NULL, (i) }
115 +#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
116 + (h), PARSE_OPT_NOARG, NULL, (p) }
117 #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
118 #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
119 #define OPT_DATE(s, l, v, h) \