2015-03-20 16:47:28 +00:00
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
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")
|
2015-08-27 15:08:49 +00:00
|
|
|
// Regular App configuration goes here...
|
2015-03-20 16:47:28 +00:00
|
|
|
|
2015-08-27 15:08:49 +00:00
|
|
|
// We'll add two positional arguments, a input file, and a config file.
|
|
|
|
//
|
|
|
|
// 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.
|
2015-04-14 02:18:50 +00:00
|
|
|
.arg(Arg::with_name("input")
|
2015-03-20 16:47:28 +00:00
|
|
|
.help("the input file to use") // Displayed when showing help info
|
2015-08-27 15:08:49 +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"
|
|
|
|
// Can also specifiy a list using
|
|
|
|
// requires_all(Vec<&str>)
|
2015-06-30 02:21:47 +00:00
|
|
|
.conflicts_with("output") // Opposite of requires(), says "if the
|
2015-08-27 15:08:49 +00:00
|
|
|
// user uses -a, they CANNOT use 'output'"
|
|
|
|
// also has a mutually_excludes_all(Vec<&str>)
|
|
|
|
.required(true) // By default this argument MUST be present
|
|
|
|
// NOTE: mutual exclusions take precedence over
|
|
|
|
// required arguments
|
2015-03-20 16:47:28 +00:00
|
|
|
)
|
2015-08-27 15:08:49 +00:00
|
|
|
.arg(Arg::with_name("config")
|
|
|
|
.help("the config file to use")
|
|
|
|
.index(2)) // Note, we do not need to specify required(true)
|
|
|
|
// 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
|
2015-03-20 16:47:28 +00:00
|
|
|
|
2015-08-27 15:08:49 +00:00
|
|
|
// NOTE: In order to compile this example, comment out mutually_excludes()
|
|
|
|
// because we have not defined an "output" argument.
|
2015-03-20 16:47:28 +00:00
|
|
|
.get_matches();
|
|
|
|
|
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") {
|
2015-08-27 15:08:49 +00:00
|
|
|
// It's safe to call unwrap() because of the required options we set above
|
2018-01-25 04:05:05 +00:00
|
|
|
println!(
|
|
|
|
"Doing work with {} and {}",
|
|
|
|
in_file,
|
|
|
|
matches.value_of("config").unwrap()
|
|
|
|
);
|
2015-03-20 16:47:28 +00:00
|
|
|
}
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|