Skip to content

Commit

Permalink
feat: support option whose names ends with - and : (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Apr 9, 2024
1 parent 5842639 commit 4c876fc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ impl Param for FlagOptionParam {
output.push(short.to_string());
};
let mut name_suffix = String::new();
if self.prefixed {
if self.prefixed || self.data.name.ends_with('-') {
name_suffix.push('-');
}
if self.assigned {
if self.assigned || self.data.name.ends_with(':') {
name_suffix.push(':');
}
output.push(format!(
Expand Down Expand Up @@ -156,11 +156,15 @@ impl FlagOptionParam {
row_notations: &[&str],
) -> Self {
let (mut prefixed, mut assigned) = (false, false);
if let Some(new_name) = data.name.strip_suffix(':') {
if let Some(new_name) = data.name.strip_suffix("::") {
data.name = format!("{new_name}:");
} else if let Some(new_name) = data.name.strip_suffix(':') {
data.name = new_name.to_string();
assigned = true;
}
if let Some(new_name) = data.name.strip_suffix('-') {
if let Some(new_name) = data.name.strip_suffix("--") {
data.name = format!("{new_name}-");
} else if let Some(new_name) = data.name.strip_suffix('-') {
data.name = new_name.to_string();
prefixed = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ 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:");
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
13 changes: 13 additions & 0 deletions tests/snapshots/integration__spec__option_assigned.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ argc__positionals=( v1 )
argc__args=([0]="prog" [1]="--oc" [2]="v1")
argc__positionals=([0]="v1")
argc_oc=
************ RUN ************
prog --o: v1
# OUTPUT
argc_o_=v1
argc__args=( prog --o: v1 )
argc__positionals=( )
# RUN_OUTPUT
argc__args=([0]="prog" [1]="--o:" [2]="v1")
argc__positionals=()
argc_o_=v1
15 changes: 13 additions & 2 deletions tests/snapshots/integration__spec__option_prefixed.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source: tests/spec.rs
expression: data
---
RUN
************ RUN ************
prog -o1 -Dv1=foo -Dv2 bar

# OUTPUT
Expand All @@ -14,7 +14,18 @@ argc_D["v2"]=bar
argc__args=( prog -o1 '-Dv1=foo' -Dv2 bar )
argc__positionals=( )

# BUILD_OUTPUT
# RUN_OUTPUT
error: unexpected argument `-o1` found

************ RUN ************
prog -v-

# OUTPUT
argc_v_=1
argc__args=( prog -v- )
argc__positionals=( )

# RUN_OUTPUT
argc__args=([0]="prog" [1]="-v-")
argc__positionals=()
argc_v_=1
11 changes: 10 additions & 1 deletion tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,15 @@ fn option_prefixed() {
let script = r###"
# @option -o-
# @option -D-*
# @flag -v--
"###;
snapshot!(script, &["prog", "-o1", "-Dv1=foo", "-Dv2", "bar"]);
snapshot_multi!(
script,
[
vec!["prog", "-o1", "-Dv1=foo", "-Dv2", "bar"],
vec!["prog", "-v-"]
]
);
}

#[test]
Expand All @@ -234,13 +241,15 @@ fn option_assigned() {
# @option --oa:
# @option --ob:*
# @option --oc: <VALUE?>
# @option --o::
"###;
snapshot_multi!(
script,
[
vec!["prog", "--oa=v1", "--ob=1", "--ob=2"],
vec!["prog", "--oa", "v1"],
vec!["prog", "--oc", "v1"],
vec!["prog", "--o:", "v1"],
]
);
}
Expand Down

0 comments on commit 4c876fc

Please sign in to comment.