2020-01-09 03:56:27 +00:00
|
|
|
use clap::clap_app;
|
2015-09-08 12:53:31 +00:00
|
|
|
|
2016-01-21 06:48:30 +00:00
|
|
|
// Note, there isn't a need for "use clap::{ ... };" Because the clap_app! macro uses
|
|
|
|
// $crate:: internally
|
2015-09-08 12:53:31 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Validation example testing that a file exists
|
2020-04-30 17:52:45 +00:00
|
|
|
let file_exists = |path: &str| {
|
2015-09-08 12:53:31 +00:00
|
|
|
if std::fs::metadata(path).is_ok() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(String::from("File doesn't exist"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// External module may contain this subcommand. If this exists in another module, a function is
|
2018-10-19 20:42:13 +00:00
|
|
|
// required to access it. Recommend `fn clap() -> Clap::`.
|
2015-09-08 12:53:31 +00:00
|
|
|
let external_sub_command = clap_app!( @subcommand foo =>
|
|
|
|
(@arg bar: -b "Bar")
|
|
|
|
);
|
|
|
|
|
|
|
|
let matches = clap_app!(MyApp =>
|
|
|
|
(@setting SubcommandRequiredElseHelp)
|
|
|
|
(version: "1.0")
|
|
|
|
(author: "Alice")
|
2020-06-20 10:52:39 +00:00
|
|
|
(license: "MIT OR Apache-2.0")
|
2015-09-08 12:53:31 +00:00
|
|
|
(about: "Does awesome things")
|
|
|
|
(@arg config: -c --config <conf> #{1, 2} {file_exists} "Sets a custom config file")
|
2016-12-13 06:42:11 +00:00
|
|
|
(@arg proxyHostname: --("proxy-hostname") +takes_value "Sets the hostname of the proxy to use")
|
2015-09-08 12:53:31 +00:00
|
|
|
(@arg input: * "Input file")
|
|
|
|
(@group test =>
|
|
|
|
(@attributes +required)
|
|
|
|
(@arg output: "Sets an optional output file")
|
|
|
|
(@arg debug: -d ... "Turn debugging information on")
|
|
|
|
)
|
|
|
|
(subcommand: external_sub_command)
|
|
|
|
(@subcommand test =>
|
|
|
|
(about: "does testing things")
|
|
|
|
(version: "2.5")
|
|
|
|
(@arg list: -l "Lists test values")
|
|
|
|
(@arg test_req: -r requires[list] "Tests requirement for listing")
|
|
|
|
(@arg aaaa: --aaaa +takes_value {
|
2019-10-02 13:27:19 +00:00
|
|
|
|a| if a.contains('a') {
|
2015-09-08 12:53:31 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(String::from("string does not contain at least one a"))
|
|
|
|
}
|
|
|
|
} "Test if the argument contains an a")
|
|
|
|
)
|
|
|
|
).get_matches();
|
|
|
|
|
|
|
|
// You can check the value provided by positional arguments, or option arguments
|
|
|
|
if let Some(o) = matches.value_of("output") {
|
|
|
|
println!("Value for output: {}", o);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(c) = matches.value_of("config") {
|
|
|
|
println!("Value for config: {}", c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can see how many times a particular flag or argument occurred
|
|
|
|
// Note, only flags can have multiple occurrences
|
|
|
|
match matches.occurrences_of("debug") {
|
|
|
|
0 => println!("Debug mode is off"),
|
|
|
|
1 => println!("Debug mode is kind of on"),
|
|
|
|
2 => println!("Debug mode is on"),
|
2020-05-02 20:26:19 +00:00
|
|
|
_ => println!("Don't be crazy"),
|
2015-09-08 12:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// You can check for the existence of subcommands, and if found use their
|
|
|
|
// matches just as you would the top level app
|
|
|
|
if let Some(ref matches) = matches.subcommand_matches("test") {
|
|
|
|
// "$ myapp test" was run
|
|
|
|
if matches.is_present("list") {
|
|
|
|
// "$ myapp test -l" was run
|
|
|
|
println!("Printing testing lists...");
|
|
|
|
} else {
|
|
|
|
println!("Not printing testing lists...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continued program logic goes here...
|
|
|
|
}
|