Skip to content

Commit

Permalink
Merge pull request #40 from jgardona/feat/hexa-units
Browse files Browse the repository at this point in the history
Hexadecimal Units
  • Loading branch information
jgardona committed Nov 29, 2023
2 parents 1bb3a34 + ca78145 commit 5150d2d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cli.rs
Expand Up @@ -25,7 +25,8 @@ struct Cli {
/// Read `N` bytes from the input. None for full read. The `N`
/// argument can be a unit with a decimal prefix(kb, mb).
/// Examples: --length 3kb, -l3kb, --length 1mb...
/// N unis are kb(1000), K(1024), mb(1000 * 1000) and M(1024 * 1024).
/// N unis are kb(1000), K(1024), mb(1000 * 1000) M(1024 * 1024),
/// and a prefix 0x for hexadecimal, `0x0a`.
#[arg(value_name = "N", short, long, verbatim_doc_comment)]
length: Option<String>,

Expand Down Expand Up @@ -67,6 +68,9 @@ fn parse_unit(input: &str) -> std::result::Result<usize, ParseIntError> {
let mut value = suffix.parse::<usize>()?;
value *= 1024 * 1024;
Ok(value)
} else if let Some(prefix) = input.strip_prefix("0x") {
let value = usize::from_str_radix(prefix, 16)?;
Ok(value)
} else {
input.parse::<usize>()
}
Expand Down Expand Up @@ -107,6 +111,14 @@ mod test_cli {
let result = parse_unit("1mb")?;
assert_eq!(expected, result);

let expected = 10;
let result = parse_unit("0x0a")?;
assert_eq!(expected, result);

let expected = 1024;
let result = parse_unit("0x400")?;
assert_eq!(expected, result);

Ok(())
}
}

0 comments on commit 5150d2d

Please sign in to comment.