Skip to content

Commit

Permalink
refactor: options can accpet last single dash value (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jul 8, 2024
1 parent b4996ff commit f37c746
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -1239,6 +1244,7 @@ fn match_flag_option<'a, 'b>(
split_last_arg_at: &mut Option<usize>,
combine_shorts: bool,
signs: &str,
compgen: bool,
) {
let arg = &args[*arg_index];
if param.terminated() {
Expand All @@ -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 };
Expand All @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions tests/snapshots/integration__spec__option_single_dash.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ expression: data
prog --oa -

# OUTPUT
command cat >&2 <<-'EOF'
error: incorrect number of values for `--oa <OA>`
EOF
exit 1
argc_oa=-
argc__args=( prog --oa - )
argc__positionals=( )

# RUN_OUTPUT
error: incorrect number of values for `--oa <OA>`
argc__args=([0]="prog" [1]="--oa" [2]="-")
argc__positionals=()
argc_oa=-

************ RUN ************
prog --oa -
Expand Down

0 comments on commit f37c746

Please sign in to comment.