From b4996ff39c7defcece2f774ea7a9357ef92dfda4 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 8 Jul 2024 17:45:56 +0800 Subject: [PATCH] fix: options can accept single dash value (#341) --- src/build.rs | 4 ++- src/matcher.rs | 9 +++--- ...integration__spec__option_single_dash.snap | 28 +++++++++++++++++++ tests/spec.rs | 11 ++++++++ 4 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/snapshots/integration__spec__option_single_dash.snap diff --git a/src/build.rs b/src/build.rs index 1a01d22..94390bf 100644 --- a/src/build.rs +++ b/src/build.rs @@ -24,7 +24,9 @@ _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 - break + if [[ "${#_argc_take_value}" -gt 1 ]] || [[ "$_argc_take_index" == $(($_argc_len - 1)) ]]; then + break + fi fi _argc_take_args_values+=("$_argc_take_value") _argc_take_args_len=$((_argc_take_args_len + 1)) diff --git a/src/matcher.rs b/src/matcher.rs index cef6afa..5746cd1 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -137,7 +137,7 @@ impl<'a, 'b, T: Runtime> Matcher<'a, 'b, T> { &mut is_rest_args_positional, cmd, ); - } else if arg.starts_with(|c| signs.contains(c)) { + } else if arg.len() > 1 && arg.starts_with(|c| signs.contains(c)) { if let Some((k, v)) = arg.split_once('=') { if let Some(param) = cmd.find_flag_option(k) { add_param_choice_fn(&mut choice_fns, param); @@ -1186,9 +1186,10 @@ fn take_value_args<'a>( if assigned || len == 0 { return output; } - let end = (start + len).min(args.len()); - for arg in args.iter().take(end).skip(start) { - if arg.starts_with(|c| signs.contains(c)) { + 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) { break; } output.push(arg.as_str()); diff --git a/tests/snapshots/integration__spec__option_single_dash.snap b/tests/snapshots/integration__spec__option_single_dash.snap new file mode 100644 index 0000000..29e9c92 --- /dev/null +++ b/tests/snapshots/integration__spec__option_single_dash.snap @@ -0,0 +1,28 @@ +--- +source: tests/spec.rs +expression: data +--- +************ RUN ************ +prog --oa - + +# OUTPUT +command cat >&2 <<-'EOF' +error: incorrect number of values for `--oa ` +EOF +exit 1 + +# RUN_OUTPUT +error: incorrect number of values for `--oa ` + +************ RUN ************ +prog --oa - + +# OUTPUT +argc_oa=- +argc__args=( prog --oa - '' ) +argc__positionals=( '' ) + +# RUN_OUTPUT +argc__args=([0]="prog" [1]="--oa" [2]="-" [3]="") +argc__positionals=([0]="") +argc_oa=- diff --git a/tests/spec.rs b/tests/spec.rs index 67ac279..12fd336 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -406,3 +406,14 @@ cmd_a() { :; } ] ); } + +#[test] +fn option_single_dash() { + let script = r###" +# @option --oa +"###; + snapshot_multi!( + script, + [vec!["prog", "--oa", "-"], vec!["prog", "--oa", "-", ""],] + ); +}