Skip to content

Commit 29f25d4

Browse files
bebarinogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5acb3e5 commit 29f25d4

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

parse-options.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,20 @@ int parse_options(int argc, const char **argv, const struct option *options,
361361
return parse_options_end(&ctx);
362362
}
363363

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+
364378
#define USAGE_OPTS_WIDTH 24
365379
#define USAGE_GAP 2
366380

@@ -421,15 +435,9 @@ int usage_with_options_internal(const char * const *usagestr,
421435
break;
422436
/* FALLTHROUGH */
423437
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 {
433441
if (opts->flags & PARSE_OPT_OPTARG)
434442
if (opts->long_name)
435443
pos += fprintf(stderr, "[=...]");

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum parse_opt_option_flags {
3131
PARSE_OPT_NONEG = 4,
3232
PARSE_OPT_HIDDEN = 8,
3333
PARSE_OPT_LASTARG_DEFAULT = 16,
34+
PARSE_OPT_LITERAL_ARGHELP = 64,
3435
};
3536

3637
struct option;
@@ -66,6 +67,9 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
6667
* PARSE_OPT_NONEG: says that this option cannot be negated
6768
* PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
6869
* 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.
6973
*
7074
* `callback`::
7175
* pointer to the callback to use for OPTION_CALLBACK.

0 commit comments

Comments
 (0)