-
-
Notifications
You must be signed in to change notification settings - Fork 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
Argument cannot require a particular value of positional argument #764
Comments
.arg(Arg::with_name("error")
.long("error")
.requires("element") // Use the name, not the value
.default_value("0.25")
)
.arg(Arg::with_name("element")
.value_name("ELEMENT")
.possible_value("channel")
.min_values(1)
); Let me know if that works as expected. 😉 |
The point of this issue is that I want to require specific value of (there
might be more valid values added eventually) the positional argument.
I understand the feature like that not being built-in, but I would very
much like if clap at very least figured out the argument config was
malformed (i.e. I required a non-existant argument) and told me so.
…On Dec 5, 2016 04:16, "Kevin K." ***@***.***> wrote:
Arg::requires takes an argument name, not an argument value. Try:
.arg(Arg::with_name("error")
.long("error")
.requires("element") // NB: particular value of a positional argument
.default_value("0.25")
)
.arg(Arg::with_name("element")
.value_name("ELEMENT")
.possible_value("channel")
.min_values(1)
);
Let me know if that works as expected. 😉
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#764 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AApc0qDGpgdxoZBD_22kiS4iT8RATLgIks5rE3QQgaJpZM4LDmS5>
.
|
Ahhh Ok, my mistake! There seems to be two version of this issue in my mind.
Both seem doable, although I haven't tried implementing them yet. With number 2, the only issue I can see is should this be a new default (i.e. only set
I had a branch which was the base of 3.x which used enum variants as argument keys instead of strings (although was still backwards compatible with strings) which solved exactly this. Although I've been waiting for Macros 1.1 in order to finish this. Edit: more thoughts on the matter |
I'm working on this in a local branch, here's the TODO:
|
…ethods for conditional default values One can now implement conditional default values. I.e. a default value that is only applied in certain conditions, such as another arg being present, or another arg being present *and* containing a specific value. Now it's possible to say, "Only apply this default value if arg X is present" or "Only apply this value if arg X is present, but also only if arg X's value is equal to Y" This new method is fully compatible with the current `Arg::default_value`, which gets set only if the arg wasn't used at runtime *and* none of the specified conditions were met. Releates to #764
…uire additional args An arg can now conditionally require additional arguments if it's value matches a specific value. For example, arg A can state that it only requires arg B if the value X was used with arg A. If any other value is used with A, arg B isn't required. Relates to #764
…ethods for conditional default values One can now implement conditional default values. I.e. a default value that is only applied in certain conditions, such as another arg being present, or another arg being present *and* containing a specific value. Now it's possible to say, "Only apply this default value if arg X is present" or "Only apply this value if arg X is present, but also only if arg X's value is equal to Y" This new method is fully compatible with the current `Arg::default_value`, which gets set only if the arg wasn't used at runtime *and* none of the specified conditions were met. Releates to #764
…uire additional args An arg can now conditionally require additional arguments if it's value matches a specific value. For example, arg A can state that it only requires arg B if the value X was used with arg A. If any other value is used with A, arg B isn't required. Relates to #764
…tionally required An arg can now be conditionally required (i.e. it's only required if arg A is used with a value of V). For example: ```rust let res = App::new("ri") .arg(Arg::with_name("cfg") .required_if("extra", "val") .takes_value(true) .long("config")) .arg(Arg::with_name("extra") .takes_value(true) .long("extra")) .get_matches_from_safe(vec![ "ri", "--extra", "val" ]); assert!(res.is_err()); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); ``` Relates to #764
Affected Version of clap
Expected Behavior Summary
The application works or clap reports a misconfiguration error.
Actual Behavior Summary
Instead clap complains about some required argument not being provided
and doesn’t accept either of
--error 0.25
orchannel
positional argument in isolation:Steps to Reproduce the issue
The text was updated successfully, but these errors were encountered: