From 4d4b0b7c4a33e9d691a924d2d1baada94c664bc6 Mon Sep 17 00:00:00 2001 From: "Kevin K." Date: Mon, 16 Mar 2015 10:55:45 -0400 Subject: [PATCH] Update README.md --- README.md | 97 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 1464db66..d2725599 100644 --- a/README.md +++ b/README.md @@ -11,61 +11,62 @@ When using `clap` you define a set of parameters and rules for your arguments an After defining a list of possible valid arguments and subcommands, `clap` gives you a list of valid matches that the user supplied at runtime, or informs the user of their error and exits gracefully. You can use this list to determine the functioning of your program. -# Example (Full example with comments in examples/myapp.rs) +## Quick Example ```rust +// (Full example with comments in examples/myapp.rs) extern crate clap; use clap::{Arg, App, SubCommand}; -// ... - -let matches = App::new("MyApp") - .version("1.0") - .author("Kevin K. ") - .about("Does awesome things") - .arg(Arg::new("config") - .short("c") - .long("config") - .help("Sets a custom config file") - .takes_value(true)) - .arg(Arg::new("output") - .help("Sets an optional output file") - .index(1)) - .arg(Arg::new("debug") - .short("d") - .multiple(true) - .help("Turn debugging information on")) - .subcommand(SubCommand::new("test") - .about("controls testing features") - .arg(Arg::new("verbose") - .short("v") - .help("print test information verbosely"))) - .get_matches(); - -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); -} - -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"), - 3 | _ => println!("Don't be crazy"), -} - -if let Some(ref matches) = matches.subcommand_matches("test") { - if matches.is_present("list") { - println!("Printing testing lists..."); - } else { - println!("Not printing testing lists..."); +fn main() { + let matches = App::new("MyApp") + .version("1.0") + .author("Kevin K. ") + .about("Does awesome things") + .arg(Arg::new("config") + .short("c") + .long("config") + .help("Sets a custom config file") + .takes_value(true)) + .arg(Arg::new("output") + .help("Sets an optional output file") + .index(1)) + .arg(Arg::new("debug") + .short("d") + .multiple(true) + .help("Turn debugging information on")) + .subcommand(SubCommand::new("test") + .about("controls testing features") + .arg(Arg::new("verbose") + .short("v") + .help("print test information verbosely"))) + .get_matches(); + + 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); + } + + 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"), + 3 | _ => println!("Don't be crazy"), + } + + if let Some(ref matches) = matches.subcommand_matches("test") { + if matches.is_present("list") { + println!("Printing testing lists..."); + } else { + println!("Not printing testing lists..."); + } + } + + // more porgram logic goes here... } - -// more porgram logic goes here... ``` If you were to compile the above program and run it with the flag `--help` or `-h` (or `help` subcommand, since we defined `test` as a subcommand) the following output woud be presented