Skip to content

Commit b04ba2b

Browse files
committed
parse-options: deprecate OPT_BOOLEAN
It is natural to expect that an option defined with OPT_BOOLEAN() could be used in this way: int option = -1; /* unspecified */ struct option options[] = { OPT_BOOLEAN(0, "option", &option, "set option"), OPT_END() }; parse_options(ac, av, prefix, options, usage, 0); if (option < 0) ... do the default thing ... else if (!option) ... --no-option was given ... else ... --option was given ... to easily tell three cases apart: - There is no mention of the `--option` on the command line; - The variable is positively set with `--option`; or - The variable is explicitly negated with `--no-option`. Unfortunately, this is not the case. OPT_BOOLEAN() increments the variable every time `--option` is given, and resets it to zero when `--no-option` is given. As a first step to remedy this, introduce a true boolean OPT_BOOL(), and rename OPT_BOOLEAN() to OPT_COUNTUP(). To help transitioning, OPT_BOOLEAN and OPTION_BOOLEAN are defined as deprecated synonyms to OPT_COUNTUP and OPTION_COUNTUP respectively. This is what db7244b (parse-options new features., 2007-11-07) from four years ago started by marking OPTION_BOOLEAN as "INCR would have been a better name". Some existing users do depend on the count-up semantics; for example, users of OPT__VERBOSE() could use it to raise the verbosity level with repeated use of `-v` on the command line, but they probably should be rewritten to use OPT__VERBOSITY() instead these days. I suspect that some users of OPT__FORCE() may also use it to implement different level of forcibleness but I didn't check. On top of this patch, here are the remaining clean-up tasks that other people can help: - Look at each hit in "git grep -e OPT_BOOLEAN"; trace all uses of the value that is set to the underlying variable, and if it can proven that the variable is only used as a boolean, replace it with OPT_BOOL(). If the caller does depend on the count-up semantics, replace it with OPT_COUNTUP() instead. - Same for OPTION_BOOLEAN; replace it with OPTION_SET_INT and arrange to set 1 to the variable for a true boolean, and otherwise replace it with OPTION_COUNTUP. - Look at each hit in "git grep -e OPT__VERBOSE -e OPT__QUIET" and see if they can be replaced with OPT__VERBOSITY(). I'll follow this message up with a separate patch as an example. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d714b1 commit b04ba2b

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,14 @@ There are some macros to easily define options:
135135
describes the group or an empty string.
136136
Start the description with an upper-case letter.
137137

138-
`OPT_BOOLEAN(short, long, &int_var, description)`::
139-
Introduce a boolean option.
140-
`int_var` is incremented on each use.
138+
`OPT_BOOL(short, long, &int_var, description)`::
139+
Introduce a boolean option. `int_var` is set to one with
140+
`--option` and set to zero with `--no-option`.
141+
142+
`OPT_COUNTUP(short, long, &int_var, description)`::
143+
Introduce a count-up option.
144+
`int_var` is incremented on each use of `--option`, and
145+
reset to zero with `--no-option`.
141146

142147
`OPT_BIT(short, long, &int_var, description, mask)`::
143148
Introduce a boolean option.
@@ -148,8 +153,9 @@ There are some macros to easily define options:
148153
If used, `int_var` is bitwise-anded with the inverted `mask`.
149154

150155
`OPT_SET_INT(short, long, &int_var, description, integer)`::
151-
Introduce a boolean option.
152-
If used, set `int_var` to `integer`.
156+
Introduce an integer option.
157+
`int_var` is set to `integer` with `--option`, and
158+
reset to zero with `--no-option`.
153159

154160
`OPT_SET_PTR(short, long, &ptr_var, description, ptr)`::
155161
Introduce a boolean option.

parse-options.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int get_value(struct parse_opt_ctx_t *p,
8383
*(int *)opt->value &= ~opt->defval;
8484
return 0;
8585

86-
case OPTION_BOOLEAN:
86+
case OPTION_COUNTUP:
8787
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
8888
return 0;
8989

@@ -319,7 +319,7 @@ static void parse_options_check(const struct option *opts)
319319
err |= optbug(opts, "uses feature "
320320
"not supported for dashless options");
321321
switch (opts->type) {
322-
case OPTION_BOOLEAN:
322+
case OPTION_COUNTUP:
323323
case OPTION_BIT:
324324
case OPTION_NEGBIT:
325325
case OPTION_SET_INT:

parse-options.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum parse_opt_type {
1010
/* options with no arguments */
1111
OPTION_BIT,
1212
OPTION_NEGBIT,
13-
OPTION_BOOLEAN, /* _INCR would have been a better name */
13+
OPTION_COUNTUP,
1414
OPTION_SET_INT,
1515
OPTION_SET_PTR,
1616
/* options with arguments (usually) */
@@ -21,6 +21,9 @@ enum parse_opt_type {
2121
OPTION_FILENAME
2222
};
2323

24+
/* Deprecated synonym */
25+
#define OPTION_BOOLEAN OPTION_COUNTUP
26+
2427
enum parse_opt_flags {
2528
PARSE_OPT_KEEP_DASHDASH = 1,
2629
PARSE_OPT_STOP_AT_NON_OPTION = 2,
@@ -122,10 +125,11 @@ struct option {
122125
PARSE_OPT_NOARG, NULL, (b) }
123126
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
124127
(h), PARSE_OPT_NOARG, NULL, (b) }
125-
#define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, \
128+
#define OPT_COUNTUP(s, l, v, h) { OPTION_COUNTUP, (s), (l), (v), NULL, \
126129
(h), PARSE_OPT_NOARG }
127130
#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
128131
(h), PARSE_OPT_NOARG, NULL, (i) }
132+
#define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
129133
#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
130134
(h), PARSE_OPT_NOARG, NULL, (p) }
131135
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
@@ -149,6 +153,8 @@ struct option {
149153
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
150154
parse_opt_color_flag_cb, (intptr_t)"always" }
151155

156+
/* Deprecated synonym */
157+
#define OPT_BOOLEAN OPT_COUNTUP
152158

153159
/* parse_options() will filter out the processed options and leave the
154160
* non-option arguments in argv[].

0 commit comments

Comments
 (0)