2021-12-01 20:30:51 +00:00
|
|
|
use clap::{ArgEnum, Parser};
|
2021-11-30 03:05:42 +00:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
#[clap(about, version, author)] // Pull these from `Cargo.toml`
|
|
|
|
struct Cli {
|
|
|
|
/// Sets a custom config file. Could have been an Option<T> with no default too
|
2021-12-01 20:30:51 +00:00
|
|
|
#[clap(
|
|
|
|
short,
|
|
|
|
long,
|
|
|
|
parse(from_os_str),
|
|
|
|
default_value = "default.toml",
|
|
|
|
value_name = "PATH"
|
|
|
|
)]
|
2021-11-30 03:05:42 +00:00
|
|
|
config: std::path::PathBuf,
|
|
|
|
/// Some input. Because this isn't an Option<T> it's required to be used
|
|
|
|
input: String,
|
2021-12-01 20:30:51 +00:00
|
|
|
/// What mode to run the program in
|
|
|
|
#[clap(short, long, arg_enum, default_value_t)]
|
|
|
|
mode: Mode,
|
2021-11-30 03:05:42 +00:00
|
|
|
/// A level of verbosity, and can be used multiple times
|
|
|
|
#[clap(short, long, parse(from_occurrences))]
|
|
|
|
verbose: i32,
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:30:51 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum)]
|
|
|
|
enum Mode {
|
|
|
|
Fast,
|
|
|
|
Slow,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Mode {
|
|
|
|
fn default() -> Self {
|
|
|
|
Mode::Slow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Mode {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.to_possible_value()
|
|
|
|
.expect("no values are skipped")
|
|
|
|
.get_name()
|
|
|
|
.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 03:05:42 +00:00
|
|
|
fn main() {
|
2021-12-01 20:30:51 +00:00
|
|
|
let cli = Cli::parse();
|
2021-11-30 03:05:42 +00:00
|
|
|
|
2021-12-01 20:30:51 +00:00
|
|
|
println!("Value for config: {}", cli.config.display());
|
|
|
|
println!("Using input file: {}", cli.input);
|
|
|
|
match cli.mode {
|
|
|
|
Mode::Fast => {
|
|
|
|
println!("Hare");
|
|
|
|
}
|
|
|
|
Mode::Slow => {
|
|
|
|
println!("Tortoise");
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 03:05:42 +00:00
|
|
|
|
|
|
|
// Vary the output based on how many times the user used the "verbose" flag
|
|
|
|
// (i.e. 'myprog -v -v -v' or 'myprog -vvv' vs 'myprog -v'
|
2021-12-01 20:30:51 +00:00
|
|
|
match cli.verbose {
|
2021-11-30 03:05:42 +00:00
|
|
|
0 => println!("No verbose info"),
|
|
|
|
1 => println!("Some verbose info"),
|
|
|
|
2 => println!("Tons of verbose info"),
|
|
|
|
_ => println!("Don't be ridiculous"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// more program logic goes here...
|
|
|
|
}
|