@matheusbsbs / matheus-bagliano / commits / 3a9f0f41db
Fork 0

parse-options: catch likely typo in presense of aggregated options.

If options are aggregated, and that the whole token is an exact prefix of a long option that is longer than 2 letters, reject it. This is to prevent a common typo: $ git commit -amend to get interpreted as "commit all with message 'end'". The typo check isn't performed if there is no aggregation, because the stuck form is the recommended one. If we have `-o` being a valid short option that takes an argument, and --option a long one, then we _MUST_ accept -option as "'o' option with argument 'ption'", which is our official recommended form. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Jan 26, 2008 at 12:26 UTC 3a9f0f41db87e197708f84aeb2487bc983f99c9c
3 files changed +40 -2
parse-options.c
+28 -2
@@ -216,6 +216,26 @@ is_abbreviated:
216 return error("unknown option `%s'", arg);
217 }
218
219 +void check_typos(const char *arg, const struct option *options)
220 +{
221 + if (strlen(arg) < 3)
222 + return;
223 +
224 + if (!prefixcmp(arg, "no-")) {
225 + error ("did you mean `--%s` (with two dashes ?)", arg);
226 + exit(129);
227 + }
228 +
229 + for (; options->type != OPTION_END; options++) {
230 + if (!options->long_name)
231 + continue;
232 + if (!prefixcmp(options->long_name, arg)) {
233 + error ("did you mean `--%s` (with two dashes ?)", arg);
234 + exit(129);
235 + }
236 + }
237 +}
238 +
239 static NORETURN void usage_with_options_internal(const char * const *,
240 const struct option *, int);
241
@@ -235,12 +255,18 @@ int parse_options(int argc, const char **argv, const struct option *options,
255
256 if (arg[1] != '-') {
257 args.opt = arg + 1;
238 - do {
258 + if (*args.opt == 'h')
259 + usage_with_options(usagestr, options);
260 + if (parse_short_opt(&args, options) < 0)
261 + usage_with_options(usagestr, options);
262 + if (args.opt)
263 + check_typos(arg + 1, options);
264 + while (args.opt) {
265 if (*args.opt == 'h')
266 usage_with_options(usagestr, options);
267 if (parse_short_opt(&args, options) < 0)
268 usage_with_options(usagestr, options);
243 - } while (args.opt);
269 + }
270 continue;
271 }
272
t/t0040-parse-options.sh
+11
@@ -19,6 +19,7 @@ string options
19 get a string
20 --string2 <str> get another string
21 --st <st> get another string (pervert ordering)
22 + -o <str> get another string
23
24 EOF
25
@@ -103,4 +104,14 @@ test_expect_success 'non ambiguous option (after two options it abbreviates)' '
104 git diff expect output
105 '
106
107 +cat > expect.err << EOF
108 +error: did you mean \`--boolean\` (with two dashes ?)
109 +EOF
110 +
111 +test_expect_success 'detect possible typos' '
112 + ! test-parse-options -boolean > output 2> output.err &&
113 + test ! -s output &&
114 + git diff expect.err output.err
115 +'
116 +
117 test_done
test-parse-options.c
+1
@@ -19,6 +19,7 @@ int main(int argc, const char **argv)
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_STRING('o', NULL, &string, "str", "get another string"),
23 OPT_END(),
24 };
25 int i;