Closed westandskif closed 2 years ago
Look at the "pro tip" section in https://docs.rs/clap/2.33.3/clap/struct.Arg.html#method.multiple
use std::str::FromStr;
use structopt::StructOpt;
#[derive(Debug, PartialEq, Clone)]
pub enum Protocol {
Http,
Https,
}
impl FromStr for Protocol {
type Err = &'static str;
fn from_str(protocol: &str) -> Result<Self, Self::Err> {
match protocol {
"http" => Ok(Protocol::Http),
"https" => Ok(Protocol::Https),
_ => Err("could not parse protocol"),
}
}
}
#[derive(Debug, StructOpt)]
pub enum Target {
/// accepts lines of urls OR lines with tab-separated urls and countries
Stdin,
/// fetch & test archlinux mirrors
Arch,
/// fetch & test manjaro mirrors
Manjaro,
/// fetch & test rebornos mirrors
#[structopt(name = "rebornos")]
RebornOS,
/// fetch & test artix mirrors
Artix,
}
#[derive(Debug, StructOpt)]
#[structopt(name = "rate-mirrors config")]
/// Usually default options should work
pub struct Config {
/// Per-mirror speed test timeout in milliseconds
#[structopt(subcommand)]
pub target: Target,
/// Test only specified protocols (can be passed multiple times)
#[structopt(long = "protocol", number_of_values = 1)]
pub protocols: Vec<Protocol>,
//...
}
fn main() {
println!("{:?}", Config::from_args());
}
Which works as you expect. Note that clap v3 does a better job than clap v2 on this.
Thank you very much!
Hello! Thank you for the great library!
I got a question about mine, which uses
structopt
. It looks like it doesn't support GNU accepted option syntax.Code: permalink Code sample:
rate-mirrors --protocol http arch
just returns--help
, while docs say:If you run it this way:
rate-mirrors --protocol=http arch
, it works just fine. Am I doing something wrong? Can you please help?