Skip to content

Commit 289cb15

Browse files
rscharfegitster
authored andcommitted
parse-options: recognize abbreviated negated option with arg
Giving an argument to an option that doesn't take one causes Git to report that error specifically: $ git rm --dry-run=bogus error: option `dry-run' takes no value The same is true when the option is negated or abbreviated: $ git rm --no-dry-run=bogus error: option `no-dry-run' takes no value $ git rm --dry=bogus error: option `dry-run' takes no value Not so when doing both, though: $ git rm --no-dry=bogus error: unknown option `no-dry=bogus' usage: git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] (Rest of the usage message omitted.) Improve consistency and usefulness of the error message by recognizing abbreviated negated options even if they have a (most likely bogus) argument. With this patch we get: $ git rm --no-dry=bogus error: option `no-dry-run' takes no value Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit 289cb15

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

parse-options.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static enum parse_opt_result parse_long_opt(
391391
ambiguous_option = abbrev_option;
392392
ambiguous_flags = abbrev_flags;
393393
}
394-
if (!(flags & OPT_UNSET) && *arg_end)
394+
if (*arg_end)
395395
p->opt = arg_end + 1;
396396
abbrev_option = options;
397397
abbrev_flags = flags ^ opt_flags;
@@ -412,7 +412,8 @@ static enum parse_opt_result parse_long_opt(
412412
if (!skip_prefix(arg + 3, long_name, &rest)) {
413413
/* abbreviated and negated? */
414414
if (allow_abbrev &&
415-
starts_with(long_name, arg + 3))
415+
!strncmp(long_name, arg + 3,
416+
arg_end - arg - 3))
416417
goto is_abbreviated;
417418
else
418419
continue;

t/t0040-parse-options.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ test_expect_success 'superfluous value provided: boolean' '
210210
test_cmp expect actual
211211
'
212212

213+
test_expect_success 'superfluous value provided: boolean, abbreviated' '
214+
cat >expect <<-\EOF &&
215+
error: option `yes'\'' takes no value
216+
EOF
217+
test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
218+
test-tool parse-options --ye=hi 2>actual &&
219+
test_cmp expect actual &&
220+
221+
cat >expect <<-\EOF &&
222+
error: option `no-yes'\'' takes no value
223+
EOF
224+
test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
225+
test-tool parse-options --no-ye=hi 2>actual &&
226+
test_cmp expect actual
227+
'
228+
213229
test_expect_success 'superfluous value provided: cmdmode' '
214230
cat >expect <<-\EOF &&
215231
error: option `mode1'\'' takes no value

0 commit comments

Comments
 (0)