From 29f25d493c1021a53acf41e5763e732217dd75c3 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Thu, 21 May 2009 00:33:17 -0700 Subject: 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= 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=[,] In such a case, the caller can pass a string to argh with placeholders already enclosed in necessary angle brackets (e.g. "[,]") and set this flag. Signed-off-by: Stephen Boyd Signed-off-by: Junio C Hamano --- parse-options.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'parse-options.c') diff --git a/parse-options.c b/parse-options.c index cf71bcffd..e8955be7b 100644 --- a/parse-options.c +++ b/parse-options.c @@ -361,6 +361,20 @@ int parse_options(int argc, const char **argv, const struct option *options, return parse_options_end(&ctx); } +static int usage_argh(const struct option *opts) +{ + const char *s; + int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP; + if (opts->flags & PARSE_OPT_OPTARG) + if (opts->long_name) + s = literal ? "[=%s]" : "[=<%s>]"; + else + s = literal ? "[%s]" : "[<%s>]"; + else + s = literal ? " %s" : " <%s>"; + return fprintf(stderr, s, opts->argh); +} + #define USAGE_OPTS_WIDTH 24 #define USAGE_GAP 2 @@ -421,15 +435,9 @@ int usage_with_options_internal(const char * const *usagestr, break; /* FALLTHROUGH */ case OPTION_STRING: - if (opts->argh) { - if (opts->flags & PARSE_OPT_OPTARG) - 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->argh) + pos += usage_argh(opts); + else { if (opts->flags & PARSE_OPT_OPTARG) if (opts->long_name) pos += fprintf(stderr, "[=...]"); -- cgit v1.2.1