2022-08-01 14:02:52 +00:00
|
|
|
use clap::error::ErrorKind;
|
|
|
|
use clap::{CommandFactory, Parser};
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(author, version, about, long_about = None)]
|
2021-11-30 18:30:19 +00:00
|
|
|
struct Cli {
|
|
|
|
/// set version manually
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, value_name = "VER")]
|
2021-11-30 18:30:19 +00:00
|
|
|
set_ver: Option<String>,
|
|
|
|
|
|
|
|
/// auto inc major
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-30 18:30:19 +00:00
|
|
|
major: bool,
|
|
|
|
|
|
|
|
/// auto inc minor
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-30 18:30:19 +00:00
|
|
|
minor: bool,
|
|
|
|
|
|
|
|
/// auto inc patch
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-30 18:30:19 +00:00
|
|
|
patch: bool,
|
|
|
|
|
|
|
|
/// some regular input
|
|
|
|
input_file: Option<String>,
|
|
|
|
|
|
|
|
/// some special input argument
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long)]
|
2021-11-30 18:30:19 +00:00
|
|
|
spec_in: Option<String>,
|
|
|
|
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short)]
|
2021-11-30 18:30:19 +00:00
|
|
|
config: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
|
|
// Let's assume the old version 1.2.3
|
|
|
|
let mut major = 1;
|
|
|
|
let mut minor = 2;
|
|
|
|
let mut patch = 3;
|
|
|
|
|
|
|
|
// See if --set-ver was used to set the version manually
|
|
|
|
let version = if let Some(ver) = cli.set_ver.as_deref() {
|
|
|
|
if cli.major || cli.minor || cli.patch {
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Cli::command();
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.error(
|
2021-11-30 18:30:19 +00:00
|
|
|
ErrorKind::ArgumentConflict,
|
|
|
|
"Can't do relative and absolute version change",
|
|
|
|
)
|
|
|
|
.exit();
|
|
|
|
}
|
|
|
|
ver.to_string()
|
|
|
|
} else {
|
|
|
|
// Increment the one requested (in a real program, we'd reset the lower numbers)
|
|
|
|
let (maj, min, pat) = (cli.major, cli.minor, cli.patch);
|
|
|
|
match (maj, min, pat) {
|
|
|
|
(true, false, false) => major += 1,
|
|
|
|
(false, true, false) => minor += 1,
|
|
|
|
(false, false, true) => patch += 1,
|
|
|
|
_ => {
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Cli::command();
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.error(
|
2021-11-30 18:30:19 +00:00
|
|
|
ErrorKind::ArgumentConflict,
|
2022-03-02 05:19:14 +00:00
|
|
|
"Can only modify one version field",
|
2021-11-30 18:30:19 +00:00
|
|
|
)
|
|
|
|
.exit();
|
|
|
|
}
|
|
|
|
};
|
2023-01-29 19:14:47 +00:00
|
|
|
format!("{major}.{minor}.{patch}")
|
2021-11-30 18:30:19 +00:00
|
|
|
};
|
|
|
|
|
2023-01-29 19:14:47 +00:00
|
|
|
println!("Version: {version}");
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
// Check for usage of -c
|
|
|
|
if let Some(config) = cli.config.as_deref() {
|
|
|
|
let input = cli
|
|
|
|
.input_file
|
|
|
|
.as_deref()
|
2022-01-04 07:24:36 +00:00
|
|
|
// 'or' is preferred to 'or_else' here since `Option::as_deref` is 'const'
|
|
|
|
.or(cli.spec_in.as_deref())
|
2021-11-30 18:30:19 +00:00
|
|
|
.unwrap_or_else(|| {
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Cli::command();
|
2022-02-14 21:47:20 +00:00
|
|
|
cmd.error(
|
2021-11-30 18:30:19 +00:00
|
|
|
ErrorKind::MissingRequiredArgument,
|
|
|
|
"INPUT_FILE or --spec-in is required when using --config",
|
|
|
|
)
|
|
|
|
.exit()
|
|
|
|
});
|
2023-01-29 19:14:47 +00:00
|
|
|
println!("Doing work using input {input} and config {config}");
|
2021-11-30 18:30:19 +00:00
|
|
|
}
|
|
|
|
}
|