Skip to content

Instantly share code, notes, and snippets.

@rampion
Created April 30, 2021 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rampion/4c610cb8d33ced0defd68741fce71d3d to your computer and use it in GitHub Desktop.
Save rampion/4c610cb8d33ced0defd68741fce71d3d to your computer and use it in GitHub Desktop.
extern crate structopt;
use clap::ArgGroup;
use structopt::StructOpt;
#[derive(Debug, StructOpt, PartialEq)]
#[structopt(group(ArgGroup::with_name("confs").multiple(true)))]
pub struct Command {
#[structopt(long = "config", group = "confs", parse(from_str = config))]
configs: Vec<Conf>,
#[structopt(long = "config-file", group = "confs", parse(from_str = config_file))]
config_files: Vec<Conf>,
}
fn config(src: &str) -> Conf {
Conf::Config(src.to_owned())
}
fn config_file(src: &str) -> Conf {
Conf::ConfigFile(src.to_owned())
}
#[derive(Debug, PartialEq)]
pub enum Conf {
Config(String),
ConfigFile(String),
}
fn main() {
let args = vec![
"name",
"--config",
"steve",
"--config-file",
"dave",
"--config-file",
"charles",
"--config",
"francis",
];
let matches = Command::clap().get_matches_from(args);
let opt = Command::from_clap(&matches);
assert_eq!(
opt,
Command {
configs: vec![
Conf::Config("steve".to_owned()),
Conf::Config("francis".to_owned())
],
config_files: vec![
Conf::ConfigFile("dave".to_owned()),
Conf::ConfigFile("charles".to_owned())
]
}
);
assert_eq!(
matches.values_of("confs").unwrap().collect::<Vec<&str>>(),
vec!["steve", "dave", "charles", "francis"]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment