-
Notifications
You must be signed in to change notification settings - Fork 152
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
Negative int value gets parsed as a new flag. #129
Comments
Should work
You may also be interested by https://docs.rs/clap/2.32.0/clap/enum.AppSettings.html#variant.AllowLeadingHyphen and https://docs.rs/clap/2.32.0/clap/enum.AppSettings.html#variant.AllowNegativeNumbers See https://github.com/TeXitoi/structopt/blob/master/examples/raw_attributes.rs for an example using AppSettings. I'll add an example later. Does it fix your problem? |
I couldn't find any examples for how to do this without using the equals sign (TeXitoi's third link has a 404 and I can't find it in the examples dir, or maybe I'm not looking hard enough?) but for anybody else who lands here from google, I figured it out and it works like this: #[derive(Debug, StructOpt)]
#[structopt(about = "My little program.", settings = &[AppSettings::AllowNegativeNumbers])]
struct Opt {
/// My parameter that accepts negative numbers
param: i32,
} you can then run it like so: ./myprogram --param -20 |
TeXitoi's workaround also doesn't apply for multiple int arguments, like: #[derive(StructOpt)]
#[structopt(about = "CLI accepting a list of integers.")]
struct VecInt {
#[structopt(short, required = true)]
ints: Vec<isize>,
}, I haven't figured out how to escape the ints on the command line yet. |
There's an I would be happy with a solution where I escape the negative values somewhow. |
@cdstanford can you provide an example command line that fails? I want to make sure I'm finding a solution to your problem and not my guess as to your problem. |
Absolutely -- apologies for not providing a full example before. Here's a CLI that tries to add up lists of integers: use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(about = "Sum one or more lists of ints.")]
enum Cli {
SumOne {
#[structopt(short, required = true, allow_hyphen_values(true))]
a: Vec<isize>,
},
SumTwo {
#[structopt(short, required = true, allow_hyphen_values(true))]
a: Vec<isize>,
#[structopt(short, required = true, allow_hyphen_values(true))]
b: Vec<isize>,
},
}
fn main() {
fn sum(l: &[isize]) -> isize {
l.iter().sum()
}
match Cli::from_args() {
Cli::SumOne { a } => println!("{}", sum(&a)),
Cli::SumTwo { a, b } => println!("{} {}", sum(&a), sum(&b)),
}
} The
|
P.S. Let me know if I should rase a new issue for this case, it's diverged a bit from the original post here. |
If you add this to your clap = { version = "2", features = ["debug"] } You can see whats going wrong under the hood
What I find strange is use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(about = "Sum one or more lists of ints.")]
#[structopt(settings = &[structopt::clap::AppSettings::AllowNegativeNumbers])]
enum Cli {
SumOne {
#[structopt(short, required = true)]
a: Vec<isize>,
},
SumTwo {
#[structopt(short, required = true)]
a: Vec<isize>,
#[structopt(short, required = true)]
b: Vec<isize>,
},
}
fn main() {
fn sum(l: &[isize]) -> isize {
l.iter().sum()
}
match Cli::from_args() {
Cli::SumOne { a } => println!("{}", sum(&a)),
Cli::SumTwo { a, b } => println!("{} {}", sum(&a), sum(&b)),
}
} gives
|
Nevermind, I made the common mistake of forgetting This works: use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(about = "Sum one or more lists of ints.")]
#[structopt(global_setting = structopt::clap::AppSettings::AllowNegativeNumbers)]
enum Cli {
SumOne {
#[structopt(short, required = true)]
a: Vec<isize>,
},
SumTwo {
#[structopt(short, required = true)]
a: Vec<isize>,
#[structopt(short, required = true)]
b: Vec<isize>,
},
}
fn main() {
fn sum(l: &[isize]) -> isize {
l.iter().sum()
}
match Cli::from_args() {
Cli::SumOne { a } => println!("{}", sum(&a)),
Cli::SumTwo { a, b } => println!("{} {}", sum(&a), sum(&b)),
}
}
`` |
I created clap-rs/clap#3028 about the API trap of |
Excellent!! Thanks so much for the help! |
Previously a negative number would be interpreted as a flag. TeXitoi/structopt#129
Previously a negative number would be interpreted as a flag. TeXitoi/structopt#129
With an int-valued flag, one cannot enter a negative integer as parameter, because it gets parsed as new flag.
Eg:
The text was updated successfully, but these errors were encountered: