From d8ca41ec3d7e346d0e818918a89ecdda40c29ae3 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 1 Oct 2022 20:30:45 +0100 Subject: [PATCH] Finish the argument parsing --- tools/src/main.rs | 16 ++++++++++++++-- tools/src/release.rs | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/tools/src/main.rs b/tools/src/main.rs index c0b13de7..4df25fdd 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -4,6 +4,12 @@ use clap::Command; mod publish; mod release; +#[derive(Debug)] +pub enum Error { + PublishError(publish::Error), + ReleaseError(release::Error), +} + fn cli() -> Command { Command::new("Agb tools") .subcommand_required(true) @@ -16,8 +22,14 @@ fn main() { let matches = cli().get_matches(); let result = match matches.subcommand() { - Some(("publish", arg_matches)) => publish::publish(arg_matches), - Some(("release", arg_matches)) => todo!(), + Some(("publish", arg_matches)) => { + publish::publish(arg_matches).map_err(Error::PublishError) + } + + Some(("release", arg_matches)) => { + release::release(arg_matches).map_err(Error::ReleaseError) + } + _ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"), }; diff --git a/tools/src/release.rs b/tools/src/release.rs index 0cd04340..a0c52df2 100644 --- a/tools/src/release.rs +++ b/tools/src/release.rs @@ -3,11 +3,31 @@ pub fn command() -> clap::Command { .about("Prepares and commits the changes required to release agb") .arg( clap::Arg::new("version") + .required(true) .help("New version to release") .value_parser(version_parser), ) + .arg( + clap::Arg::new("Dry run") + .long("dry-run") + .help("Don't do anything with git (but does everything else)") + .action(clap::ArgAction::SetTrue), + ) } +pub fn release(matches: &clap::ArgMatches) -> Result<(), Error> { + let dry_run = matches.get_one::("Dry run").expect("defined by clap"); + let version = matches + .get_one::("version") + .expect("defined by clap"); + + println!("dry run: {}, version: {:?}", dry_run, version); + todo!() +} + +#[derive(Debug)] +pub enum Error {} + #[derive(Debug, Clone, PartialEq, Eq)] struct Version { major: u32, @@ -52,7 +72,9 @@ impl std::str::FromStr for Version { } fn version_parser(maybe_version: &str) -> Result { - maybe_version.parse().map_err(|_| "Failed to parse version") + maybe_version + .parse() + .map_err(|_| "Failed to parse version, must be of the format x.y.z") } #[cfg(test)]