2015-03-20 16:47:28 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
|
|
|
|
fn main() {
|
2015-08-27 15:08:49 +00:00
|
|
|
// Of the three argument types, flags are the most simple. Flags are simple switches which can
|
|
|
|
// be either "on" or "off"
|
|
|
|
//
|
|
|
|
// clap also supports multiple occurrences of flags, the common example is "verbosity" where a
|
|
|
|
// user could want a little information with "-v" or tons of information with "-v -v" or "-vv"
|
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...
|
|
|
|
// We'll add a flag that represents an awesome meter...
|
|
|
|
//
|
|
|
|
// I'll explain each possible setting that "flags" accept. Keep in mind
|
|
|
|
// that you DO NOT need to set each of these for every flag, only the ones
|
|
|
|
// you want for your individual case.
|
|
|
|
.arg(
|
2020-05-14 20:50:56 +00:00
|
|
|
Arg::new("awesome")
|
2020-04-21 15:51:38 +00:00
|
|
|
.about("turns up the awesome") // Displayed when showing help info
|
2018-11-14 17:05:06 +00:00
|
|
|
.short('a') // Trigger this arg with "-a"
|
|
|
|
.long("awesome") // Trigger this arg with "--awesome"
|
|
|
|
.multiple(true) // This flag should allow multiple
|
|
|
|
// occurrences such as "-aaa" or "-a -a"
|
|
|
|
.requires("config") // Says, "If the user uses -a, 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>)
|
|
|
|
.conflicts_with("output"), // Opposite of requires(), says "if the
|
|
|
|
// user uses -a, they CANNOT use 'output'"
|
|
|
|
// also has a conflicts_with_all(Vec<&str>)
|
2020-07-18 17:47:04 +00:00
|
|
|
// and an exclusive(true)
|
2018-11-14 17:05:06 +00:00
|
|
|
)
|
|
|
|
// NOTE: In order to compile this example, comment out requires() and
|
|
|
|
// conflicts_with() because we have not defined an "output" or "config"
|
|
|
|
// argument.
|
|
|
|
.get_matches();
|
2015-03-20 16:47:28 +00:00
|
|
|
|
|
|
|
// We can find out whether or not awesome was used
|
|
|
|
if matches.is_present("awesome") {
|
2015-08-27 15:08:49 +00:00
|
|
|
println!("Awesomeness is turned on");
|
2015-03-20 16:47:28 +00:00
|
|
|
}
|
2015-08-27 15:08:49 +00:00
|
|
|
|
2020-07-18 17:47:04 +00:00
|
|
|
// If we set the multiple() option of a flag we can check how many times the user specified
|
2015-03-20 16:47:28 +00:00
|
|
|
//
|
|
|
|
// Note: if we did not specify the multiple() option, and the user used "awesome" we would get
|
|
|
|
// a 1 (no matter how many times they actually used it), or a 0 if they didn't use it at all
|
|
|
|
match matches.occurrences_of("awesome") {
|
|
|
|
0 => println!("Nothing is awesome"),
|
|
|
|
1 => println!("Some things are awesome"),
|
|
|
|
2 => println!("Lots of things are awesome"),
|
2020-05-02 20:26:19 +00:00
|
|
|
_ => println!("EVERYTHING is awesome!"),
|
2015-03-20 16:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|