Skip to content

Commit d978ead

Browse files
committed
Merge branch 'sb/parse-options-integer'
* sb/parse-options-integer: parse-options: simplify usage argh handling parse-options: make OPT_INTEGER's argh explicit
2 parents 3b91202 + 34aec9f commit d978ead

File tree

4 files changed

+24
-48
lines changed

4 files changed

+24
-48
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,

builtin-tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
376376
struct commit_list *with_commit = NULL;
377377
struct option options[] = {
378378
OPT_BOOLEAN('l', NULL, &list, "list tag names"),
379-
{ OPTION_INTEGER, 'n', NULL, &lines, NULL,
380-
"print n lines of each tag message",
379+
{ OPTION_INTEGER, 'n', NULL, &lines, "n",
380+
"print <n> lines of each tag message",
381381
PARSE_OPT_OPTARG, NULL, 1 },
382382
OPT_BOOLEAN('d', NULL, &delete, "delete tags"),
383383
OPT_BOOLEAN('v', NULL, &verify, "verify tags"),

parse-options.c

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,15 @@ int parse_options(int argc, const char **argv, const char *prefix,
464464
static int usage_argh(const struct option *opts)
465465
{
466466
const char *s;
467-
int literal = opts->flags & PARSE_OPT_LITERAL_ARGHELP;
467+
int literal = (opts->flags & PARSE_OPT_LITERAL_ARGHELP) || !opts->argh;
468468
if (opts->flags & PARSE_OPT_OPTARG)
469469
if (opts->long_name)
470470
s = literal ? "[=%s]" : "[=<%s>]";
471471
else
472472
s = literal ? "[%s]" : "[<%s>]";
473473
else
474474
s = literal ? " %s" : " <%s>";
475-
return fprintf(stderr, s, opts->argh);
475+
return fprintf(stderr, s, opts->argh ? opts->argh : "...");
476476
}
477477

478478
#define USAGE_OPTS_WIDTH 24
@@ -524,40 +524,8 @@ int usage_with_options_internal(const char * const *usagestr,
524524
if (opts->type == OPTION_NUMBER)
525525
pos += fprintf(stderr, "-NUM");
526526

527-
switch (opts->type) {
528-
case OPTION_ARGUMENT:
529-
break;
530-
case OPTION_INTEGER:
531-
if (opts->flags & PARSE_OPT_OPTARG)
532-
if (opts->long_name)
533-
pos += fprintf(stderr, "[=<n>]");
534-
else
535-
pos += fprintf(stderr, "[<n>]");
536-
else
537-
pos += fprintf(stderr, " <n>");
538-
break;
539-
case OPTION_CALLBACK:
540-
if (opts->flags & PARSE_OPT_NOARG)
541-
break;
542-
/* FALLTHROUGH */
543-
case OPTION_FILENAME:
544-
/* FALLTHROUGH */
545-
case OPTION_STRING:
546-
if (opts->argh)
547-
pos += usage_argh(opts);
548-
else {
549-
if (opts->flags & PARSE_OPT_OPTARG)
550-
if (opts->long_name)
551-
pos += fprintf(stderr, "[=...]");
552-
else
553-
pos += fprintf(stderr, "[...]");
554-
else
555-
pos += fprintf(stderr, " ...");
556-
}
557-
break;
558-
default: /* OPTION_{BIT,BOOLEAN,NUMBER,SET_INT,SET_PTR} */
559-
break;
560-
}
527+
if (!(opts->flags & PARSE_OPT_NOARG))
528+
pos += usage_argh(opts);
561529

562530
if (pos <= USAGE_OPTS_WIDTH)
563531
pad = USAGE_OPTS_WIDTH - pos;

parse-options.h

Lines changed: 14 additions & 8 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.
@@ -104,14 +104,20 @@ struct option {
104104
};
105105

106106
#define OPT_END() { OPTION_END }
107-
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, (h) }
107+
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
108+
(h), PARSE_OPT_NOARG}
108109
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
109-
#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
110-
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, (h), 0, NULL, (b) }
111-
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
112-
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, (h), 0, NULL, (i) }
113-
#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, (h), 0, NULL, (p) }
114-
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), NULL, (h) }
110+
#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), \
111+
PARSE_OPT_NOARG, NULL, (b) }
112+
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
113+
(h), PARSE_OPT_NOARG, NULL, (b) }
114+
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, \
115+
(h), PARSE_OPT_NOARG }
116+
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
117+
(h), PARSE_OPT_NOARG, NULL, (i) }
118+
#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
119+
(h), PARSE_OPT_NOARG, NULL, (p) }
120+
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
115121
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
116122
#define OPT_DATE(s, l, v, h) \
117123
{ OPTION_CALLBACK, (s), (l), (v), "time",(h), 0, \

0 commit comments

Comments
 (0)