@matheusbsbs / matheus-bagliano / commits / 243e0614e0
Fork 0

parse-options: abbreviation engine fix.

When an option could be an ambiguous abbreviation of two options, the code used to error out. Even if an exact match would have occured later. Test and original patch by Pierre Habouzit. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 5, 2007 at 13:15 UTC 243e0614e0f8783599b20106b50eee56d0a17332
3 files changed +35 -12
parse-options.c
+21 -12
@@ -119,8 +119,8 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
119 const struct option *options)
120 {
121 const char *arg_end = strchr(arg, '=');
122 - const struct option *abbrev_option = NULL;
123 - int abbrev_flags = 0;
122 + const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
123 + int abbrev_flags = 0, ambiguous_flags = 0;
124
125 if (!arg_end)
126 arg_end = arg + strlen(arg);
@@ -137,16 +137,16 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
137 /* abbreviated? */
138 if (!strncmp(options->long_name, arg, arg_end - arg)) {
139 is_abbreviated:
140 - if (abbrev_option)
141 - return error("Ambiguous option: %s "
142 - "(could be --%s%s or --%s%s)",
143 - arg,
144 - (flags & OPT_UNSET) ?
145 - "no-" : "",
146 - options->long_name,
147 - (abbrev_flags & OPT_UNSET) ?
148 - "no-" : "",
149 - abbrev_option->long_name);
140 + if (abbrev_option) {
141 + /*
142 + * If this is abbreviated, it is
143 + * ambiguous. So when there is no
144 + * exact match later, we need to
145 + * error out.
146 + */
147 + ambiguous_option = abbrev_option;
148 + ambiguous_flags = abbrev_flags;
149 + }
150 if (!(flags & OPT_UNSET) && *arg_end)
151 p->opt = arg_end + 1;
152 abbrev_option = options;
@@ -176,6 +176,15 @@ is_abbreviated:
176 }
177 return get_value(p, options, flags);
178 }
179 +
180 + if (ambiguous_option)
181 + return error("Ambiguous option: %s "
182 + "(could be --%s%s or --%s%s)",
183 + arg,
184 + (ambiguous_flags & OPT_UNSET) ? "no-" : "",
185 + ambiguous_option->long_name,
186 + (abbrev_flags & OPT_UNSET) ? "no-" : "",
187 + abbrev_option->long_name);
188 if (abbrev_option)
189 return get_value(p, abbrev_option, abbrev_flags);
190 return error("unknown option `%s'", arg);
t/t0040-parse-options.sh
+13
@@ -18,6 +18,7 @@ string options
18 -s, --string <string>
19 get a string
20 --string2 <str> get another string
21 + --st <st> get another string (pervert ordering)
22
23 EOF
24
@@ -90,4 +91,16 @@ test_expect_failure 'ambiguously abbreviated option' '
91 test $? != 129
92 '
93
94 +cat > expect << EOF
95 +boolean: 0
96 +integer: 0
97 +string: 123
98 +EOF
99 +
100 +test_expect_success 'non ambiguous option (after two options it abbreviates)' '
101 + test-parse-options --st 123 > output 2> output.err &&
102 + test ! -s output.err &&
103 + git diff expect output
104 +'
105 +
106 test_done
test-parse-options.c
+1
@@ -18,6 +18,7 @@ int main(int argc, const char **argv)
18 OPT_GROUP("string options"),
19 OPT_STRING('s', "string", &string, "string", "get a string"),
20 OPT_STRING(0, "string2", &string, "str", "get another string"),
21 + OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
22 OPT_END(),
23 };
24 int i;