2015-03-20 16:47:28 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
fn main() {
|
2015-08-27 15:08:49 +00:00
|
|
|
// Positional arguments are those values after the program name which are not preceded by any
|
|
|
|
// identifier (such as "myapp some_file"). Positionals support many of the same options as
|
|
|
|
// flags, as well as a few additional ones.
|
2015-03-20 16:47:28 +00:00
|
|
|
let matches = App::new("MyApp")
|
2018-11-14 17:05:06 +00:00
|
|
|
// Regular App configuration goes here...
|
2020-07-18 17:47:04 +00:00
|
|
|
// We'll add two positional arguments, an input file, and a config file.
|
2018-11-14 17:05:06 +00:00
|
|
|
//
|
|
|
|
// I'll explain each possible setting that "positionals" accept. Keep in
|
|
|
|
// mind that you DO NOT need to set each of these for every flag, only the
|
|
|
|
// ones that apply to your individual case.
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("input")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("the input file to use") // Displayed when showing help info
|
2018-11-14 17:05:06 +00:00
|
|
|
.index(1) // Set the order in which the user must
|
|
|
|
// specify this argument (Starts at 1)
|
|
|
|
.requires("config") // Says, "If the user uses "input", they MUST
|
|
|
|
// also use this other 'config' arg too"
|
2020-07-18 17:47:04 +00:00
|
|
|
// Can also specify a list using
|
2018-11-14 17:05:06 +00:00
|
|
|
// requires_all(Vec<&str>)
|
|
|
|
.required(true), // By default this argument MUST be present
|
|
|
|
// NOTE: mutual exclusions take precedence over
|
|
|
|
// required arguments
|
|
|
|
)
|
2020-05-14 20:50:56 +00:00
|
|
|
.arg(Arg::new("config").about("the config file to use").index(2)) // Note, we do not need to specify required(true)
|
2018-11-14 17:05:06 +00:00
|
|
|
// if we don't want to, because "input" already
|
|
|
|
// requires "config"
|
|
|
|
// Note, we also do not need to specify requires("input")
|
|
|
|
// because requires lists are automatically two-way
|
|
|
|
.get_matches();
|
2015-03-20 16:47:28 +00:00
|
|
|
|
2015-08-27 15:08:49 +00:00
|
|
|
// We can find out whether or not "input" or "config" were used
|
2015-03-20 16:47:28 +00:00
|
|
|
if matches.is_present("input") {
|
2015-08-27 15:08:49 +00:00
|
|
|
println!("An input file was specified");
|
2015-03-20 16:47:28 +00:00
|
|
|
}
|
2015-08-27 15:08:49 +00:00
|
|
|
|
2015-03-20 16:47:28 +00:00
|
|
|
// We can also get the values for those arguments
|
|
|
|
if let Some(ref in_file) = matches.value_of("input") {
|
2018-01-25 04:05:05 +00:00
|
|
|
println!(
|
|
|
|
"Doing work with {} and {}",
|
|
|
|
in_file,
|
2021-11-12 14:42:25 +00:00
|
|
|
matches
|
|
|
|
.value_of("config")
|
|
|
|
.expect("'config' is required and parsing will fail if its missing")
|
2018-01-25 04:05:05 +00:00
|
|
|
);
|
2015-03-20 16:47:28 +00:00
|
|
|
}
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|