diff --git a/README.md b/README.md index 8a13c83b..be7e69f1 100644 --- a/README.md +++ b/README.md @@ -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 A option with short alias and notation ## @option --foo![a|b] A required option with choices + ## @option -f --foo A option with short alias and notation ``` Define value option @@ -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 @@ -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 diff --git a/src/parser.rs b/src/parser.rs index b27fe984..d8239fa8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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, @@ -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]"); diff --git a/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default.snap b/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default.snap new file mode 100644 index 00000000..7ddc6735 --- /dev/null +++ b/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default.snap @@ -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: + A arg with default value [default: a] + +OPTIONS: + -h, --help Print help information + + diff --git a/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default_exec.snap b/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default_exec.snap new file mode 100644 index 00000000..a2e1386e --- /dev/null +++ b/tests/snapshots/integration__spec_test__spec_cmd_positional_with_default_exec.snap @@ -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 + + diff --git a/tests/snapshots/integration__spec_test__spec_help.snap b/tests/snapshots/integration__spec_test__spec_help.snap index 65991ef8..5c696f3e 100644 --- a/tests/snapshots/integration__spec_test__spec_help.snap +++ b/tests/snapshots/integration__spec_test__spec_help.snap @@ -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 diff --git a/tests/spec.sh b/tests/spec.sh index bf197e53..455e79c9 100644 --- a/tests/spec.sh +++ b/tests/spec.sh @@ -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 } @@ -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() { diff --git a/tests/spec_test.rs b/tests/spec_test.rs index accac3f7..f664103f 100644 --- a/tests/spec_test.rs +++ b/tests/spec_test.rs @@ -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!(