Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arguments with takes_value(false) still accept "compact"-style arguments #1543

Closed
sfackler opened this issue Sep 4, 2019 · 2 comments · Fixed by #2619
Closed

Arguments with takes_value(false) still accept "compact"-style arguments #1543

sfackler opened this issue Sep 4, 2019 · 2 comments · Fixed by #2619
Labels
C-bug Category: Updating dependencies
Milestone

Comments

@sfackler
Copy link

sfackler commented Sep 4, 2019

Rust Version

  • 1.37.0

Affected Version of clap

  • 2.33.0

Bug or Feature Request Summary

Arguments configured to not take a value still successfully parse from the --key=value form.

Expected Behavior Summary

The command line should fail to validate (or only accept --key=true or --key=false.

Actual Behavior Summary

The command line successfully validates as if it just contained --key.

Sample Code or Link to Sample Code

use clap::*;

fn main() {
    let matches = App::new("foo")
        .arg(Arg::with_name("foobar").long("foobar").takes_value(false).multiple(false))
        .get_matches_from(["foo", "--foobar=false"].iter());
    println!("{:?}", matches)
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d08679674fa982b892dcc26d8ed8bb25

@haxpor
Copy link

haxpor commented Mar 21, 2022

Can we reopen this issue? I tested this from the tip of the git tree and the problem is still happening, also I faced the same issue on my own code using 3.1.6.

Modify tests/builder/multiple_occurrences.rs (just for the sake of quick dirty test)

#[cfg(feature = "env")]
#[test]
fn multiple_occurrences_of_before_env() {
    let cmd = Command::new("mo_before_env").arg(
        Arg::new("verbose")
            .env("VERBOSE")
            .short('v')
            .long("verbose")
            .takes_value(false)
            .multiple_occurrences(true),
    );

    let m = cmd.clone().try_get_matches_from(vec![""]);
    assert!(m.is_ok(), "{}", m.unwrap_err());
    assert_eq!(m.unwrap().occurrences_of("verbose"), 0);

    let m = cmd.clone().try_get_matches_from(vec!["", "-v"]);
    assert!(m.is_ok(), "{}", m.unwrap_err());
    assert_eq!(m.unwrap().occurrences_of("verbose"), 1);

    let m = cmd.clone().try_get_matches_from(vec!["", "-vv"]);
    assert!(m.is_ok(), "{}", m.unwrap_err());
    assert_eq!(m.unwrap().occurrences_of("verbose"), 2);
    let m = cmd.clone().try_get_matches_from(vec!["", "-vvv"]);
    assert!(m.is_ok(), "{}", m.unwrap_err());
    assert_eq!(m.unwrap().occurrences_of("verbose"), 3);

    // this chunk of code is added to test
    // notice `-vvv=false`, test result is fine, no error out
    let m = cmd.clone().try_get_matches_from(vec!["", "-vvv=false"]);
    assert!(m.is_ok(), "{}", m.unwrap_err());
    assert_eq!(m.unwrap().occurrences_of("verbose"), 3);
}

as well I didn't see relevant changes that fix this issue in #2619.
My apology if I'm wrong nor missing something.

@epage
Copy link
Member

epage commented Mar 21, 2022

I just tried reproducing with the code sample you gave and it failed

thread 'multiple_occurrences::multiple_occurrences_of_before_env' panicked at 'error: Found argument '-=' which wasn't expected, or isn't valid in this context

        If you tried to supply `-=` as a value rather than a flag, use `-- -=`

USAGE:
    mo_before_env [OPTIONS]

For more information try --help
', tests/builder/multiple_occurrences.rs:128:5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
5 participants