Skip to content

Commit 6bbfd1f

Browse files
andreas-schwabspearce
authored andcommitted
parse-opt: ignore negation of OPT_NONEG for ambiguity checks
parse_long_opt always matches both --opt and --no-opt for any option "opt", and only get_value checks whether --no-opt is actually valid. Since the options for git branch contains both "no-merged" and "merged" there are two matches for --no-merge, but no exact match. With this patch the negation of a NONEG option is rejected earlier, but it changes the error message from "option `no-opt' isn't available" to "unknown option `no-opt'". [jk: added test] Signed-off-by: Andreas Schwab <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 5bdc32d commit 6bbfd1f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

parse-options.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
230230
abbrev_flags = flags;
231231
continue;
232232
}
233+
/* negation allowed? */
234+
if (options->flags & PARSE_OPT_NONEG)
235+
continue;
233236
/* negated and abbreviated very much? */
234237
if (!prefixcmp("no-", arg)) {
235238
flags |= OPT_UNSET;

t/t0040-parse-options.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Magic arguments
3333
--quux means --quux
3434
-NUM set integer to NUM
3535
+ same as -b
36+
--ambiguous positive ambiguity
37+
--no-ambiguous negative ambiguity
3638
3739
Standard options
3840
--abbrev[=<n>] use <n> digits to display SHA-1s
@@ -315,4 +317,22 @@ test_expect_success 'OPT_NUMBER_CALLBACK() works' '
315317
test_cmp expect output
316318
'
317319

320+
cat >expect <<EOF
321+
boolean: 0
322+
integer: 0
323+
timestamp: 0
324+
string: (not set)
325+
abbrev: 7
326+
verbose: 0
327+
quiet: no
328+
dry run: no
329+
file: (not set)
330+
EOF
331+
332+
test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
333+
test-parse-options --no-ambig >output 2>output.err &&
334+
test ! -s output.err &&
335+
test_cmp expect output
336+
'
337+
318338
test_done

test-parse-options.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static int abbrev = 7;
88
static int verbose = 0, dry_run = 0, quiet = 0;
99
static char *string = NULL;
1010
static char *file = NULL;
11+
static int ambiguous;
1112

1213
static int length_callback(const struct option *opt, const char *arg, int unset)
1314
{
@@ -59,6 +60,10 @@ int main(int argc, const char **argv)
5960
number_callback),
6061
{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
6162
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
63+
{ OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
64+
"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
65+
{ OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
66+
"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
6267
OPT_GROUP("Standard options"),
6368
OPT__ABBREV(&abbrev),
6469
OPT__VERBOSE(&verbose),

0 commit comments

Comments
 (0)