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
#[command(flatten)] on optional field makes it required #5092
Comments
We have a discrepancy in how we treat A lot of this depends on how people are using an
Can we pick a one-size-fits all option? If not, how do we allow variability of this? |
Is there a workaround? |
The way to workaround this heavily depends on your use case. If you want one argument to be present when the group is, you can use ArgGroup::requires. As an example, let's take the reproduction case and change it to use that: use clap::{Args, Parser};
fn main() {
MainArgs::parse();
}
#[derive(Parser, Debug)]
pub struct MainArgs {
#[arg(short = 'V', long)]
pub version: bool,
#[command(flatten)]
pub specific_args: Option<SpecificArgs>,
}
#[derive(Args, Debug)]
#[group(requires = "positional")]
pub struct SpecificArgs {
#[arg(required = false)]
pub positional: String,
}
|
…guration SOCKS5 optionally supports username+password authentication, which has been implemented previously. This commit addresses a bug in the argument parsing, which made username+password required arguments when adding a remote SOCKS5 api access method using `mullvad api-access add socks5 remote`. Apparently, this is a known pitfall with `clap`: clap-rs/clap#5092
…guration SOCKS5 optionally supports username+password authentication, which has been implemented previously. This commit addresses a bug in the argument parsing, which made username+password required arguments when adding a remote SOCKS5 api access method using `mullvad api-access add socks5 remote`. Apparently, this is a known pitfall with `clap`: clap-rs/clap#5092
…guration SOCKS5 optionally supports username+password authentication, which has been implemented previously. This commit addresses a bug in the argument parsing, which made username+password required arguments when adding a remote SOCKS5 api access method using `mullvad api-access add socks5 remote`. Apparently, this is a known pitfall with `clap`: clap-rs/clap#5092
Please complete the following tasks
Rust Version
1.72
Clap Version
4.3.4
Minimal reproducible code
Steps to reproduce the bug with the above code
Actual Behaviour
Expected Behaviour
No error because the
SpecificArgs
is optional.Additional Context
I want to use this to have a top level
Parser
with a few global args (e.g.--version
but also some others), and I want to reuse this across different CLI tools, each with their ownConcreteArgs
. TheConcreteArgs
are usually written as mandatory in their corresponding#[derive(Args)]
parser, but they should only be mandatory if none of the global flags are present. To do this, I madeSpecificArgs
optional in the#[command(flatten)]
but that doesn't seem to have the intended effect.Similar discussion: #4954
Debug Output
No response
The text was updated successfully, but these errors were encountered: