2024-05-10 15:55:24 +02:00
|
|
|
//! # 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;
|
|
|
|
|
2024-05-12 01:02:55 +02:00
|
|
|
mod format;
|
|
|
|
use format::*;
|
2024-05-10 15:55:24 +02:00
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[clap(author, version, about, long_about = None)]
|
2024-05-12 18:51:18 +02:00
|
|
|
#[command(
|
|
|
|
author,
|
|
|
|
version,
|
|
|
|
about,
|
|
|
|
long_about,
|
|
|
|
help_template = r#"{about-section}
|
|
|
|
{usage-heading} {usage}
|
|
|
|
{all-args}{tab}
|
|
|
|
|
|
|
|
{name}: {version}
|
|
|
|
Author: {author-with-newline}
|
|
|
|
"#
|
|
|
|
)]
|
2024-05-10 15:55:24 +02:00
|
|
|
#[clap(group(
|
|
|
|
ArgGroup::new("format")
|
2024-05-12 01:55:56 +02:00
|
|
|
.args(&["hex", "bin", "oct", "dec", "base64", "base32"]),
|
2024-05-10 15:55:24 +02:00
|
|
|
))]
|
|
|
|
struct Cli {
|
|
|
|
#[arg(short, long)]
|
|
|
|
/// add a prefix (like "0x" for hex)
|
|
|
|
prefix: bool,
|
2024-05-12 19:33:48 +02:00
|
|
|
#[arg(short = 'P', long)]
|
|
|
|
/// add a padding to make the number at least one byte long
|
|
|
|
///
|
|
|
|
/// For example, `0b1100` will be `0b00001100` with this.
|
|
|
|
/// This does not apply to all formats, only hexadecimal and binary.
|
|
|
|
padding: bool,
|
2024-05-10 15:59:57 +02:00
|
|
|
#[arg(short = 'x', long, default_value_t = true)]
|
2024-05-10 15:55:24 +02:00
|
|
|
/// 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,
|
2024-05-12 01:49:11 +02:00
|
|
|
#[arg(short = 's', long)]
|
|
|
|
/// format to base64
|
|
|
|
base64: bool,
|
2024-05-12 01:55:56 +02:00
|
|
|
#[arg(short = 'z', long)]
|
|
|
|
/// format to base32
|
|
|
|
base32: bool,
|
2024-05-12 01:05:48 +02:00
|
|
|
#[clap(value_parser=maybe_hex::<Num>, required=true)]
|
2024-05-10 15:55:24 +02:00
|
|
|
/// at least one number that should be formatted
|
|
|
|
///
|
|
|
|
/// supports either base 10 or base 16 inputs (with 0xaaaa)
|
|
|
|
numbers: Vec<Num>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Cli {
|
|
|
|
fn format(&self) -> Format {
|
|
|
|
if self.oct {
|
|
|
|
Format::Octal
|
|
|
|
} else if self.bin {
|
|
|
|
Format::Bin
|
|
|
|
} else if self.dec {
|
|
|
|
Format::Dec
|
2024-05-12 01:49:11 +02:00
|
|
|
} else if self.base64 {
|
|
|
|
Format::Base64
|
2024-05-12 01:55:56 +02:00
|
|
|
} else if self.base32 {
|
|
|
|
Format::Base32
|
2024-05-10 15:59:57 +02:00
|
|
|
} else if self.hex {
|
2024-05-10 15:55:24 +02:00
|
|
|
Format::Hex
|
2024-05-10 15:59:57 +02:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
2024-05-10 15:55:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 13:57:36 +02:00
|
|
|
fn main() {
|
2024-05-10 15:55:24 +02:00
|
|
|
let cli = Cli::parse();
|
2024-05-12 19:33:48 +02:00
|
|
|
let options: FormatOptions = FormatOptions::default()
|
|
|
|
.padding(cli.padding)
|
|
|
|
.prefix(cli.prefix);
|
2024-05-10 15:55:24 +02:00
|
|
|
|
|
|
|
let mut out: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
for num in &cli.numbers {
|
2024-05-12 19:33:48 +02:00
|
|
|
out.push(cli.format().format(*num, options));
|
2024-05-10 15:55:24 +02:00
|
|
|
}
|
|
|
|
for o in out {
|
|
|
|
println!("{o}")
|
|
|
|
}
|
|
|
|
}
|