@@ -1919,52 +1919,78 @@ impl<'a, 'b> Arg<'a, 'b> {
1919
1919
}
1920
1920
}
1921
1921
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.
1926
1926
///
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.
1929
1931
///
1930
1932
/// # Examples
1931
1933
///
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.
1933
1936
///
1934
1937
/// ```rust
1935
1938
/// # 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"
1940
1946
/// .get_matches_from(vec![
1941
- /// "delims",
1942
- /// "--option=val1,val2,val3",
1947
+ /// "reqdelims", "-o", "val1,val2,val3",
1943
1948
/// ]);
1944
1949
///
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"]);
1948
1952
/// ```
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.
1950
1978
///
1951
1979
/// ```rust
1952
1980
/// # 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"
1958
1987
/// .get_matches_from(vec![
1959
- /// "nodelims",
1960
- /// "--option=val1,val2,val3",
1988
+ /// "reqdelims", "-o", "val1", "val2", "val3",
1961
1989
/// ]);
1962
1990
///
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"]);
1966
1993
/// ```
1967
- /// [`Arg::value_delimiter`]: ./struct.Arg.html#method.value_delimiter
1968
1994
pub fn require_delimiter ( mut self , d : bool ) -> Self {
1969
1995
if d {
1970
1996
self . setb ( ArgSettings :: UseValueDelimiter ) ;
0 commit comments