Skip to content

Commit 03455b7

Browse files
committed
docs: adds addtional blurbs about using multiples with subcommands
1 parent 0c223f5 commit 03455b7

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/args/arg.rs

+38-5
Original file line numberDiff line numberDiff line change
@@ -1891,18 +1891,48 @@ impl<'a, 'b> Arg<'a, 'b> {
18911891
/// **WARNING:**
18921892
///
18931893
/// Setting `multiple(true)` for an [option] with no other details, allows multiple values
1894-
/// **and** multiple occurrences because it isn't possible to have more occurrences than values for
1895-
/// options. Because multiple values are allowed, `--option val1 val2 val3` is perfectly valid,
1896-
/// be careful when designing a CLI where positional arguments are expected after a option which
1897-
/// accepts multiple values, as `clap` will continue parsing *values* until it reaches the max
1898-
/// or specific number of values defined, or another flag or option.
1894+
/// **and** multiple occurrences because it isn't possible to have more occurrences than values
1895+
/// for options. Because multiple values are allowed, `--option val1 val2 val3` is perfectly
1896+
/// valid, be careful when designing a CLI where positional arguments are expected after a
1897+
/// option which accepts multiple values, as `clap` will continue parsing *values* until it
1898+
/// reaches the max or specific number of values defined, or another flag or option.
18991899
///
19001900
/// **Pro Tip**:
19011901
///
19021902
/// It's possible to define an option which allows multiple occurrences, but only one value per
19031903
/// occurrence. To do this use [`Arg::number_of_values(1)`] in coordination with
19041904
/// [`Arg::multiple(true)`].
19051905
///
1906+
/// **WARNING:**
1907+
///
1908+
/// When using args with `multiple(true)` on [options] or [positionals] (i.e. those args that
1909+
/// accept values) and [subcommands], one needs to consider the posibility of an argument value
1910+
/// being the same as a valid subcommand. By default `clap` will parse the argument in question
1911+
/// as a value *only if* a value is possible at that moment. Otherwise it will be parsed as a
1912+
/// subcommand. In effect, this means using `multiple(true)` with no additional parameters and
1913+
/// a possible value that coincides with a subcommand name, the subcommand cannot be called
1914+
/// unless another argument is passed first.
1915+
///
1916+
/// As an example, consider a CLI with an option `--ui-paths=<paths>...` and subcommand `signer`
1917+
///
1918+
/// The following would be parsed as values to `--ui-paths`.
1919+
///
1920+
/// ```notrust
1921+
/// $ program --ui-paths path1 path2 signer
1922+
/// ```
1923+
///
1924+
/// This is because `--ui-paths` accepts multiple values. `clap` will continue parsing values
1925+
/// until another argument is reached and it knows `--ui-paths` is done.
1926+
///
1927+
/// By adding additional parameters to `--ui-paths` we can solve this issue. Consider adding
1928+
/// [`Arg::number_of_values(1)`] as discussed above. The following are all valid, and `signer`
1929+
/// is parsed as both a subcommand and a value in the second case.
1930+
///
1931+
/// ```notrust
1932+
/// $ program --ui-paths path1 signer
1933+
/// $ program --ui-paths path1 --ui-paths signer signer
1934+
/// ```
1935+
///
19061936
/// # Examples
19071937
///
19081938
/// ```rust
@@ -2036,6 +2066,9 @@ impl<'a, 'b> Arg<'a, 'b> {
20362066
/// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
20372067
/// ```
20382068
/// [option]: ./struct.Arg.html#method.takes_value
2069+
/// [options]: ./struct.Arg.html#method.takes_value
2070+
/// [subcommands]: ./struct.SubCommand.html
2071+
/// [positionals]: ./struct.Arg.html#method.index
20392072
/// [`Arg::number_of_values(1)`]: ./struct.Arg.html#method.number_of_values
20402073
/// [`Arg::multiple(true)`]: ./struct.Arg.html#method.multiple
20412074
pub fn multiple(self, multi: bool) -> Self {

0 commit comments

Comments
 (0)