@matheusbsbs / matheus-bagliano / commits / 6bbfd1fa98
Fork 0

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 <schwab@linux-m68k.org> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Andreas Schwab committed Sep 25, 2009 at 20:44 UTC 6bbfd1fa98b0c1fa1684bd35e64404799f0cc2b3
3 files changed +28
parse-options.c
+3
@@ -230,6 +230,9 @@ is_abbreviated:
230 abbrev_flags = flags;
231 continue;
232 }
233 + /* negation allowed? */
234 + if (options->flags & PARSE_OPT_NONEG)
235 + continue;
236 /* negated and abbreviated very much? */
237 if (!prefixcmp("no-", arg)) {
238 flags |= OPT_UNSET;
t/t0040-parse-options.sh
+20
@@ -33,6 +33,8 @@ Magic arguments
33 --quux means --quux
34 -NUM set integer to NUM
35 + same as -b
36 + --ambiguous positive ambiguity
37 + --no-ambiguous negative ambiguity
38
39 Standard options
40 --abbrev[=<n>] use <n> digits to display SHA-1s
@@ -315,4 +317,22 @@ test_expect_success 'OPT_NUMBER_CALLBACK() works' '
317 test_cmp expect output
318 '
319
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 +
338 test_done
test-parse-options.c
+5
@@ -8,6 +8,7 @@ static int abbrev = 7;
8 static int verbose = 0, dry_run = 0, quiet = 0;
9 static char *string = NULL;
10 static char *file = NULL;
11 +static int ambiguous;
12
13 static int length_callback(const struct option *opt, const char *arg, int unset)
14 {
@@ -59,6 +60,10 @@ int main(int argc, const char **argv)
60 number_callback),
61 { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
62 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 },
67 OPT_GROUP("Standard options"),
68 OPT__ABBREV(&abbrev),
69 OPT__VERBOSE(&verbose),