Skip to content

Commit 3a9f0f4

Browse files
MadCodergitster
authored andcommitted
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>
1 parent 923e3ec commit 3a9f0f4

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

parse-options.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,26 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
216216
return error("unknown option `%s'", arg);
217217
}
218218

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+
219239
static NORETURN void usage_with_options_internal(const char * const *,
220240
const struct option *, int);
221241

@@ -235,12 +255,18 @@ int parse_options(int argc, const char **argv, const struct option *options,
235255

236256
if (arg[1] != '-') {
237257
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) {
239265
if (*args.opt == 'h')
240266
usage_with_options(usagestr, options);
241267
if (parse_short_opt(&args, options) < 0)
242268
usage_with_options(usagestr, options);
243-
} while (args.opt);
269+
}
244270
continue;
245271
}
246272

t/t0040-parse-options.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ string options
1919
get a string
2020
--string2 <str> get another string
2121
--st <st> get another string (pervert ordering)
22+
-o <str> get another string
2223
2324
EOF
2425

@@ -103,4 +104,14 @@ test_expect_success 'non ambiguous option (after two options it abbreviates)' '
103104
git diff expect output
104105
'
105106

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+
106117
test_done

test-parse-options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ int main(int argc, const char **argv)
1919
OPT_STRING('s', "string", &string, "string", "get a string"),
2020
OPT_STRING(0, "string2", &string, "str", "get another string"),
2121
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
22+
OPT_STRING('o', NULL, &string, "str", "get another string"),
2223
OPT_END(),
2324
};
2425
int i;

0 commit comments

Comments
 (0)