From 348613cbf83747c5f4fbadd6e3bc7c7558cdaf05 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 10 May 2024 15:59:57 +0200 Subject: [PATCH] fix cli interface to not need a format flag --- src/main.rs | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index a1e8414..d4958df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,14 +54,13 @@ impl Format { #[clap(author, version, about, long_about = None)] #[clap(group( ArgGroup::new("format") - .required(true) .args(&["hex", "bin", "oct", "dec"]), ))] struct Cli { #[arg(short, long)] /// add a prefix (like "0x" for hex) prefix: bool, - #[arg(short = 'x', long)] + #[arg(short = 'x', long, default_value_t = true)] /// format to hexadecimal hex: bool, #[arg(short, long)] @@ -88,17 +87,16 @@ impl Cli { Format::Bin } else if self.dec { Format::Dec - } else { + } else if self.hex { Format::Hex + } else { + unreachable!() } } } fn main() { - match formatter() { - Ok(_) => (), - Err(_err) => usage(), - } + let _ = formatter(); } fn formatter() -> anyhow::Result<()> { @@ -115,26 +113,3 @@ fn formatter() -> anyhow::Result<()> { Ok(()) } - -fn help() { - let help: String = format!( - r##"numf {} - - -h, --help print this help - -p, --prefix print a prefix before the formatted number - -b, --binary format to binary - -o, --octal format to octal - -x, --hex format to hex (default) - - Author: Christoph J. Scherr 2024 - "##, - env!("CARGO_PKG_VERSION") - ); - println!("{help}"); - exit(1); -} - -fn usage() { - println!("Usage: numf -hpbox NUMBER\n"); - exit(1); -}