Skip to content

Commit 1a85b49

Browse files
dschogitster
authored andcommitted
parse-options: make OPT_ARGUMENT() more useful
`OPT_ARGUMENT()` is intended to keep the specified long option in `argv` and not to do anything else. However, it would make a lot of sense for the caller to know whether this option was seen at all or not. For example, we want to teach `git difftool` to work outside of any Git worktree, but only when `--no-index` was specified. Note: nothing in Git uses OPT_ARGUMENT(). Even worse, looking through the commit history, one can easily see that nothing even ever used it, apart from the regression test. So not only do we make `OPT_ARGUMENT()` more useful, we are also about to introduce its first real user! Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1dcda05 commit 1a85b49

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ There are some macros to easily define options:
198198
The filename will be prefixed by passing the filename along with
199199
the prefix argument of `parse_options()` to `prefix_filename()`.
200200

201-
`OPT_ARGUMENT(long, description)`::
201+
`OPT_ARGUMENT(long, &int_var, description)`::
202202
Introduce a long-option argument that will be kept in `argv[]`.
203+
If this option was seen, `int_var` will be set to one (except
204+
if a `NULL` pointer was passed).
203205

204206
`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
205207
Recognize numerical options like -123 and feed the integer as

parse-options.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ static enum parse_opt_result parse_long_opt(
286286
optname(options, flags));
287287
if (*rest)
288288
continue;
289+
if (options->value)
290+
*(int *)options->value = options->defval;
289291
p->out[p->cpidx++] = arg - 2;
290292
return PARSE_OPT_DONE;
291293
}

parse-options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ struct option {
138138
{ OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), (cb) }
139139

140140
#define OPT_END() { OPTION_END }
141-
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
142-
(h), PARSE_OPT_NOARG}
141+
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
142+
(h), PARSE_OPT_NOARG, NULL, 1 }
143143
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
144144
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
145145
#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \

t/helper/test-parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int cmd__parse_options(int argc, const char **argv)
132132
OPT_NOOP_NOARG(0, "obsolete"),
133133
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
134134
OPT_GROUP("Magic arguments"),
135-
OPT_ARGUMENT("quux", "means --quux"),
135+
OPT_ARGUMENT("quux", NULL, "means --quux"),
136136
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
137137
number_callback),
138138
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",

0 commit comments

Comments
 (0)