Skip to content

Commit

Permalink
feat: positional parameter with default (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Mar 7, 2022
1 parent 51e8834 commit 20844a8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ Define alias for a subcommand
## @option --foo! A required option
## @option --foo* A option with multiple values
## @option --foo+ A required option with multiple values
## @option --foo=a A option with default
## @option --foo[a|b] A option with choices
## @option --foo[=a|b] A option with choices and default value
## @option -f --foo <PATH> A option with short alias and notation
## @option --foo![a|b] A required option with choices
## @option -f --foo <PATH> A option with short alias and notation
```

Define value option
Expand Down Expand Up @@ -160,6 +161,7 @@ Define flag option
# @arg value! A required positional argument
# @arg value* A positional argument support multiple values
# @arg value+ A required positional argument support multiple values
# @arg value=a A positional argument with default value
# @arg value[a|b] A positional argument with choices
# @arg value[=a|b] A positional argument with choices and default value
# @arg value![a|b] A required positional argument with choices
Expand All @@ -168,11 +170,7 @@ Define positional argument

#### modifier

- `*`: occur multiple times, optional
- `+`: occur multiple times, required
- `!`: required
- `[a|b|c]`: choices
- `![a|b|c]`: choices, required
See [option's modifier](#modifier)

## License

Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ fn parse_positional_arg(input: &str) -> nom::IResult<&str, ArgData> {
parse_arg_choices_default,
parse_arg_choices_required,
parse_arg_choices,
parse_arg_assign,
parse_arg_mark,
)),
parse_tail,
Expand Down Expand Up @@ -627,6 +628,7 @@ mod tests {
assert_parse_positional_arg!("foo!");
assert_parse_positional_arg!("foo+");
assert_parse_positional_arg!("foo*");
assert_parse_positional_arg!("foo=a");
assert_parse_positional_arg!("foo[a|b]");
assert_parse_positional_arg!("foo[=a|b]");
assert_parse_positional_arg!("foo![a|b]");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: tests/spec_test.rs
assertion_line: 115
expression: output

---
RUN
spec cmd-positional-with-default -h

STDOUT


STDERR
spec-cmd-positional-with-default
Positional with default value

USAGE:
spec cmd-positional-with-default [ARG]

ARGS:
<ARG> A arg with default value [default: a]

OPTIONS:
-h, --help Print help information


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: tests/spec_test.rs
assertion_line: 123
expression: output

---
RUN
spec cmd-positional-with-default

STDOUT
argc_arg=a
argc__call=cmd_positional_with_default

STDERR


2 changes: 2 additions & 0 deletions tests/snapshots/integration__spec_test__spec_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ SUBCOMMANDS:
Positional with choices and value
cmd-positional-with-choices-and-required
Positional with choices and required
cmd-positional-with-default
Positional with default value
cmd-preferred
Preferred
cmd-without-any-arg
Expand Down
12 changes: 9 additions & 3 deletions tests/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ cmd_flag_formats() {
}

# @cmd Positional one required
# @arg arg1! A required arg
# @arg arg1! A required arg
cmd_positional_only() {
print_argc_vars
}

# @cmd Positional all required
# @arg arg1! A required arg
# @arg arg2+ A required arg, multiple
# @arg arg1! A required arg
# @arg arg2+ A required arg, multiple
cmd_positional_requires() {
print_argc_vars
}
Expand All @@ -80,6 +80,12 @@ cmd_positional_with_choices() {
print_argc_vars
}

# @cmd Positional with default value
# @arg arg=a A arg with default value
cmd_positional_with_default() {
print_argc_vars
}

# @cmd Positional with choices and value
# @arg arg[=a|b] A arg with choices and default value
cmd_positional_with_choices_and_default() {
Expand Down
16 changes: 16 additions & 0 deletions tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ fn test_spec_cmd_option_names_exec_eval() {
);
}

#[test]
fn test_spec_cmd_positional_with_default() {
snapshot!(
include_str!("spec.sh"),
&["spec", "cmd-positional-with-default", "-h"],
);
}

#[test]
fn test_spec_cmd_positional_with_default_exec() {
snapshot!(
include_str!("spec.sh"),
&["spec", "cmd-positional-with-default"],
);
}

#[test]
fn test_spec_cmd_positional_with_choices() {
snapshot!(
Expand Down

0 comments on commit 20844a8

Please sign in to comment.