Skip to content

Commit 9732ed1

Browse files
committed
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]>
1 parent b9317c5 commit 9732ed1

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
@@ -261,6 +261,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
261261
optname(options, flags));
262262
if (*rest)
263263
continue;
264+
if (options->value)
265+
*(int *)options->value = options->defval;
264266
p->out[p->cpidx++] = arg - 2;
265267
return 0;
266268
}

parse-options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ struct option {
128128
#define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f)
129129

130130
#define OPT_END() { OPTION_END }
131-
#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
132-
(h), PARSE_OPT_NOARG}
131+
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
132+
(h), PARSE_OPT_NOARG, NULL, 1 }
133133
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
134134
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
135135
#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \

t/helper/test-parse-options.c

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

0 commit comments

Comments
 (0)