• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use clap::{ArgGroup, Parser};
2 
3 #[derive(Parser)]
4 #[command(author, version, about, long_about = None)]
5 #[command(group(
6             ArgGroup::new("vers")
7                 .required(true)
8                 .args(["set_ver", "major", "minor", "patch"]),
9         ))]
10 struct Cli {
11     /// set version manually
12     #[arg(long, value_name = "VER")]
13     set_ver: Option<String>,
14 
15     /// auto inc major
16     #[arg(long)]
17     major: bool,
18 
19     /// auto inc minor
20     #[arg(long)]
21     minor: bool,
22 
23     /// auto inc patch
24     #[arg(long)]
25     patch: bool,
26 
27     /// some regular input
28     #[arg(group = "input")]
29     input_file: Option<String>,
30 
31     /// some special input argument
32     #[arg(long, group = "input")]
33     spec_in: Option<String>,
34 
35     #[arg(short, requires = "input")]
36     config: Option<String>,
37 }
38 
main()39 fn main() {
40     let cli = Cli::parse();
41 
42     // Let's assume the old version 1.2.3
43     let mut major = 1;
44     let mut minor = 2;
45     let mut patch = 3;
46 
47     // See if --set_ver was used to set the version manually
48     let version = if let Some(ver) = cli.set_ver.as_deref() {
49         ver.to_string()
50     } else {
51         // Increment the one requested (in a real program, we'd reset the lower numbers)
52         let (maj, min, pat) = (cli.major, cli.minor, cli.patch);
53         match (maj, min, pat) {
54             (true, _, _) => major += 1,
55             (_, true, _) => minor += 1,
56             (_, _, true) => patch += 1,
57             _ => unreachable!(),
58         };
59         format!("{major}.{minor}.{patch}")
60     };
61 
62     println!("Version: {version}");
63 
64     // Check for usage of -c
65     if let Some(config) = cli.config.as_deref() {
66         let input = cli
67             .input_file
68             .as_deref()
69             .unwrap_or_else(|| cli.spec_in.as_deref().unwrap());
70         println!("Doing work using input {input} and config {config}");
71     }
72 }
73