Skip to content

Commit

Permalink
fix: options can accept single dash value (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jul 8, 2024
1 parent 355a3e1 commit b4996ff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 5 additions & 4 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
28 changes: 28 additions & 0 deletions tests/snapshots/integration__spec__option_single_dash.snap
Original file line number Diff line number Diff line change
@@ -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 <OA>`
EOF
exit 1
# RUN_OUTPUT
error: incorrect number of values for `--oa <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=-
11 changes: 11 additions & 0 deletions tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "-", ""],]
);
}

0 comments on commit b4996ff

Please sign in to comment.