Skip to content

Commit 34aec9f

Browse files
bebarinogitster
authored andcommitted
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>
1 parent e3a0ca8 commit 34aec9f

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

builtin-apply.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,9 +3277,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
32773277
OPT_BOOLEAN(0, "stat", &diffstat,
32783278
"instead of applying the patch, output diffstat for the input"),
32793279
{ 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 },
32813282
{ 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 },
32833285
OPT_BOOLEAN(0, "numstat", &numstat,
32843286
"shows number of added and deleted lines in decimal notation"),
32853287
OPT_BOOLEAN(0, "summary", &summary,

parse-options.c

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -440,15 +440,15 @@ int parse_options(int argc, const char **argv, const char *prefix,
440440
static int usage_argh(const struct option *opts)
441441
{
442442
const char *s;
443-
int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
443+
int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
444444
if (opts->flags & PARSE_OPT_OPTARG)
445445
if (opts->long_name)
446446
s = literal ? "[=%s]" : "[=<%s>]";
447447
else
448448
s = literal ? "[%s]" : "[<%s>]";
449449
else
450450
s = literal ? " %s" : " <%s>";
451-
return fprintf(stderr, s, opts->argh);
451+
return fprintf(stderr, s, opts->argh ? opts->argh : "...");
452452
}
453453

454454
#define USAGE_OPTS_WIDTH 24
@@ -500,33 +500,8 @@ int usage_with_options_internal(const char * const *usagestr,
500500
if (opts->type == OPTION_NUMBER)
501501
pos += fprintf(stderr, "-NUM");
502502

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);
530505

531506
if (pos <= USAGE_OPTS_WIDTH)
532507
pad = USAGE_OPTS_WIDTH - pos;

parse-options.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
6767
* `flags`::
6868
* mask of parse_opt_option_flags.
6969
* 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
7171
* PARSE_OPT_NONEG: says that this option cannot be negated
7272
* PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
7373
* shown only in the full usage.
@@ -101,13 +101,19 @@ struct option {
101101
};
102102

103103
#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}
105106
#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) }
111117
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
112118
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
113119
#define OPT_DATE(s, l, v, h) \

0 commit comments

Comments
 (0)