Make sure that command line OVERRIDES env, not prepends (fix #1835)

This commit is contained in:
CreepySkeleton 2020-04-22 11:35:20 +03:00
parent dd1505fdfd
commit 0e7ff777da
3 changed files with 28 additions and 17 deletions

View file

@ -489,7 +489,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -553,7 +553,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -2183,7 +2183,7 @@ impl<'help> Arg<'help> {
/// ```
/// Running the above program produces the following output
///
/// ```notrust
/// ```text
/// valnames
///
/// USAGE:
@ -2252,7 +2252,7 @@ impl<'help> Arg<'help> {
/// ```
/// Running the above program produces the following output
///
/// ```notrust
/// ```text
/// valnames
///
/// USAGE:
@ -2780,7 +2780,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays the following help message
///
/// ```notrust
/// ```text
/// cust-ord
///
/// USAGE:
@ -3354,7 +3354,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -3551,7 +3551,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays the following help message
///
/// ```notrust
/// ```text
/// nlh
///
/// USAGE:
@ -3610,7 +3610,7 @@ impl<'help> Arg<'help> {
///
/// The following would be parsed as values to `--ui-paths`.
///
/// ```notrust
/// ```text
/// $ program --ui-paths path1 path2 signer
/// ```
///
@ -3622,7 +3622,7 @@ impl<'help> Arg<'help> {
/// valid, and `signer` is parsed as a subcommand in the first case, but a value in the second
/// case.
///
/// ```notrust
/// ```text
/// $ program --ui-paths path1 signer
/// $ program --ui-paths path1 --ui-paths signer signer
/// ```
@ -3901,7 +3901,7 @@ impl<'help> Arg<'help> {
/// that setting this requires all values to come after a `--` to indicate they
/// should all be captured. For example:
///
/// ```notrust
/// ```text
/// --foo something -- -v -v -v -b -b -b --baz -q -u -x
/// ```
/// Will result in everything after `--` to be considered one raw argument. This behavior
@ -3951,7 +3951,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -3978,7 +3978,7 @@ impl<'help> Arg<'help> {
///
/// Then the following would be displayed
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -4029,7 +4029,7 @@ impl<'help> Arg<'help> {
///
/// The above example displays
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:
@ -4056,7 +4056,7 @@ impl<'help> Arg<'help> {
///
/// Then the following would be displayed
///
/// ```notrust
/// ```text
/// helptest
///
/// USAGE:

View file

@ -1557,9 +1557,12 @@ where
pub(crate) fn add_env(&mut self, matcher: &mut ArgMatcher) -> ClapResult<()> {
for a in self.app.args.args.iter() {
if let Some(ref val) = a.env {
if let Some(ref val) = val.1 {
self.add_val_to_arg(a, OsStr::new(val), matcher)?;
// Use env only if the arg was not present among command line args
if matcher.get(&a.id).map_or(true, |a| a.occurs == 0) {
if let Some(ref val) = a.env {
if let Some(ref val) = val.1 {
self.add_val_to_arg(a, OsStr::new(val), matcher)?;
}
}
}
}

View file

@ -82,6 +82,10 @@ fn opt_user_override() {
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 1);
assert_eq!(m.value_of("arg").unwrap(), "opt");
// see https://github.com/clap-rs/clap/issues/1835
let values: Vec<_> = m.values_of("arg").unwrap().collect();
assert_eq!(values, vec!["opt"]);
}
#[test]
@ -112,6 +116,10 @@ fn positionals_user_override() {
assert!(m.is_present("arg"));
assert_eq!(m.occurrences_of("arg"), 1);
assert_eq!(m.value_of("arg").unwrap(), "opt");
// see https://github.com/clap-rs/clap/issues/1835
let values: Vec<_> = m.values_of("arg").unwrap().collect();
assert_eq!(values, vec!["opt"]);
}
#[test]