Skip to content

Commit 4c25356

Browse files
avargitster
authored andcommitted
parse-options API: remove OPTION_ARGUMENT feature
As was noted in 1a85b49 (parse-options: make OPT_ARGUMENT() more useful, 2019-03-14) there's only ever been one user of the OPT_ARGUMENT(), that user was added in 20de316 (difftool: allow running outside Git worktrees with --no-index, 2019-03-14). The OPT_ARGUMENT() feature itself was added way back in 580d5bf (parse-options: new option type to treat an option-like parameter as an argument., 2008-03-02), but as discussed in 1a85b49 wasn't used until 20de316 in 2019. Now that the preceding commit has migrated this code over to using "struct strvec" to manage the "args" member of a "struct child_process", we can just use that directly instead of relying on OPT_ARGUMENT. This has a minor change in behavior in that if we'll pass --no-index we'll now always pass it as the first argument, before we'd pass it in whatever position the caller did. Preserving this was the real value of OPT_ARGUMENT(), but as it turns out we didn't need that either. We can always inject it as the first argument, the other end will parse it just the same. Note that we cannot remove the "out" and "cpidx" members of "struct parse_opt_ctx_t" added in 580d5bf, while they were introduced with OPT_ARGUMENT() we since used them for other things. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc5b594 commit 4c25356

File tree

6 files changed

+3
-28
lines changed

6 files changed

+3
-28
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,6 @@ 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, &int_var, description)`::
202-
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).
205-
206201
`OPT_NUMBER_CALLBACK(&var, description, func_ptr)`::
207202
Recognize numerical options like -123 and feed the integer as
208203
if it was an argument to the function given by `func_ptr`.

builtin/difftool.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
709709
"tool returns a non - zero exit code")),
710710
OPT_STRING('x', "extcmd", &extcmd, N_("command"),
711711
N_("specify a custom command for viewing diffs")),
712-
OPT_ARGUMENT("no-index", &no_index, N_("passed to `diff`")),
712+
OPT_BOOL(0, "no-index", &no_index, N_("passed to `diff`")),
713713
OPT_END()
714714
};
715715
struct child_process child = CHILD_PROCESS_INIT;
@@ -763,6 +763,8 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
763763
* each file that changed.
764764
*/
765765
strvec_push(&child.args, "diff");
766+
if (no_index)
767+
strvec_push(&child.args, "--no-index");
766768
if (dir_diff)
767769
strvec_pushl(&child.args, "--raw", "--no-abbrev", "-z", NULL);
768770
strvec_pushv(&child.args, argv);

parse-options.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,6 @@ static enum parse_opt_result parse_long_opt(
310310
again:
311311
if (!skip_prefix(arg, long_name, &rest))
312312
rest = NULL;
313-
if (options->type == OPTION_ARGUMENT) {
314-
if (!rest)
315-
continue;
316-
if (*rest == '=')
317-
return error(_("%s takes no value"),
318-
optname(options, flags));
319-
if (*rest)
320-
continue;
321-
if (options->value)
322-
*(int *)options->value = options->defval;
323-
p->out[p->cpidx++] = arg - 2;
324-
return PARSE_OPT_DONE;
325-
}
326313
if (!rest) {
327314
/* abbreviated? */
328315
if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN) &&

parse-options.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
enum parse_opt_type {
99
/* special types */
1010
OPTION_END,
11-
OPTION_ARGUMENT,
1211
OPTION_GROUP,
1312
OPTION_NUMBER,
1413
OPTION_ALIAS,
@@ -155,8 +154,6 @@ struct option {
155154
#define OPT_INTEGER_F(s, l, v, h, f) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h), (f) }
156155

157156
#define OPT_END() { OPTION_END }
158-
#define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
159-
(h), PARSE_OPT_NOARG, NULL, 1 }
160157
#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
161158
#define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
162159
#define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \

t/helper/test-parse-options.c

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

t/t0040-parse-options.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ String options
3737
--list <str> add str to list
3838
3939
Magic arguments
40-
--quux means --quux
4140
-NUM set integer to NUM
4241
+ same as -b
4342
--ambiguous positive ambiguity
@@ -263,10 +262,6 @@ test_expect_success 'detect possible typos' '
263262
test_cmp typo.err output.err
264263
'
265264

266-
test_expect_success 'keep some options as arguments' '
267-
test-tool parse-options --expect="arg 00: --quux" --quux
268-
'
269-
270265
cat >expect <<\EOF
271266
Callback: "four", 0
272267
boolean: 5

0 commit comments

Comments
 (0)