Finish the argument parsing

This commit is contained in:
Gwilym Kuiper 2022-10-01 20:30:45 +01:00
parent 57772af416
commit d8ca41ec3d
2 changed files with 37 additions and 3 deletions

View file

@ -4,6 +4,12 @@ use clap::Command;
mod publish; mod publish;
mod release; mod release;
#[derive(Debug)]
pub enum Error {
PublishError(publish::Error),
ReleaseError(release::Error),
}
fn cli() -> Command { fn cli() -> Command {
Command::new("Agb tools") Command::new("Agb tools")
.subcommand_required(true) .subcommand_required(true)
@ -16,8 +22,14 @@ fn main() {
let matches = cli().get_matches(); let matches = cli().get_matches();
let result = match matches.subcommand() { let result = match matches.subcommand() {
Some(("publish", arg_matches)) => publish::publish(arg_matches), Some(("publish", arg_matches)) => {
Some(("release", arg_matches)) => todo!(), 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`"), _ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"),
}; };

View file

@ -3,11 +3,31 @@ pub fn command() -> clap::Command {
.about("Prepares and commits the changes required to release agb") .about("Prepares and commits the changes required to release agb")
.arg( .arg(
clap::Arg::new("version") clap::Arg::new("version")
.required(true)
.help("New version to release") .help("New version to release")
.value_parser(version_parser), .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::<bool>("Dry run").expect("defined by clap");
let version = matches
.get_one::<Version>("version")
.expect("defined by clap");
println!("dry run: {}, version: {:?}", dry_run, version);
todo!()
}
#[derive(Debug)]
pub enum Error {}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
struct Version { struct Version {
major: u32, major: u32,
@ -52,7 +72,9 @@ impl std::str::FromStr for Version {
} }
fn version_parser(maybe_version: &str) -> Result<Version, &'static str> { fn version_parser(maybe_version: &str) -> Result<Version, &'static str> {
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)] #[cfg(test)]