Commit graph

2112 commits

Author SHA1 Message Date
Kevin K
0c223f54ed
fix: when an argument requires a value and that value happens to match a subcommand name, its parsed as a value
**This commit contains a breaking change in order to fix a bug.**

The only users affected are those relying on the "bug." The bug is only in code that uses
subcommands, and parent commands with arguments accepting multiple values (positionals or options)
unrestrained *and* where a value may coincide with a subcommand's name.

Imagine a CLI with a positional argument `files` that accepts multiple values but no other conditions
or parameters (just `Arg::multiple(true)`), and a subcommand `log`

Prior to this commit, the following was valid:

```
$ prog file1 file2 file3 log
```

which would be parsed as:

 * files = file1, file2, file3
 * subcommand = log

However, if there was a file named `log` the subcommand isn't callable.

The CLI should be designed in a way that either limits number of values so that clap knows
when `files` is done and can then look for subcommands, or other AppSettings which change
the behavior.

Possible fixes are `Arg::number_of_values`, `Arg::max_values`,
`AppSettings::ArgsNegateSubcommands`, `Arg::require_equals`,
`AppSettings::SubcommandsNegateArgs`, and more.

This *does not* affect CLIs which contain other arguments between the unrestrained multiple
value args, and the any subcommands. Such as if our example above also had a flag `-f`

The following would be parsed the same before and after this commit.

```
$ prog file1 file2 file3 -f log
```

This is because upon reaching the flag `-f`, clap stops parsing `files`.

No other breaking changes were made.

---

When using args with `multiple(true)` on options or positionals (i.e. those args that
accept values) and subcommands, one needs to consider the posibility of an argument value
being the same as a valid subcommand. By default `clap` will parse the argument in question
as a value *only if* a value is possible at that moment. Otherwise it will be parsed as a
subcommand. In effect, this means using `multiple(true)` with no additional parameters and
a possible value that coincides with a subcommand name, the subcommand cannot be called
unless another argument is passed first.

As an example, consider a CLI with an option `--ui-paths=<paths>...` and subcommand `signer`

The following would be parsed as values to `--ui-paths`.

```
$ program --ui-paths path1 path2 signer
```

This is because `--ui-paths` accepts multiple values. `clap` will continue parsing values
until another argument is reached and it knows `--ui-paths` is done.

By adding additional parameters to `--ui-paths` we can solve this issue. Consider adding
`Arg::number_of_values(1)` as discussed above. The following are all valid, and `signer`
is parsed as both a subcommand and a value in the second case.

```
$ program --ui-paths path1 signer
$ program --ui-paths path1 --ui-paths signer signer
```

Closes #1031
2017-10-24 14:59:23 -07:00
Kevin K
b399ee2604
tests: adds tests to ensure number_of_values and default_value can be used together 2017-10-24 12:57:04 -07:00
Kevin K
5eb342a99d
fix: fixes a bug that prevented number_of_values and default_values to be used together
Closes #1050
Closes #1056
2017-10-24 12:56:02 -07:00
Kevin K
f7a6955238
tests: adds tests to make sure args with default values can have conflicts 2017-10-24 12:23:29 -07:00
Kevin K
58b5b4be31
fix: fixes a bug that didn't allow args with default values to have conflicts
Closes #1071
2017-10-24 12:22:44 -07:00
Kevin K
9cb92c17c5 Merge pull request #1077 from kbknapp/issue-1076
Issue 1076
2017-10-24 14:59:18 -04:00
Kevin K
2c3f7f6054
tests: adds tests to protect against panics when using globals and calling App::get_matches_from_safe_borrow multiple times 2017-10-24 10:46:00 -04:00
Kevin K
d86ec79742
fix: fixes a panic when using global args and calling App::get_matches_from_safe_borrow multiple times
Closes #1076
2017-10-24 10:45:52 -04:00
Kevin K
6ac846d850 Merge pull request #1075 from kbknapp/issues-978,1010,1061
Issues 978,1010,1061
2017-10-24 08:01:30 -04:00
Kevin K
48e0529bcd
chore: clippy run 2017-10-23 23:03:40 -04:00
Kevin K
8fd59e0b7b
tests: updates tests new global values being propagated up and down 2017-10-23 22:14:16 -04:00
Kevin K
a43f9dd4aa
fix: fixes issues and potential regressions with global args values not being propagated properly or at all
Closes #1010
Closes #1061
Closes #978
2017-10-23 21:29:05 -04:00
Kevin K
ead076f03a
docs: updates the docs to reflect changes to global args and that global args values can now be propagated back up the stack 2017-10-23 21:28:26 -04:00
Kevin K
cbd09c7fec
tests: refactors the tests for propagating global values and flags as well as adds some tests for default values and flags 2017-10-23 21:27:29 -04:00
Kevin K
0d6cd6e7d9
chore: fixes some spelling mistakes 2017-10-23 21:26:45 -04:00
Kevin K
2bb5ddcee6
depr(AppSettings::PropagateGlobalValuesDown): this setting is no longer required to propagate values down or up 2017-10-23 21:25:57 -04:00
Kevin K
899f3d320d
refactor: removes unused imports and fixes spelling error 2017-10-23 20:13:50 -04:00
Kevin K
8a2e1dbdcd
wip: implement 1061 2017-10-21 15:55:31 -04:00
Kevin K
569ced1f3d Merge pull request #1070 from willmurphyscode/propagate-values-down
Fix 978: global args' values available to all subcommands
2017-10-18 10:40:01 -04:00
Will Murphy
ae060c399b more cleanup 2017-10-18 07:42:17 -04:00
Will Murphy
e85079fcff more cleanup 2017-10-18 07:42:17 -04:00
Will Murphy
9c983a5da4 remove accidentally tracked .fmt file 2017-10-18 07:42:17 -04:00
Will Murphy
6158fc9b50 Clean up commented code and old TODOs 2017-10-18 07:42:17 -04:00
Will Murphy
35b21164bc Working implementation 2017-10-18 07:42:17 -04:00
Will Murphy
3916d61f16 compiling recursive implementation 2017-10-18 07:42:17 -04:00
Will Murphy
15437bf3a7 use global setting in test 2017-10-18 07:42:17 -04:00
Will Murphy
97b8abe1a7 WORKS but with manual recursion 2017-10-18 07:42:17 -04:00
Will Murphy
fea31ddfb1 WIP something like the right shape 2017-10-18 07:42:17 -04:00
Will Murphy
97e978e7b0 starting path using several small methods, looks promising 2017-10-18 07:42:17 -04:00
Will Murphy
aafcf88354 WIP 2017-10-18 07:42:17 -04:00
Will Murphy
6d981fb970 my tests pass but I broke one existing test 2017-10-18 07:42:17 -04:00
Will Murphy
3bedc69bff explanatory test comments 2017-10-18 07:42:17 -04:00
Will Murphy
1948de9c6b remove outdated comment 2017-10-18 07:42:17 -04:00
Will Murphy
a22e20191b rename inner sub command 2017-10-18 07:42:17 -04:00
Will Murphy
6c014f31b3 remove old copy paste test code 2017-10-18 07:42:17 -04:00
Will Murphy
af28b2ace2 clean up tests and make them ore thorough 2017-10-18 07:42:17 -04:00
Will Murphy
f4c3a21a8f Change tests to reduce duplication
This change lets us test multiple orderings of the global
argument and subcommand issue with less duplicated code.
2017-10-18 07:42:17 -04:00
Will Murphy
12a7310233 Replicate diesel's issue with failing nested subcommand 2017-10-18 07:42:17 -04:00
Will Murphy
6f3b454573 Add failing test for globals to subcommands
Issue 978 mentions that subcommands cannot see the value
of globals that get passed, even if ArgSettings::PropagateGlobalsDown
is true.

This change commits a failing test that reproduces this error.
2017-10-18 07:42:17 -04:00
Kevin K
caeb133bb5 Merge pull request #1067 from kbknapp/issue-1052
Issue 1052
2017-10-12 19:26:38 -04:00
Kevin K
3e645ae9fc
tests: adds tests for required delimiters appearing in the help and usage strings 2017-10-12 15:11:51 -07:00
Kevin K
dce616998e
imp: args that have require_delimiter(true) is now reflected in help and usage strings
Closes #1052
2017-10-12 15:11:24 -07:00
Kevin K
e36ca736ff Merge pull request #1065 from bluejekyll/add-env-help-str
always add env parameter when passed
2017-10-11 21:06:00 -04:00
Benjamin Fry
85f0fd1c87 always add env parameter when passed 2017-10-11 17:15:48 -07:00
Kevin K
797b6b3340 Merge pull request #1062 from kbknapp/bluejekyll-from_env
Bluejekyll from env
2017-10-07 20:03:27 -04:00
Kevin K
2acd9b23f5
Merge branch 'from_env' of https://github.com/bluejekyll/clap-rs into bluejekyll-from_env 2017-10-07 10:22:23 -07:00
Kevin K
609a1f27a1 Merge pull request #1058 from H2CO3/master
Reduce amount of unsafe code
2017-10-07 12:59:00 -04:00
Kevin K
ff5d726455 Merge pull request #1059 from H2CO3/textwrap
Reorganize optionally depending on term_width
2017-10-07 12:57:53 -04:00
Árpád Goretity
e1a4cfe3a3 Bump textwrap version as required 2017-10-06 13:53:08 +02:00
Árpád Goretity
ac97edde90 Reorganize optionally depending on term_width 2017-10-06 13:14:01 +02:00