Skip to content

Commit 49af4e3

Browse files
committed
docs(Arg): adds docs for
1 parent 920b559 commit 49af4e3

File tree

1 file changed

+54
-28
lines changed

1 file changed

+54
-28
lines changed

src/args/arg.rs

+54-28
Original file line numberDiff line numberDiff line change
@@ -1919,52 +1919,78 @@ impl<'a, 'b> Arg<'a, 'b> {
19191919
}
19201920
}
19211921

1922-
/// Specifies whether or not an argument should allow grouping of multiple values via a
1923-
/// delimiter. I.e. should `--option=val1,val2,val3` be parsed as three values (`val1`, `val2`,
1924-
/// and `val3`) or as a single value (`val1,val2,val3`). Defaults to using `,` (comma) as the
1925-
/// value delimiter for all arguments that accept values (options and positional arguments)
1922+
/// Specifies that *multiple values* may only be set using the delimiter. This means if an
1923+
/// if an option is encountered, and no delimiter is found, it automatically assumed that no
1924+
/// additional values for that option follow. This is unlike the default, where it is generally
1925+
/// assumed that more values will follow regardless of whether or not a delimiter is used.
19261926
///
1927-
/// **NOTE:** The default is `true`. Setting the value to `true` will reset any previous use of
1928-
/// [`Arg::value_delimiter`] back to the default of `,` (comma).
1927+
/// **NOTE:** The default is `false`.
1928+
///
1929+
/// **NOTE:** It's a good idea to inform the user that use of a delimiter is required, either
1930+
/// through help text or other means.
19291931
///
19301932
/// # Examples
19311933
///
1932-
/// The following example shows the default behavior.
1934+
/// These examples demonstrate what happens when `require_delimiter(true)` is used. Notice
1935+
/// everything works in this first example, as we use a delimiter, as expected.
19331936
///
19341937
/// ```rust
19351938
/// # use clap::{App, Arg};
1936-
/// let delims = App::new("delims")
1937-
/// .arg(Arg::with_name("option")
1938-
/// .long("option")
1939-
/// .takes_value(true))
1939+
/// let delims = App::new("reqdelims")
1940+
/// .arg(Arg::with_name("opt")
1941+
/// .short("o")
1942+
/// .takes_value(true)
1943+
/// .multiple(true)
1944+
/// .require_delimiter(true))
1945+
/// // Simulate "$ reqdelims -o val1,val2,val3"
19401946
/// .get_matches_from(vec![
1941-
/// "delims",
1942-
/// "--option=val1,val2,val3",
1947+
/// "reqdelims", "-o", "val1,val2,val3",
19431948
/// ]);
19441949
///
1945-
/// assert!(delims.is_present("option"));
1946-
/// assert_eq!(delims.occurrences_of("option"), 1);
1947-
/// assert_eq!(delims.values_of("option").unwrap().collect::<Vec<_>>(), ["val1", "val2", "val3"]);
1950+
/// assert!(delims.is_present("opt"));
1951+
/// assert_eq!(delims.values_of("opt").unwrap().collect::<Vec<_>>(), ["val1", "val2", "val3"]);
19481952
/// ```
1949-
/// The next example shows the difference when turning delimiters off.
1953+
/// In this next example, we will *not* use a delimiter. Notice it's now an error.
1954+
///
1955+
/// ```rust
1956+
/// # use clap::{App, Arg, ErrorKind};
1957+
/// let res = App::new("reqdelims")
1958+
/// .arg(Arg::with_name("opt")
1959+
/// .short("o")
1960+
/// .takes_value(true)
1961+
/// .multiple(true)
1962+
/// .require_delimiter(true))
1963+
/// // Simulate "$ reqdelims -o val1 val2 val3"
1964+
/// .get_matches_from_safe(vec![
1965+
/// "reqdelims", "-o", "val1", "val2", "val3",
1966+
/// ]);
1967+
///
1968+
/// assert!(res.is_err());
1969+
/// let err = res.unwrap_err();
1970+
/// assert_eq!(err.kind, ErrorKind::UnknownArgument);
1971+
/// ```
1972+
/// What's happening is `-o` is getting `val1`, and because delimiters are required yet none
1973+
/// were present, it stops parsing `-o`. At this point it reaches `val2` and because no
1974+
/// positional arguments have been defined, it's an error of an unexpected argument.
1975+
///
1976+
/// In this final example, we contrast the above with `clap`'s default behavior where the above
1977+
/// is *not* an error.
19501978
///
19511979
/// ```rust
19521980
/// # use clap::{App, Arg};
1953-
/// let nodelims = App::new("nodelims")
1954-
/// .arg(Arg::with_name("option")
1955-
/// .long("option")
1956-
/// .use_delimiter(false)
1957-
/// .takes_value(true))
1981+
/// let delims = App::new("reqdelims")
1982+
/// .arg(Arg::with_name("opt")
1983+
/// .short("o")
1984+
/// .takes_value(true)
1985+
/// .multiple(true))
1986+
/// // Simulate "$ reqdelims -o val1 val2 val3"
19581987
/// .get_matches_from(vec![
1959-
/// "nodelims",
1960-
/// "--option=val1,val2,val3",
1988+
/// "reqdelims", "-o", "val1", "val2", "val3",
19611989
/// ]);
19621990
///
1963-
/// assert!(nodelims.is_present("option"));
1964-
/// assert_eq!(nodelims.occurrences_of("option"), 1);
1965-
/// assert_eq!(nodelims.value_of("option").unwrap(), "val1,val2,val3");
1991+
/// assert!(delims.is_present("opt"));
1992+
/// assert_eq!(delims.values_of("opt").unwrap().collect::<Vec<_>>(), ["val1", "val2", "val3"]);
19661993
/// ```
1967-
/// [`Arg::value_delimiter`]: ./struct.Arg.html#method.value_delimiter
19681994
pub fn require_delimiter(mut self, d: bool) -> Self {
19691995
if d {
19701996
self.setb(ArgSettings::UseValueDelimiter);

0 commit comments

Comments
 (0)