Skip to content

Commit af54383

Browse files
committed
Merge branch 'jc/parse-options-boolean'
* jc/parse-options-boolean: apply: use OPT_NOOP_NOARG revert: use OPT_NOOP_NOARG parseopt: add OPT_NOOP_NOARG archive.c: use OPT_BOOL() parse-options: deprecate OPT_BOOLEAN Conflicts: builtin/revert.c
2 parents fbca691 + af1032e commit af54383

File tree

10 files changed

+53
-22
lines changed

10 files changed

+53
-22
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 16 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.
@@ -198,6 +204,11 @@ There are some macros to easily define options:
198204
"auto", set `int_var` to 1 if stdout is a tty or a pager,
199205
0 otherwise.
200206

207+
`OPT_NOOP_NOARG(short, long)`::
208+
Introduce an option that has no effect and takes no arguments.
209+
Use it to hide deprecated options that are still to be recognized
210+
and ignored silently.
211+
201212

202213
The last element of the array must be `OPT_END()`.
203214

archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static int parse_archive_args(int argc, const char **argv,
318318
"prepend prefix to each pathname in the archive"),
319319
OPT_STRING('o', "output", &output, "file",
320320
"write the archive to this file"),
321-
OPT_BOOLEAN(0, "worktree-attributes", &worktree_attributes,
321+
OPT_BOOL(0, "worktree-attributes", &worktree_attributes,
322322
"read .gitattributes in working directory"),
323323
OPT__VERBOSE(&verbose, "report archived files on stderr"),
324324
OPT__COMPR('0', &compression_level, "store only", 0),
@@ -332,7 +332,7 @@ static int parse_archive_args(int argc, const char **argv,
332332
OPT__COMPR_HIDDEN('8', &compression_level, 8),
333333
OPT__COMPR('9', &compression_level, "compress better", 9),
334334
OPT_GROUP(""),
335-
OPT_BOOLEAN('l', "list", &list,
335+
OPT_BOOL('l', "list", &list,
336336
"list supported archive formats"),
337337
OPT_GROUP(""),
338338
OPT_STRING(0, "remote", &remote, "repo",

builtin/apply.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,7 +3831,6 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
38313831
int i;
38323832
int errs = 0;
38333833
int is_not_gitdir = !startup_info->have_repository;
3834-
int binary;
38353834
int force_apply = 0;
38363835

38373836
const char *whitespace_option = NULL;
@@ -3850,12 +3849,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
38503849
"ignore additions made by the patch"),
38513850
OPT_BOOLEAN(0, "stat", &diffstat,
38523851
"instead of applying the patch, output diffstat for the input"),
3853-
{ OPTION_BOOLEAN, 0, "allow-binary-replacement", &binary,
3854-
NULL, "old option, now no-op",
3855-
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3856-
{ OPTION_BOOLEAN, 0, "binary", &binary,
3857-
NULL, "old option, now no-op",
3858-
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG },
3852+
OPT_NOOP_NOARG(0, "allow-binary-replacement"),
3853+
OPT_NOOP_NOARG(0, "binary"),
38593854
OPT_BOOLEAN(0, "numstat", &numstat,
38603855
"shows number of added and deleted lines in decimal notation"),
38613856
OPT_BOOLEAN(0, "summary", &summary,

builtin/revert.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
133133
{
134134
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
135135
const char *me = action_name(opts);
136-
int noop;
137136
int reset = 0;
138137
int contin = 0;
139138
struct option options[] = {
140139
OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
141140
OPT_BOOLEAN(0, "continue", &contin, "continue the current operation"),
142141
OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
143142
OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
144-
{ OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)",
145-
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 0 },
143+
OPT_NOOP_NOARG('r', NULL),
146144
OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
147145
OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
148146
OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),

parse-options-cb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
123123
string_list_append(v, xstrdup(arg));
124124
return 0;
125125
}
126+
127+
int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
128+
{
129+
return 0;
130+
}

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: 14 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,13 @@ struct option {
149153
{ OPTION_CALLBACK, (s), (l), (v), "when", (h), PARSE_OPT_OPTARG, \
150154
parse_opt_color_flag_cb, (intptr_t)"always" }
151155

156+
#define OPT_NOOP_NOARG(s, l) \
157+
{ OPTION_CALLBACK, (s), (l), NULL, NULL, \
158+
"no-op (backward compatibility)", \
159+
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
160+
161+
/* Deprecated synonym */
162+
#define OPT_BOOLEAN OPT_COUNTUP
152163

153164
/* parse_options() will filter out the processed options and leave the
154165
* non-option arguments in argv[].
@@ -210,6 +221,7 @@ extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
210221
extern int parse_opt_with_commit(const struct option *, const char *, int);
211222
extern int parse_opt_tertiary(const struct option *, const char *, int);
212223
extern int parse_opt_string_list(const struct option *, const char *, int);
224+
extern int parse_opt_noop_cb(const struct option *, const char *, int);
213225

214226
#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
215227
#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))

t/t0040-parse-options.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ EOF
8787
test_expect_success 'long options' '
8888
test-parse-options --boolean --integer 1729 --boolean --string2=321 \
8989
--verbose --verbose --no-dry-run --abbrev=10 --file fi.le\
90-
> output 2> output.err &&
90+
--obsolete > output 2> output.err &&
9191
test ! -s output.err &&
9292
test_cmp expect output
9393
'

t/t5001-archive-attr.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ test_expect_missing worktree/ignored
5757
test_expect_exists worktree/ignored-by-tree
5858
test_expect_missing worktree/ignored-by-worktree
5959

60+
test_expect_success 'git archive --worktree-attributes option' '
61+
git archive --worktree-attributes --worktree-attributes HEAD >worktree.tar &&
62+
(mkdir worktree2 && cd worktree2 && "$TAR" xf -) <worktree.tar
63+
'
64+
65+
test_expect_missing worktree2/ignored
66+
test_expect_exists worktree2/ignored-by-tree
67+
test_expect_missing worktree2/ignored-by-worktree
68+
6069
test_expect_success 'git archive vs. bare' '
6170
(cd bare && git archive HEAD) >bare-archive.tar &&
6271
test_cmp archive.tar bare-archive.tar

test-parse-options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int main(int argc, const char **argv)
5454
OPT_STRING(0, "string2", &string, "str", "get another string"),
5555
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
5656
OPT_STRING('o', NULL, &string, "str", "get another string"),
57+
OPT_NOOP_NOARG(0, "obsolete"),
5758
OPT_SET_PTR(0, "default-string", &string,
5859
"set string to default", (unsigned long)"default"),
5960
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),

0 commit comments

Comments
 (0)