-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
specifying default value makes field required #2043
Comments
I think you are trying to assign your own semantics to clap here. Having a default value makes the arg optional but the value of the arg will not be undefined but will instead be the default value. |
But |
@jgarvin The temporary workaround is |
@CreepySkeleton can you explain what exactly the problem is and what the solution should be. I am afraid that I don't understand. |
#[derive(Clap)]
struct Opts {
// this option is required because it's not Option (please excuse the pun)
a: u32,
// this option is NOT required, despite the fact it's not Option
// if absent, the default value will be used
#[clap(default_value = "1")]
b: u32,
// this should be a compile time error because default_value (singular)
// is kinda meaningless with `Vec`
//
// This case is what OP complained about: it turns out the field somehow becomes required (WTF???).
#[clap(default_value = "1")]
c: Vec<u32>,
// this should be a legit - and preferable! - usage
// the field is not required
#[clap(default_values = &["1"])]
d: Vec<u32>
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
With use clap::Clap;
#[derive(Clap, Debug)]
struct Opts {
// this option is required because it's not Option (please excuse the pun)
//a: u32,
// this option is NOT required, despite the fact it's not Option
// if absent, the default value will be used
//#[clap(default_value = "1")]
//b: u32,
// this should be a compile time error because default_value (singular)
// is kinda meaningless with `Vec`
//
// This case is what OP complained about: it turns out the field somehow becomes required (WTF???).
#[clap(default_value = "1")]
c: Vec<u32>,
// this should be a legit - and preferable! - usage
// the field is not required
//#[clap(default_values = &["1"])]
//d: Vec<u32>,
}
fn main() {
let opts = Opts::parse(); // panics
dbg!(opts);
}
Am I doing something wrong? |
How to reproduce
With this declaration passing the argument is not required:
with this declaration it is:
I would expect specifying a default value to imply optional, so this doesn't seem like it can be intentional.
Version
The text was updated successfully, but these errors were encountered: