Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export argc__last_flag_option #244

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct Matcher<'a, 'b> {
script_path: Option<String>,
term_width: Option<usize>,
split_last_arg_at: Option<usize>,
last_flag_option: Option<&'a str>,
}

type FlagOptionArg<'a, 'b> = (&'b str, Vec<&'b str>, Option<&'a str>);
Expand Down Expand Up @@ -73,6 +74,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
let mut split_last_arg_at = None;
let mut arg_comp = ArgComp::Any;
let mut choice_fns = HashSet::new();
let mut last_flag_option = None;
let args_len = args.len();
if root.delegated() {
positional_args = args.iter().skip(1).map(|v| v.as_str()).collect();
Expand All @@ -88,6 +90,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
while arg_index < args_len {
let cmd = cmds[cmd_level].1;
let arg = args[arg_index].as_str();
last_flag_option = None;
if arg == "--" {
if is_rest_args_positional {
add_positional_arg(
Expand Down Expand Up @@ -121,6 +124,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
split_last_arg_at = Some(k.len() + 1);
}
flag_option_args[cmd_level].push((k, vec![v], Some(param.name())));
last_flag_option = Some(param.name());
} else if let Some((param, prefix)) = cmd.find_prefixed_option(arg) {
add_param_choice_fn(&mut choice_fns, param);
match_prefix_option(
Expand All @@ -132,6 +136,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
&mut split_last_arg_at,
&prefix,
);
last_flag_option = Some(param.name());
} else {
flag_option_args[cmd_level].push((k, vec![v], None));
}
Expand All @@ -145,6 +150,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
&mut arg_comp,
&mut split_last_arg_at,
);
last_flag_option = Some(param.name());
} else if let Some((param, prefix)) = cmd.find_prefixed_option(arg) {
add_param_choice_fn(&mut choice_fns, param);
match_prefix_option(
Expand All @@ -156,6 +162,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
&mut split_last_arg_at,
&prefix,
);
last_flag_option = Some(param.name());
} else if let Some(subcmd) = find_subcommand(cmd, arg, &positional_args) {
match_command(
&mut cmds,
Expand Down Expand Up @@ -193,6 +200,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
&mut arg_comp,
&mut split_last_arg_at,
);
last_flag_option = Some(param.name());
} else {
flag_option_args[cmd_level].push((arg, vec![], None));
}
Expand Down Expand Up @@ -241,6 +249,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
script_path: None,
term_width: None,
split_last_arg_at,
last_flag_option,
}
}

Expand Down Expand Up @@ -278,6 +287,12 @@ impl<'a, 'b> Matcher<'a, 'b> {
if let Some(name) = &last_cmd.name {
output.push(ArgcValue::Single("_cmd_fn".into(), name.to_string()));
}
if let Some(name) = self.last_flag_option {
output.push(ArgcValue::Single(
"_last_flag_option".into(),
name.to_string(),
));
}
output
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ __argc_filter= /color:default
ARGC_COMPGEN=1 /color:default
ARGC_CWORD=abc /color:default
ARGC_LAST_ARG=--oa=abc /color:default
ARGC_VARS=YXJnY19vYT1hYmM7YXJnY19fYXJncz0oIHByb2cgJy0tb2E9YWJjJyApO2FyZ2NfX2NtZF9hcmdfaW5kZXg9MDthcmdjX19wb3NpdGlvbmFscz0oICApOw== /color:default
ARGC_VARS=YXJnY19vYT1hYmM7YXJnY19fYXJncz0oIHByb2cgJy0tb2E9YWJjJyApO2FyZ2NfX2NtZF9hcmdfaW5kZXg9MDthcmdjX19sYXN0X2ZsYWdfb3B0aW9uPW9hO2FyZ2NfX3Bvc2l0aW9uYWxzPSggICk7 /color:default
argc__args=([0]="prog" [1]="--oa=abc") /color:default
argc__cmd_arg_index=0 /color:default
argc__last_flag_option=oa /color:default
argc__positionals=() /color:default
argc_oa=abc /color:default

Expand Down
2 changes: 2 additions & 0 deletions tests/snapshots/integration__param_fn__case1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ argc_cc=''
argc__args=( prog cmda --cc '' )
argc__cmd_arg_index=1
argc__cmd_fn=cmda
argc__last_flag_option=cc
argc__positionals=( )
_choice_fn;exit;

Expand All @@ -32,6 +33,7 @@ argc_cc=''
argc__args=( prog cmda -a --oa oa --cc '' )
argc__cmd_arg_index=1
argc__cmd_fn=cmda
argc__last_flag_option=cc
argc__positionals=( )
_choice_fn;exit;

Expand Down