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

refactor: prefixed option supports multi/non-multi #240

Merged
merged 1 commit into from
Aug 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ impl FlagOptionParam {
}

pub(crate) fn prefixed(&self) -> Option<String> {
if self.data.modifer != Modifier::Prefixed {
if !matches!(
self.data.modifer,
Modifier::Prefixed | Modifier::MultiPrefixed
) {
return None;
}

Expand Down Expand Up @@ -570,6 +573,7 @@ pub(crate) enum Modifier {
MultiCharRequired(char),
Terminated,
Prefixed,
MultiPrefixed,
}

impl Modifier {
Expand All @@ -582,7 +586,8 @@ impl Modifier {
Self::MultiCharOptional(_) => true,
Self::MultiCharRequired(_) => true,
Self::Terminated => true,
Self::Prefixed => true,
Self::Prefixed => false,
Self::MultiPrefixed => true,
}
}

Expand All @@ -596,6 +601,7 @@ impl Modifier {
Self::MultiCharRequired(_) => true,
Self::Terminated => false,
Self::Prefixed => false,
Self::MultiPrefixed => false,
}
}

Expand All @@ -609,6 +615,7 @@ impl Modifier {
Self::MultiCharRequired(c) => format!("+{c}"),
Self::Terminated => "~".to_string(),
Self::Prefixed => "-".to_string(),
Self::MultiPrefixed => "-*".to_string(),
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,17 @@ fn parse_param_modifer(input: &str) -> nom::IResult<&str, ParamData> {
map(
pair(parse_param_name, preceded(tag("*"), opt(parse_multi_char))),
|(mut arg, multi_char)| {
let modifier = match multi_char {
Some(c) => Modifier::MultiCharOptional(c),
match multi_char {
Some(c) => arg.modifer = Modifier::MultiCharOptional(c),
None => {
if let Some(name) = arg.name.strip_suffix('-') {
arg.name = name.to_string();
Modifier::Prefixed
arg.modifer = Modifier::MultiPrefixed
} else {
Modifier::MultipleOptional
arg.modifer = Modifier::MultipleOptional
}
}
};
arg.modifer = modifier;
arg
},
),
Expand All @@ -374,7 +373,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 @@ -740,6 +745,8 @@ mod tests {
assert_parse_option_arg!("--foo+,");
assert_parse_option_arg!("--foo*,");
assert_parse_option_arg!("--foo!");
assert_parse_option_arg!("--foo-*");
assert_parse_option_arg!("--foo-");
assert_parse_option_arg!("--foo=a");
assert_parse_option_arg!("--foo=`_foo`");
assert_parse_option_arg!("--foo[a|b]");
Expand Down Expand Up @@ -774,6 +781,8 @@ mod tests {
assert_parse_option_arg!("-foo+");
assert_parse_option_arg!("-foo*");
assert_parse_option_arg!("-foo!");
assert_parse_option_arg!("-foo-*");
assert_parse_option_arg!("-foo-");
assert_parse_option_arg!("-foo=a");
assert_parse_option_arg!("-foo=`_foo`");
assert_parse_option_arg!("-foo[a|b]");
Expand Down
4 changes: 4 additions & 0 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ fn option_prefixed() {
let script = r###"
# @option -D-*[`_choice_fn`]
# @option -X --ox-*[`_choice_fn`]
# @option -s-[`_choice_fn`]
_choice_fn() {
echo VAR1=value1
echo VAR2=value2
Expand All @@ -610,8 +611,11 @@ _choice_fn() {
vec!["prog", "-DVAR1"],
vec!["prog", "-X"],
vec!["prog", "-XVAR1"],
vec!["prog", "-XVAR3", "-"],
vec!["prog", "--ox"],
vec!["prog", "--ox", "VAR1"],
vec!["prog", "-s"],
vec!["prog", "-sVAR3", "-"],
]
);
}
Expand Down
Binary file modified tests/snapshots/integration__compgen__option_prefixed.snap
Binary file not shown.
5 changes: 3 additions & 2 deletions tests/snapshots/integration__spec__option_prefixed.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ source: tests/spec.rs
expression: data
---
RUN
prog -D v1 -Dv2=foo
prog -D v1 -Dv2=foo -o1

OUTPUT
argc_o=1
argc_D=( v1 'v2=foo' )
argc__args=( prog -D v1 '-Dv2=foo' )
argc__args=( prog -D v1 '-Dv2=foo' -o1 )
argc__cmd_arg_index=0
argc__positionals=( )

3 changes: 2 additions & 1 deletion tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ fn option_terminated() {
#[test]
fn option_prefixed() {
let script = r###"
# @option -o-
# @option -D-*
"###;
snapshot!(script, &["prog", "-D", "v1", "-Dv2=foo"]);
snapshot!(script, &["prog", "-D", "v1", "-Dv2=foo", "-o1"]);
}

#[test]
Expand Down