Skip to content

Commit

Permalink
refactor: use tailing '-' to mark prefixed option (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Aug 8, 2023
1 parent 291f69d commit 5f05c8c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)| {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
Expand Down

0 comments on commit 5f05c8c

Please sign in to comment.