From 5f05c8c3b7c1a8b54cb0779c4a8fed039a9df558 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 8 Aug 2023 09:28:51 +0800 Subject: [PATCH] refactor: use tailing '-' to mark prefixed option (#226) --- src/param.rs | 2 +- src/parser.rs | 15 +++++++++------ tests/compgen.rs | 4 ++-- tests/spec.rs | 2 +- 4 files changed, 13 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..d87769e3 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 let Some(name) = arg.name.strip_suffix('-') { + arg.name = name.to_string(); + arg.modifer = Modifier::Prefixed; + } + arg + }), ))(input) } @@ -806,7 +808,8 @@ 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-"); + 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"]); }