]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-opt: ignore negation of OPT_NONEG for ambiguity checks
authorAndreas Schwab <schwab@linux-m68k.org>
Fri, 25 Sep 2009 18:44:44 +0000 (20:44 +0200)
committerShawn O. Pearce <spearce@spearce.org>
Tue, 29 Sep 2009 14:28:47 +0000 (07:28 -0700)
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>
parse-options.c
t/t0040-parse-options.sh
test-parse-options.c

index a64a4d6ee267cf2a691f546f668f3f644616408c..f5594114ede8a50090c8b97987b52a660235fa56 100644 (file)
@@ -230,6 +230,9 @@ is_abbreviated:
                                abbrev_flags = flags;
                                continue;
                        }
+                       /* negation allowed? */
+                       if (options->flags & PARSE_OPT_NONEG)
+                               continue;
                        /* negated and abbreviated very much? */
                        if (!prefixcmp("no-", arg)) {
                                flags |= OPT_UNSET;
index bbc821ef97de76ef532ec1d1d704da15e8973500..3d450ed379fcd1bf480fc75feb0e6ddb8f05e5be 100755 (executable)
@@ -33,6 +33,8 @@ Magic arguments
     --quux                means --quux
     -NUM                  set integer to NUM
     +                     same as -b
+    --ambiguous           positive ambiguity
+    --no-ambiguous        negative ambiguity
 
 Standard options
     --abbrev[=<n>]        use <n> digits to display SHA-1s
@@ -315,4 +317,22 @@ test_expect_success 'OPT_NUMBER_CALLBACK() works' '
        test_cmp expect output
 '
 
+cat >expect <<EOF
+boolean: 0
+integer: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: 0
+quiet: no
+dry run: no
+file: (not set)
+EOF
+
+test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
+       test-parse-options --no-ambig >output 2>output.err &&
+       test ! -s output.err &&
+       test_cmp expect output
+'
+
 test_done
index efa734b42e38d7f5a98393cd69ac0ffe10160ffc..acd1a2ba70fc2ffb38cd8fb0784aa6e5e693183f 100644 (file)
@@ -8,6 +8,7 @@ static int abbrev = 7;
 static int verbose = 0, dry_run = 0, quiet = 0;
 static char *string = NULL;
 static char *file = NULL;
+static int ambiguous;
 
 static int length_callback(const struct option *opt, const char *arg, int unset)
 {
@@ -59,6 +60,10 @@ int main(int argc, const char **argv)
                        number_callback),
                { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
                  PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
+               { OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
+                 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
+               { OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
+                 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
                OPT_GROUP("Standard options"),
                OPT__ABBREV(&abbrev),
                OPT__VERBOSE(&verbose),