2015-09-01 03:50:17 +00:00
|
|
|
// In order to use YAML to define your CLI you must compile clap with the "yaml" feature becasue
|
|
|
|
// it's **not** included by default.
|
|
|
|
//
|
|
|
|
// In order to do this, ensure your Cargo.toml looks like one of the following:
|
|
|
|
//
|
|
|
|
// [dependencies.clap]
|
|
|
|
// features = ["yaml"]
|
|
|
|
//
|
|
|
|
// __OR__
|
|
|
|
//
|
|
|
|
// [dependencies]
|
|
|
|
// clap = { features = ["yaml"] }
|
|
|
|
|
2020-01-09 03:56:27 +00:00
|
|
|
// Using yaml requires calling a clap macro `load_yaml!()`.
|
2018-11-05 01:11:48 +00:00
|
|
|
// Note: If you're using clap as a dependency and don't have a feature for your users called
|
|
|
|
// "yaml", you'll need to remove the #[cfg(feature = "yaml")] conditional compilation attribute
|
2015-12-08 04:16:07 +00:00
|
|
|
#[cfg(feature = "yaml")]
|
2015-09-01 03:50:17 +00:00
|
|
|
fn main() {
|
2020-02-03 18:04:07 +00:00
|
|
|
use clap::{load_yaml, App};
|
2016-01-12 05:13:14 +00:00
|
|
|
|
2015-09-01 03:50:17 +00:00
|
|
|
// To load a yaml file containing our CLI definition such as the example '17_yaml.yml' we can
|
|
|
|
// use the convenience macro which loads the file at compile relative to the current file
|
|
|
|
// similiar to how modules are found.
|
|
|
|
//
|
|
|
|
// Then we pass that yaml object to App to build the CLI.
|
|
|
|
//
|
|
|
|
// Finally we call get_matches() to start the parsing process. We use the matches just as we
|
|
|
|
// normally would
|
|
|
|
let yml = load_yaml!("17_yaml.yml");
|
2019-06-19 22:49:09 +00:00
|
|
|
let m = App::from(yml).get_matches();
|
2015-09-01 03:50:17 +00:00
|
|
|
|
|
|
|
// Because the example 17_yaml.yml is rather large we'll just look a single arg so you can
|
|
|
|
// see that it works...
|
|
|
|
if let Some(mode) = m.value_of("mode") {
|
|
|
|
match mode {
|
2016-10-27 10:24:08 +00:00
|
|
|
"vi" => println!("You are using vi"),
|
|
|
|
"emacs" => println!("You are using emacs..."),
|
2018-01-25 04:05:05 +00:00
|
|
|
_ => unreachable!(),
|
2015-09-01 03:50:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("--mode <MODE> wasn't used...");
|
|
|
|
}
|
2015-12-08 04:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "yaml"))]
|
|
|
|
fn main() {
|
|
|
|
// As stated above, if clap is not compiled with the YAML feature, it is disabled.
|
|
|
|
println!("YAML feature is disabled.");
|
|
|
|
println!("Pass --features yaml to cargo when trying this example.");
|
|
|
|
}
|