-
-
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
Add AllowNegativeNumbers (similar to AllowLeadingHyphen, but more restricted) #696
Comments
Can you post the source, or a link to the source? It could be some option set that's causing it. |
Got it. If I enable the "AllowLeadingHyphen" setting, then check/suggest/exit does not work. |
Ah, yeah the docs warn that, that particular setting will silence what would have otherwise been certain errors. You can still manually validate those values using a I'm going to close this, but let me know if you need any help with the solution! 😄 |
|
@chenhouwu
See:
For example, lets say you only want to support numbers, which might be negative (i.e. fn is_num(s: String) -> Result<(), String> {
if let Err(..) = s.parse::<i64>() {
return Err(String::from("Not a valid number!"));
}
Ok(())
}
let m = App::new("test")
.setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::with_name("num")
.validator(is_num))
.get_matches();
Kind of. You can add all args in the YAML like normal, but have to add the arg using a validator manaully. i.e. fn some_function(s: String) -> Result<(), String) {
Ok(())
}
fn main() {
let yml = load_yaml!("17_yaml.yml");
let m = App::from_yaml(yml)
.arg(Arg::with_name("needs_validator")
.validator(some_function))
.get_matches();
}
Yes. But it depends on the shell being used as to which escape codes must be used.
The best way to do that is with a |
Which one do you mean:
Or:
But without reading the clap codes, I think the leading hyphen check is a pre-check before the validator, so there is a paradox:
Any misunderstanding? |
Do you mean:
If an arg is already in the YAML, then is it valid to add or update its settings from rust codes? |
That is correct. You can add all other args, besides the ones which need to be validated.
You can't disable Sorry for any confusion 😄 |
I have implemented a work around just as question
I use |
But I recommend to implement |
Interesting, I hadn't thought of adding |
@chenhouwu try out v2.15.0 on crates.io and see if that works for your use case. |
it works, great! |
I have a tool which should be invoked by
mytool --foo
, but when I invoke bymytool --foo --bar
, it will just run as normal without any warning.I don't know why, because I remember in previous version, I will get an error and exit, sometimes with suggestions like "Do you mean ....".
It's quite dangerous especially for optional arguments, because any typo will passed the check while the user just think he give correct arguments.
The text was updated successfully, but these errors were encountered: