1 use clap::{app_from_crate, arg, ArgGroup};
2 
main()3 fn main() {
4     // Create application like normal
5     let matches = app_from_crate!()
6         // Add the version arguments
7         .arg(arg!(--"set-ver" <VER> "set version manually").required(false))
8         .arg(arg!(--major         "auto inc major"))
9         .arg(arg!(--minor         "auto inc minor"))
10         .arg(arg!(--patch         "auto inc patch"))
11         // Create a group, make it required, and add the above arguments
12         .group(
13             ArgGroup::new("vers")
14                 .required(true)
15                 .args(&["set-ver", "major", "minor", "patch"]),
16         )
17         // Arguments can also be added to a group individually, these two arguments
18         // are part of the "input" group which is not required
19         .arg(arg!([INPUT_FILE] "some regular input").group("input"))
20         .arg(
21             arg!(--"spec-in" <SPEC_IN> "some special input argument")
22                 .required(false)
23                 .group("input"),
24         )
25         // Now let's assume we have a -c [config] argument which requires one of
26         // (but **not** both) the "input" arguments
27         .arg(arg!(config: -c <CONFIG>).required(false).requires("input"))
28         .get_matches();
29 
30     // Let's assume the old version 1.2.3
31     let mut major = 1;
32     let mut minor = 2;
33     let mut patch = 3;
34 
35     // See if --set-ver was used to set the version manually
36     let version = if let Some(ver) = matches.value_of("set-ver") {
37         ver.to_string()
38     } else {
39         // Increment the one requested (in a real program, we'd reset the lower numbers)
40         let (maj, min, pat) = (
41             matches.is_present("major"),
42             matches.is_present("minor"),
43             matches.is_present("patch"),
44         );
45         match (maj, min, pat) {
46             (true, _, _) => major += 1,
47             (_, true, _) => minor += 1,
48             (_, _, true) => patch += 1,
49             _ => unreachable!(),
50         };
51         format!("{}.{}.{}", major, minor, patch)
52     };
53 
54     println!("Version: {}", version);
55 
56     // Check for usage of -c
57     if matches.is_present("config") {
58         let input = matches
59             .value_of("INPUT_FILE")
60             .unwrap_or_else(|| matches.value_of("spec-in").unwrap());
61         println!(
62             "Doing work using input {} and config {}",
63             input,
64             matches.value_of("config").unwrap()
65         );
66     }
67 }
68