From 7a7f318c5ba43d7c5f92e8746b330c698223b5a4 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 8 Aug 2023 09:10:41 +0800 Subject: [PATCH 1/2] refactor: use tailing '-' to mark prefixed option --- src/param.rs | 2 +- src/parser.rs | 14 ++++++++------ tests/compgen.rs | 4 ++-- tests/spec.rs | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/param.rs b/src/param.rs index 2a3f8aee..c6caf645 100644 --- a/src/param.rs +++ b/src/param.rs @@ -574,7 +574,7 @@ impl Modifier { Self::MultiCharOptional(c) => format!("*{c}"), Self::MultiCharRequired(c) => format!("+{c}"), Self::Terminated => "~".to_string(), - Self::Prefixed => "%".to_string(), + Self::Prefixed => "-".to_string(), } } } diff --git a/src/parser.rs b/src/parser.rs index 2296beb7..a13dbe91 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -345,10 +345,6 @@ fn parse_param_modifer(input: &str) -> nom::IResult<&str, ParamData> { arg.modifer = Modifier::Terminated; arg }), - map(terminated(parse_param_name, tag("%")), |mut arg| { - arg.modifer = Modifier::Prefixed; - arg - }), map( pair(parse_param_name, preceded(tag("*"), opt(parse_multi_char))), |(mut arg, multi_char)| { @@ -371,7 +367,13 @@ fn parse_param_modifer(input: &str) -> nom::IResult<&str, ParamData> { arg }, ), - parse_param_name, + map(parse_param_name, |mut arg| { + if arg.name.ends_with("-") { + arg.name = arg.name.trim_end_matches("-").to_string(); + arg.modifer = Modifier::Prefixed; + } + arg + }), ))(input) } @@ -806,7 +808,7 @@ mod tests { assert_parse_option_arg!("-f![a|b]"); assert_parse_option_arg!("-f![`_foo`]"); assert_parse_option_arg!("-f![=a|b]"); - assert_parse_option_arg!("-D%"); + assert_parse_option_arg!("-D-"); } #[test] diff --git a/tests/compgen.rs b/tests/compgen.rs index ead7ea6e..c2d7ac71 100644 --- a/tests/compgen.rs +++ b/tests/compgen.rs @@ -567,8 +567,8 @@ _choice_fn() { #[test] fn option_prefixed() { let script = r###" -# @option -D%[`_choice_fn`] -# @option -X --ox%[`_choice_fn`] +# @option -D-[`_choice_fn`] +# @option -X --ox-[`_choice_fn`] _choice_fn() { echo VAR1=value1 echo VAR2=value2 diff --git a/tests/spec.rs b/tests/spec.rs index abc32c40..c5eb6fd8 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -199,7 +199,7 @@ fn option_terminated() { #[test] fn option_prefixed() { let script = r###" -# @option -D% +# @option -D- "###; snapshot!(script, &["prog", "-D", "v1", "-Dv2=foo"]); } From bf41b12f6c974842acea1cb6236d65f8ed4969da Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 8 Aug 2023 09:14:35 +0800 Subject: [PATCH 2/2] optimize --- src/parser.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index a13dbe91..d87769e3 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -368,8 +368,8 @@ fn parse_param_modifer(input: &str) -> nom::IResult<&str, ParamData> { }, ), map(parse_param_name, |mut arg| { - if arg.name.ends_with("-") { - arg.name = arg.name.trim_end_matches("-").to_string(); + if let Some(name) = arg.name.strip_suffix('-') { + arg.name = name.to_string(); arg.modifer = Modifier::Prefixed; } arg @@ -809,6 +809,7 @@ mod tests { assert_parse_option_arg!("-f![`_foo`]"); assert_parse_option_arg!("-f![=a|b]"); assert_parse_option_arg!("-D-"); + assert_parse_option_arg!("-D--"); } #[test]