@@ -6,39 +6,52 @@ This application describes the structure of its command-line interface using
66` clap ` 's builder style. The [ documentation] gives two other possible ways to
77instantiate an application.
88
9- In the builder style, ` with_name ` is the unique identifier that ` value_of ` will
10- use to retrieve the value passed. The ` short ` and ` long ` options control the
9+ In the builder style, each possible argument is described by an ` Arg `
10+ struct. The string given to ` Arg::new() ` is the internal
11+ name of the argument. The ` short ` and ` long ` options control the
1112flag the user will be expected to type; short flags look like ` -f ` and long
1213flags look like ` --file ` .
1314
15+ The ` get_one() ` method is used to get an argument's value.
16+ It returns ` Some(& ` value` ) ` if the argument was supplied by
17+ the user, else ` None ` .
18+
19+ The use of ` PathBuf ` is to allow file paths which are legal
20+ in Linux and MacOS, but not in Rust UTF-8 strings. This is
21+ best practice: one encounters such paths quite rarely in
22+ practice, but when it happens it is really frustrating
23+ without this.
24+
1425``` rust,edition2018
15- use clap::{Arg, App};
26+ use std::path::PathBuf;
27+
28+ use clap::{Arg, Command, builder::PathBufValueParser};
1629
1730fn main() {
18- let matches = App ::new("My Test Program")
31+ let matches = Command ::new("My Test Program")
1932 .version("0.1.0")
20- .author("Hackerman Jones <[email protected] >") 2133 .about("Teaches argument parsing")
22- .arg(Arg::with_name ("file")
23- .short("f" )
34+ .arg(Arg::new ("file")
35+ .short('f' )
2436 .long("file")
25- .takes_value(true )
26- .help("A cool file" ))
27- .arg(Arg::with_name ("num")
28- .short("n" )
37+ .help("A cool file" )
38+ .value_parser(PathBufValueParser::default() ))
39+ .arg(Arg::new ("num")
40+ .short('n' )
2941 .long("number")
30- .takes_value(true)
3142 .help("Five less than your favorite number"))
3243 .get_matches();
3344
34- let myfile = matches.value_of("file").unwrap_or("input.txt");
35- println!("The file passed is: {}", myfile);
45+ let default_file = PathBuf::from("input.txt");
46+ let myfile: &PathBuf = matches.get_one("file").unwrap_or(&default_file);
47+ println!("The file passed is: {}", myfile.display());
3648
37- let num_str = matches.value_of ("num");
49+ let num_str: Option<&String> = matches.get_one ("num");
3850 match num_str {
3951 None => println!("No idea what your favorite number is."),
4052 Some(s) => {
41- match s.parse::<i32>() {
53+ let parsed: Result<i32, _> = s.parse();
54+ match parsed {
4255 Ok(n) => println!("Your favorite number must be {}.", n + 5),
4356 Err(_) => println!("That's not a number! {}", s),
4457 }
@@ -47,24 +60,19 @@ fn main() {
4760}
4861```
4962
50- Usage information is generated by ` clap ` . The usage for the example application
51- looks like this.
63+ Usage information is generated by ` clap -h ` . The usage for
64+ the example application looks like this.
5265
5366```
54- My Test Program 0.1.0
55- Hackerman Jones <[email protected] > 5667Teaches argument parsing
5768
58- USAGE:
59- testing [OPTIONS]
60-
61- FLAGS:
62- -h, --help Prints help information
63- -V, --version Prints version information
69+ Usage: clap-cookbook [OPTIONS]
6470
65- OPTIONS:
66- -f, --file <file> A cool file
67- -n, --number <num> Five less than your favorite number
71+ Options:
72+ -f, --file <file> A cool file
73+ -n, --number <num> Five less than your favorite number
74+ -h, --help Print help
75+ -V, --version Print version
6876```
6977
7078We can test the application by running a command like the following.
0 commit comments