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

Default-able arguments are "missing" when encountering unknown flag with ignore_errors #4498

Open
MarijnS95 opened this issue Nov 21, 2022 · 2 comments

Comments

@MarijnS95
Copy link

When parsing unknown flags into a struct that has ignore_errors(true), clap fails with "The following required argument was not provided: bar" upon encountering an unrecognized flag, whereas bar is a bool flag that should default to false.

Repro

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

use clap::{CommandFactory, FromArgMatches, Parser};

#[derive(Debug, Parser)]
struct Args {
    #[clap(short, long)]
    bar: bool,
}

fn main() {
    let command = Args::command().ignore_errors(true).no_binary_name(true);

    let args = Args::from_arg_matches(&command.clone().get_matches_from::<_, &str>([])).unwrap();
    assert!(!args.bar);
    dbg!(args);

    let args = Args::from_arg_matches(&command.clone().get_matches_from(["--bar"])).unwrap();
    assert!(args.bar);
    dbg!(args);

    let args = Args::from_arg_matches(&command.clone().get_matches_from(["--foo"])).unwrap();
    assert!(!args.bar);
    dbg!(args);

    let args = Args::from_arg_matches(&command.get_matches_from(["--foo", "--bar"])).unwrap();
    assert!(args.bar);
    dbg!(args);
}

Bonus

I added an example with an unknown arg first, assuming it should step over the unknown arg and still set bar to true based on --bar presence. Not sure if that's possible at all, given that we just discussed positional args vs --bar possibly being a value to the --foo flag...

Originally posted by @MarijnS95 in #1404 (comment)

@MarijnS95
Copy link
Author

MarijnS95 commented Nov 21, 2022

Originally posted by @MarijnS95 in #1404 (comment)

Looks like this ends up being a duplicate of #4391, after GH title search (when creating issues) is more helpful than a search for keywords on its own.

EDIT: Or not, since it seems to work correctly for me when not passing --bar, and specifically requires an ignored unknown arg first...

@bonigarcia
Copy link

I am experiencing this problem as well. The following snippet is the relevant part of my clap setup:

#[derive(Parser, Debug)]
#[clap(version, about, long_about = None, ignore_errors = true, help_template = "\
{name} {version}
{about-with-newline}
{usage-heading} {usage}
{all-args}")]
struct Cli {
    /// Browser name (chrome, firefox, edge, iexplorer, safari, safaritp, or webview2)
    #[clap(long, value_parser)]
    browser: Option<String>,

    // ... More fields

    /// Output type: LOGGER (using INFO, WARN, etc.), JSON (custom JSON notation), or SHELL (Unix-like)
    #[clap(long, value_parser, default_value = "LOGGER")]
    output: String,
	
    // ... More fields
}

When I add ignore_errors = true, after adding a non-supported argument, my CLI tool fails as follows:

error: The following required argument was not provided: output

Is there any plan to fix it? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants