From f37c746379ad4c0c38aca6eba207a7ca1054fe5f Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 8 Jul 2024 18:10:05 +0800 Subject: [PATCH] refactor: options can accpet last single dash value (#342) --- src/build.rs | 2 +- src/matcher.rs | 19 ++++++++++++++++--- ...integration__spec__option_single_dash.snap | 11 ++++++----- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/build.rs b/src/build.rs index 94390bf..1eab594 100644 --- a/src/build.rs +++ b/src/build.rs @@ -24,7 +24,7 @@ _argc_take_args() { while [[ $_argc_take_index -lt $_argc_len ]]; do _argc_take_value="${argc__args[_argc_take_index]}" if [[ -n "$signs" ]] && [[ "$_argc_take_value" =~ ^["$signs"] ]]; then - if [[ "${#_argc_take_value}" -gt 1 ]] || [[ "$_argc_take_index" == $(($_argc_len - 1)) ]]; then + if [[ "${#_argc_take_value}" -gt 1 ]]; then break fi fi diff --git a/src/matcher.rs b/src/matcher.rs index 5746cd1..f30c72a 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -184,6 +184,7 @@ impl<'a, 'b, T: Runtime> Matcher<'a, 'b, T> { &mut split_last_arg_at, combine_shorts, &signs, + compgen, ); comp_option = Some(param.id()); } else if let Some(subcmd) = find_subcommand(cmd, arg, &positional_args) @@ -240,6 +241,7 @@ impl<'a, 'b, T: Runtime> Matcher<'a, 'b, T> { &mut split_last_arg_at, combine_shorts, &signs, + compgen, ); comp_option = Some(param.id()); } @@ -1181,6 +1183,7 @@ fn take_value_args<'a>( len: usize, signs: &str, assigned: bool, + compgen: bool, ) -> Vec<&'a str> { let mut output = vec![]; if assigned || len == 0 { @@ -1189,7 +1192,9 @@ fn take_value_args<'a>( let args_len = args.len(); let end = (start + len).min(args_len); for (i, arg) in args.iter().enumerate().take(end).skip(start) { - if arg.starts_with(|c| signs.contains(c)) && (arg.len() > 1 || i == args_len - 1) { + if arg.starts_with(|c| signs.contains(c)) + && (arg.len() > 1 || (compgen && i == args_len - 1)) + { break; } output.push(arg.as_str()); @@ -1239,6 +1244,7 @@ fn match_flag_option<'a, 'b>( split_last_arg_at: &mut Option, combine_shorts: bool, signs: &str, + compgen: bool, ) { let arg = &args[*arg_index]; if param.terminated() { @@ -1252,7 +1258,7 @@ fn match_flag_option<'a, 'b>( } else if let Some(prefix) = param.match_prefix(arg) { let args_len = args.len(); let prefix_len = prefix.len(); - let value_args = take_value_args(args, *arg_index + 1, 1, signs, param.assigned()); + let value_args = take_value_args(args, *arg_index + 1, 1, signs, param.assigned(), compgen); let take_empty = value_args.is_empty(); *arg_index += value_args.len(); let values = if take_empty { vec!["1"] } else { value_args }; @@ -1266,7 +1272,14 @@ fn match_flag_option<'a, 'b>( } else { let values_max = param.num_args().1; let args_len = args.len(); - let value_args = take_value_args(args, *arg_index + 1, values_max, signs, param.assigned()); + let value_args = take_value_args( + args, + *arg_index + 1, + values_max, + signs, + param.assigned(), + compgen, + ); *arg_index += value_args.len(); if *arg_index == args_len - 1 { if *arg_comp != ArgComp::FlagOrOption { diff --git a/tests/snapshots/integration__spec__option_single_dash.snap b/tests/snapshots/integration__spec__option_single_dash.snap index 29e9c92..a4a7433 100644 --- a/tests/snapshots/integration__spec__option_single_dash.snap +++ b/tests/snapshots/integration__spec__option_single_dash.snap @@ -6,13 +6,14 @@ expression: data prog --oa - # OUTPUT -command cat >&2 <<-'EOF' -error: incorrect number of values for `--oa ` -EOF -exit 1 +argc_oa=- +argc__args=( prog --oa - ) +argc__positionals=( ) # RUN_OUTPUT -error: incorrect number of values for `--oa ` +argc__args=([0]="prog" [1]="--oa" [2]="-") +argc__positionals=() +argc_oa=- ************ RUN ************ prog --oa -