From e20c08d04e0dc7bc088bb20b651d6779404e015e Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 6 May 2019 17:24:42 +0200 Subject: [PATCH] argparse: Fix validation for short-only-flags This read something like `o=!_validate_int`, and the flag modifier reading kept the pointer after the `!`, so it created a long flag called `_validate_int`, which meant it would not only error out form ```fish argparse 'i=!_validate_int' 'o=!_validate_int' -- $argv ``` with "Long flag '_validate_int' already defined", but also set $_flag_validate_int. Fixes #5864. --- src/builtin_argparse.cpp | 2 ++ tests/argparse.err | 5 +++++ tests/argparse.in | 9 +++++++++ tests/argparse.out | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 1b75a40a4..b6c4405c6 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -181,6 +181,8 @@ static bool parse_flag_modifiers(const argparse_cmd_opts_t &opts, const option_s if (*s == L'!') { s++; opt_spec->validation_command = wcstring(s); + // Move cursor to the end so we don't expect a long flag. + while (*s) s++; } else if (*s) { streams.err.append_format(BUILTIN_ERR_INVALID_OPT_SPEC, opts.name.c_str(), option_spec.c_str(), *s); diff --git a/tests/argparse.err b/tests/argparse.err index 417c205a0..291e53221 100644 --- a/tests/argparse.err +++ b/tests/argparse.err @@ -121,3 +121,8 @@ in function 'notargparse' #################### # Checking arguments after -- + +#################### +# Checking validation for short flags only +argparse: Value 'banana' for flag 'o' is not an integer +argparse: Value '-o' for flag 'i' is not an integer diff --git a/tests/argparse.in b/tests/argparse.in index 01d9aba3b..444501d7c 100644 --- a/tests/argparse.in +++ b/tests/argparse.in @@ -195,3 +195,12 @@ begin argparse a/alpha -- a --alpha -- b -a printf '%s\n' $argv end + +# #5864 - short flag only with same validation function. +logmsg Checking validation for short flags only +argparse 'i=!_validate_int' 'o=!_validate_int' -- -i 2 -o banana +argparse 'i=!_validate_int' 'o=!_validate_int' -- -i -o banana +begin + argparse 'i=!_validate_int' 'o=!_validate_int' -- -i 2 -o 3 + set -l +end diff --git a/tests/argparse.out b/tests/argparse.out index c70257994..130ae04aa 100644 --- a/tests/argparse.out +++ b/tests/argparse.out @@ -146,3 +146,15 @@ alpha aaaa a b -a + +#################### +# Checking validation for short flags only +_flag_a 'alpha' 'aaaa' +_flag_b -b +_flag_break -b +_flag_i 2 +_flag_m 1 +_flag_max 1 +_flag_o 3 +argv +saved_status 57