From d2e3c4e107ef3bcb4584da71ceb252d3f5694b21 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 5 Sep 2023 08:11:15 +0800 Subject: [PATCH] feat: export argc__last_flag_option --- src/matcher.rs | 15 +++++++++++++++ ...integration__compgen__assing_option_value.snap | 3 ++- tests/snapshots/integration__param_fn__case1.snap | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/matcher.rs b/src/matcher.rs index accb45f9..049a85b5 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -28,6 +28,7 @@ pub(crate) struct Matcher<'a, 'b> { script_path: Option, term_width: Option, split_last_arg_at: Option, + last_flag_option: Option<&'a str>, } type FlagOptionArg<'a, 'b> = (&'b str, Vec<&'b str>, Option<&'a str>); @@ -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(); @@ -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( @@ -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( @@ -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)); } @@ -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( @@ -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, @@ -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)); } @@ -241,6 +249,7 @@ impl<'a, 'b> Matcher<'a, 'b> { script_path: None, term_width: None, split_last_arg_at, + last_flag_option, } } @@ -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 } diff --git a/tests/snapshots/integration__compgen__assing_option_value.snap b/tests/snapshots/integration__compgen__assing_option_value.snap index 0f0f592c..47d24844 100644 --- a/tests/snapshots/integration__compgen__assing_option_value.snap +++ b/tests/snapshots/integration__compgen__assing_option_value.snap @@ -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 diff --git a/tests/snapshots/integration__param_fn__case1.snap b/tests/snapshots/integration__param_fn__case1.snap index e44cfe6e..5dd417fb 100644 --- a/tests/snapshots/integration__param_fn__case1.snap +++ b/tests/snapshots/integration__param_fn__case1.snap @@ -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; @@ -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;