mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
7e899cd340
This reverts commits 24cb8b1..d0abb37 from clap-rs/clap#1840 This is part of #16. clap-rs/clap#1840 wasn't the right call but we don't have time to make the decision now, so instead of having one option and changing it in 4.0, this reverts back to clap2 behavior.
61 lines
2.2 KiB
Rust
61 lines
2.2 KiB
Rust
use clap::{App, Arg};
|
|
|
|
fn main() {
|
|
// Once all App settings (including all arguments) have been set, you call get_matches() which
|
|
// parses the string provided by the user, and returns all the valid matches to the ones you
|
|
// specified.
|
|
//
|
|
// You can then query the matches struct to get information about how the user ran the program
|
|
// at startup.
|
|
//
|
|
// For this example, let's assume you created an App which accepts three arguments (plus two
|
|
// generated by clap), a flag to display debugging information triggered with "-d" or
|
|
// "--debug" as well as an option argument which specifies a custom configuration file to use
|
|
// triggered with "-c file" or "--config file" or "--config=file" and finally a positional
|
|
// argument which is the input file we want to work with, this will be the only required
|
|
// argument.
|
|
let matches = App::new("MyApp")
|
|
.about("Parses an input file to do awesome things")
|
|
.version("1.0")
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
|
.arg(
|
|
Arg::new("debug")
|
|
.help("turn on debugging information")
|
|
.short('d')
|
|
.long("debug"),
|
|
)
|
|
.arg(
|
|
Arg::new("config")
|
|
.help("sets the config file to use")
|
|
.short('c')
|
|
.long("config")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::new("input")
|
|
.help("the input file to use")
|
|
.index(1)
|
|
.required(true),
|
|
)
|
|
.get_matches();
|
|
|
|
// We can find out whether or not debugging was turned on
|
|
if matches.is_present("debug") {
|
|
println!("Debugging is turned on");
|
|
}
|
|
|
|
// If we wanted to do some custom initialization based off some configuration file
|
|
// provided by the user, we could get the file (A string of the file)
|
|
if let Some(ref file) = matches.value_of("config") {
|
|
println!("Using config file: {}", file);
|
|
}
|
|
|
|
println!(
|
|
"Doing real work with file: {}",
|
|
matches
|
|
.value_of("input")
|
|
.expect("'input' is required and parsing will fail if its missing")
|
|
);
|
|
|
|
// Continued program logic goes here...
|
|
}
|