From e96bb48e4df371e49d27bb7d93cf5393838d8137 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 10 May 2024 15:55:24 +0200 Subject: [PATCH] first working version --- Cargo.toml | 13 +++-- README.md | 5 +- src/main.rs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 150 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0543e1f..9f77ac8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,16 +1,19 @@ [package] -name = "template" +name = "numf" version = "0.1.0" edition = "2021" publish = false authors = ["Christoph J. Scherr "] license = "MIT" -description = "No description yet" +description = "Convert numbes between formats" readme = "README.md" -homepage = "https://git.cscherr.de/PlexSheep/rs-base" -repository = "https://git.cscherr.de/PlexSheep/rs-base" -keywords = ["template"] +homepage = "https://git.cscherr.de/PlexSheep/numf" +repository = "https://git.cscherr.de/PlexSheep/numf" +keywords = ["cli"] [dependencies] +anyhow = "1.0.83" +clap = { version = "4.5.4", features = ["derive"] } +clap-num = "1.1.1" diff --git a/README.md b/README.md index a27185f..b6a058b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# rs-base +# numf -Base repository for rust projects \ No newline at end of file +This is just a trivial program that format's it's arguments in binary, hex or +octal. diff --git a/src/main.rs b/src/main.rs index e7a11a9..a1e8414 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,140 @@ -fn main() { - println!("Hello, world!"); +//! # numf +//! +//! This binary should just take any amount of numbers and print them out formatted to some other +//! system. + +use clap::{ArgGroup, Parser}; +use clap_num::maybe_hex; +use std::process::exit; + +pub type Num = usize; + +#[derive(Copy, Clone, Debug)] +enum Format { + Dec, + Hex, + Bin, + Octal, +} + +impl Format { + fn prefix(&self) -> String { + match self { + Format::Dec => "0d", + Format::Hex => "0x", + Format::Bin => "0b", + Format::Octal => "0o", + } + .to_string() + } + fn format(&self, num: Num, prefix: bool) -> String { + let mut buf = String::new(); + if prefix { + buf += &self.prefix(); + } + match self { + Format::Hex => { + buf += &format!("{num:X}"); + } + Format::Bin => { + buf += &format!("{num:b}"); + } + Format::Octal => { + buf += &format!("{num:o}"); + } + Format::Dec => { + buf += &format!("{num}"); + } + } + buf + } +} + +#[derive(Parser, Debug)] +#[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)] + /// format to hexadecimal + hex: bool, + #[arg(short, long)] + /// format to binary + bin: bool, + #[arg(short, long)] + /// format to decimal + dec: bool, + #[arg(short, long)] + /// format to octal + oct: bool, + #[clap(value_parser=maybe_hex::)] + /// at least one number that should be formatted + /// + /// supports either base 10 or base 16 inputs (with 0xaaaa) + numbers: Vec, +} + +impl Cli { + fn format(&self) -> Format { + if self.oct { + Format::Octal + } else if self.bin { + Format::Bin + } else if self.dec { + Format::Dec + } else { + Format::Hex + } + } +} + +fn main() { + match formatter() { + Ok(_) => (), + Err(_err) => usage(), + } +} + +fn formatter() -> anyhow::Result<()> { + let cli = Cli::parse(); + + let mut out: Vec = Vec::new(); + + for num in &cli.numbers { + out.push(cli.format().format(*num, cli.prefix)); + } + for o in out { + println!("{o}") + } + + 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); }