@matheusbsbs / matheus-bagliano / commits / 29f25d493c
Fork 0

parse-options: add PARSE_OPT_LITERAL_ARGHELP for complicated argh's

Usually, the argh element in struct option points at a placeholder value (e.g. "val"), and is shown in the usage message as --option=<val> by enclosing the string inside of angle brackets. When the option is more complex (e.g. optional arguments separated by a comma), you would want to produce a usage message that looks like --option=<val1>[,<val2>] In such a case, the caller can pass a string to argh with placeholders already enclosed in necessary angle brackets (e.g. "<val1>[,<val2>]") and set this flag. Signed-off-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed May 21, 2009 at 00:33 UTC 29f25d493c1021a53acf41e5763e732217dd75c3
2 files changed +21 -9
parse-options.c
+17 -9
@@ -361,6 +361,20 @@ int parse_options(int argc, const char **argv, const struct option *options,
361 return parse_options_end(&ctx);
362 }
363
364 +static int usage_argh(const struct option *opts)
365 +{
366 + const char *s;
367 + int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
368 + if (opts->flags & PARSE_OPT_OPTARG)
369 + if (opts->long_name)
370 + s = literal ? "[=%s]" : "[=<%s>]";
371 + else
372 + s = literal ? "[%s]" : "[<%s>]";
373 + else
374 + s = literal ? " %s" : " <%s>";
375 + return fprintf(stderr, s, opts->argh);
376 +}
377 +
378 #define USAGE_OPTS_WIDTH 24
379 #define USAGE_GAP 2
380
@@ -421,15 +435,9 @@ int usage_with_options_internal(const char * const *usagestr,
435 break;
436 /* FALLTHROUGH */
437 case OPTION_STRING:
424 - if (opts->argh) {
425 - if (opts->flags & PARSE_OPT_OPTARG)
426 - if (opts->long_name)
427 - pos += fprintf(stderr, "[=<%s>]", opts->argh);
428 - else
429 - pos += fprintf(stderr, "[<%s>]", opts->argh);
430 - else
431 - pos += fprintf(stderr, " <%s>", opts->argh);
432 - } else {
438 + if (opts->argh)
439 + pos += usage_argh(opts);
440 + else {
441 if (opts->flags & PARSE_OPT_OPTARG)
442 if (opts->long_name)
443 pos += fprintf(stderr, "[=...]");
parse-options.h
+4
@@ -31,6 +31,7 @@ enum parse_opt_option_flags {
31 PARSE_OPT_NONEG = 4,
32 PARSE_OPT_HIDDEN = 8,
33 PARSE_OPT_LASTARG_DEFAULT = 16,
34 + PARSE_OPT_LITERAL_ARGHELP = 64,
35 };
36
37 struct option;
@@ -66,6 +67,9 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
67 * PARSE_OPT_NONEG: says that this option cannot be negated
68 * PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
69 * the long one.
70 + * PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
71 + * (i.e. '<argh>') in the help message.
72 + * Useful for options with multiple parameters.
73 *
74 * `callback`::
75 * pointer to the callback to use for OPTION_CALLBACK.