Skip to content

Commit

Permalink
fix: dashes parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Aug 25, 2023
1 parent 8e4efe4 commit 3ba6f59
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) struct Matcher<'a, 'b> {
args: &'b [String],
flag_option_args: Vec<Vec<FlagOptionArg<'a, 'b>>>,
positional_args: Vec<&'b str>,
dashes: Vec<usize>,
dashes: Option<usize>,
arg_comp: ArgComp,
choice_fns: HashSet<&'a str>,
script_path: Option<String>,
Expand Down Expand Up @@ -69,15 +69,15 @@ impl<'a, 'b> Matcher<'a, 'b> {
let mut arg_index = 1;
let mut flag_option_args = vec![vec![]];
let mut positional_args = vec![];
let mut dashes = vec![];
let mut dashes = None;
let mut split_last_arg_at = None;
let mut arg_comp = ArgComp::Any;
let mut choice_fns = HashSet::new();
let args_len = args.len();
if root.delegated() {
positional_args = args.iter().skip(1).map(|v| v.as_str()).collect();
} else {
let mut is_rest_args_positional = false; // arg(like -f --foo) will be positional arg
let mut is_rest_args_positional = false; // option(e.g. -f --foo) will be treated as positional arg
if let Some(arg) = args.last() {
if arg.starts_with('-') {
arg_comp = ArgComp::FlagOrOption;
Expand All @@ -89,7 +89,6 @@ impl<'a, 'b> Matcher<'a, 'b> {
let cmd = cmds[cmd_level].1;
let arg = args[arg_index].as_str();
if arg == "--" {
dashes.push(positional_args.len());
if is_rest_args_positional {
add_positional_arg(
&mut positional_args,
Expand All @@ -98,8 +97,13 @@ impl<'a, 'b> Matcher<'a, 'b> {
cmd,
);
}
if dashes.is_none() {
dashes = Some(positional_args.len());
if arg_index != args_len - 1 {
is_rest_args_positional = true
}
}
} else if is_rest_args_positional
|| !dashes.is_empty()
|| (cmd.no_flags_options_subcommands() && !KNOWN_OPTIONS.contains(&arg))
{
add_positional_arg(
Expand Down Expand Up @@ -267,11 +271,8 @@ impl<'a, 'b> Matcher<'a, 'b> {

pub(crate) fn to_arg_values_for_choice_fn(&self) -> Vec<ArgcValue> {
let mut output: Vec<ArgcValue> = self.to_arg_values_base();
if !self.dashes.is_empty() {
output.push(ArgcValue::Multiple(
"_dashes".into(),
self.dashes.iter().map(|v| v.to_string()).collect(),
));
if let Some(idx) = self.dashes {
output.push(ArgcValue::Single("_dashes".into(), idx.to_string()));
}
let last_cmd = self.cmds[self.cmds.len() - 1].1;
if let Some(name) = &last_cmd.name {
Expand Down Expand Up @@ -627,13 +628,13 @@ impl<'a, 'b> Matcher<'a, 'b> {
while param_index < params_len && arg_index < args_len {
let param = &cmd.positional_params[param_index];
if param.multiple() {
let dash_idx = self.dashes.first().cloned().unwrap_or_default();
let dashes_at = self.dashes.unwrap_or_default();
let takes = if param_index == 0
&& dash_idx > 0
&& dashes_at > 0
&& params_len == 2
&& cmd.positional_params[1].multiple()
{
dash_idx
dashes_at
} else {
(args_len - arg_index).saturating_sub(params_len - param_index) + 1
};
Expand Down
25 changes: 24 additions & 1 deletion tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ cmda() { :; }
snapshot_compgen!(SCRIPT, vec![vec!["prog", ""],]);
}

#[test]
fn dashes_at() {
let script = r###"
# @arg val*[`_choice_fn`]
_choice_fn() {
if [[ -z "$argc__dashes" ]]; then
echo -e "abc\ndef\nghi"
else
echo -e "v1\nv2"
fi
}
"###;

snapshot_compgen!(
script,
vec![vec!["prog", "abc", ""], vec!["prog", "abc", "--", ""],]
);
}

#[test]
fn no_space() {
let script = r###"
Expand Down Expand Up @@ -520,7 +539,11 @@ _choice_fn() {

snapshot_compgen!(
script,
vec![vec!["prog", "--oa=abc"], vec!["prog", "oa=abc"],]
vec![
vec!["prog", "--oa=abc"],
vec!["prog", "oa=abc"],
vec!["prog", "--", "--oa=abc"]
]
);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/snapshots/integration__compgen__assing_option_value.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ argc__index=0 /color:green
argc__positionals=([0]="oa=abc") /color:green
argc_val=oa=abc /color:green

************ COMPGEN `prog -- --oa=abc` ************
__argc_filter= /color:green
ARGC_COMPGEN=1 /color:green
ARGC_FILTER=--oa=abc /color:green
ARGC_LAST_ARG=--oa=abc /color:green
ARGC_VARS=YXJnY192YWw9Jy0tb2E9YWJjJzthcmdjX19hcmdzPSggcHJvZyAtLSAnLS1vYT1hYmMnICk7YXJnY19faW5kZXg9MDthcmdjX19kYXNoZXM9MDthcmdjX19wb3NpdGlvbmFscz0oICctLW9hPWFiYycgKTs= /color:green
argc__args=([0]="prog" [1]="--" [2]="--oa=abc") /color:green
argc__dashes=0 /color:green
argc__index=0 /color:green
argc__positionals=([0]="--oa=abc") /color:green
argc_val=--oa=abc /color:green


14 changes: 14 additions & 0 deletions tests/snapshots/integration__compgen__dashes_at.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog abc ` ************
abc /color:green
def /color:green
ghi /color:green

************ COMPGEN `prog abc -- ` ************
v1 /color:green
v2 /color:green


0 comments on commit 3ba6f59

Please sign in to comment.