From e6000a56f4ad5cfa5104485a060402ce43c931ee Mon Sep 17 00:00:00 2001 From: Sharif Haason Date: Sun, 27 Nov 2022 17:18:38 -0500 Subject: [PATCH] Parse `-b` argument --- src/bin/hexyl.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index da52f6e..1b9dcf5 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -19,7 +19,7 @@ use thiserror::Error as ThisError; use terminal_size::terminal_size; -use hexyl::{BorderStyle, Input, PrinterBuilder}; +use hexyl::{Base, BorderStyle, Input, PrinterBuilder}; const DEFAULT_BLOCK_SIZE: i64 = 512; @@ -336,6 +336,37 @@ fn run() -> Result<(), AnyhowError> { } }; + let base = if let Some(base) = matches.get_one::("base") + .map(|s| { + if let Ok(base_num) = s.parse::() { + match base_num { + 2 => Ok(Base::Binary), + 8 => Ok(Base::Octal), + 10 => Ok(Base::Decimal), + 16 => Ok(Base::Hexadecimal), + _ => Err(anyhow!("The number provided is not a valid base. Valid bases are 2, 8, 10, and 16.")), + } + } else { + match s.as_str() { + "b" | "bin" | "binary" => Ok(Base::Binary), + "o" | "oct" | "octal" => Ok(Base::Octal), + "d" | "dec" | "decimal" => Ok(Base::Decimal), + "x" | "hex" | "hexadecimal" => Ok(Base::Hexadecimal), + _ => Err(anyhow!("The base provided is not valid. Valid bases are \"b\", \"o\", \"d\", and \"x\".")) + } + } + }).transpose()? { + base + } else { + Base::Hexadecimal + }; + + let base_digits = match base { + Base::Binary => 8, + Base::Octal => 3, + Base::Decimal => 3, + Base::Hexadecimal => 2, + }; let group_bytes = if let Some(group_bytes) = matches .get_one::("group_bytes")