aboutsummaryrefslogtreecommitdiff
path: root/clap/examples/18_builder_macro.rs
blob: 6bdce47d330a0a38a3f93e8987d63ef0f561c0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#[macro_use]
extern crate clap;

// Note, there isn't a need for "use clap::{ ... };" Because the clap_app! macro uses
// $crate:: internally

fn main() {

    // Validation example testing that a file exists
    let file_exists = |path| {
        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
    // required to access it. Recommend `fn clap() -> Clap::SubCommand`.
    let external_sub_command = clap_app!( @subcommand foo =>
        (@arg bar: -b "Bar")
    );

    let matches = clap_app!(MyApp =>
        (@setting SubcommandRequiredElseHelp)
        (version: "1.0")
        (author: "Alice")
        (about: "Does awesome things")
        (@arg config: -c --config <conf> #{1, 2} {file_exists} "Sets a custom config file")
        (@arg proxyHostname: --("proxy-hostname") +takes_value "Sets the hostname of the proxy to use")
        (@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 {
                    |a| if a.contains('a') {
                        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"),
        3 | _ => println!("Don't be crazy"),
    }

    // 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(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...
}