]>
Commit | Line | Data |
---|---|---|
04454e1e FG |
1 | use clap::{CommandFactory, ErrorKind, Parser}; |
2 | ||
3 | #[derive(Parser)] | |
4 | #[clap(author, version, about, long_about = None)] | |
5 | struct Cli { | |
6 | /// set version manually | |
923072b8 | 7 | #[clap(long, value_name = "VER", value_parser)] |
04454e1e FG |
8 | set_ver: Option<String>, |
9 | ||
10 | /// auto inc major | |
923072b8 | 11 | #[clap(long, action)] |
04454e1e FG |
12 | major: bool, |
13 | ||
14 | /// auto inc minor | |
923072b8 | 15 | #[clap(long, action)] |
04454e1e FG |
16 | minor: bool, |
17 | ||
18 | /// auto inc patch | |
923072b8 | 19 | #[clap(long, action)] |
04454e1e FG |
20 | patch: bool, |
21 | ||
22 | /// some regular input | |
923072b8 | 23 | #[clap(value_parser)] |
04454e1e FG |
24 | input_file: Option<String>, |
25 | ||
26 | /// some special input argument | |
923072b8 | 27 | #[clap(long, value_parser)] |
04454e1e FG |
28 | spec_in: Option<String>, |
29 | ||
923072b8 | 30 | #[clap(short, value_parser)] |
04454e1e FG |
31 | config: Option<String>, |
32 | } | |
33 | ||
34 | fn main() { | |
35 | let cli = Cli::parse(); | |
36 | ||
37 | // Let's assume the old version 1.2.3 | |
38 | let mut major = 1; | |
39 | let mut minor = 2; | |
40 | let mut patch = 3; | |
41 | ||
42 | // See if --set-ver was used to set the version manually | |
43 | let version = if let Some(ver) = cli.set_ver.as_deref() { | |
44 | if cli.major || cli.minor || cli.patch { | |
45 | let mut cmd = Cli::command(); | |
46 | cmd.error( | |
47 | ErrorKind::ArgumentConflict, | |
48 | "Can't do relative and absolute version change", | |
49 | ) | |
50 | .exit(); | |
51 | } | |
52 | ver.to_string() | |
53 | } else { | |
54 | // Increment the one requested (in a real program, we'd reset the lower numbers) | |
55 | let (maj, min, pat) = (cli.major, cli.minor, cli.patch); | |
56 | match (maj, min, pat) { | |
57 | (true, false, false) => major += 1, | |
58 | (false, true, false) => minor += 1, | |
59 | (false, false, true) => patch += 1, | |
60 | _ => { | |
61 | let mut cmd = Cli::command(); | |
62 | cmd.error( | |
63 | ErrorKind::ArgumentConflict, | |
923072b8 | 64 | "Can only modify one version field", |
04454e1e FG |
65 | ) |
66 | .exit(); | |
67 | } | |
68 | }; | |
69 | format!("{}.{}.{}", major, minor, patch) | |
70 | }; | |
71 | ||
72 | println!("Version: {}", version); | |
73 | ||
74 | // Check for usage of -c | |
75 | if let Some(config) = cli.config.as_deref() { | |
76 | // todo: remove `#[allow(clippy::or_fun_call)]` lint when MSRV is bumped. | |
77 | #[allow(clippy::or_fun_call)] | |
78 | let input = cli | |
79 | .input_file | |
80 | .as_deref() | |
81 | // 'or' is preferred to 'or_else' here since `Option::as_deref` is 'const' | |
82 | .or(cli.spec_in.as_deref()) | |
83 | .unwrap_or_else(|| { | |
84 | let mut cmd = Cli::command(); | |
85 | cmd.error( | |
86 | ErrorKind::MissingRequiredArgument, | |
87 | "INPUT_FILE or --spec-in is required when using --config", | |
88 | ) | |
89 | .exit() | |
90 | }); | |
91 | println!("Doing work using input {} and config {}", input, config); | |
92 | } | |
93 | } |