diff options
author | RinHizakura <s921975628@gmail.com> | 2022-10-03 20:52:57 +0800 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2022-11-26 13:07:06 +0100 |
commit | c0f94ad6aa084c3c6be133a87c1c009d66ec9c56 (patch) | |
tree | 636bbf1769935d2cce935874b5aedd58dc8e3252 | |
parent | 55fed5e3c0b41c572f2ef36ff9678a5bf743d36b (diff) |
Refine the support of byte grouping
-rw-r--r-- | src/bin/hexyl.rs | 10 | ||||
-rw-r--r-- | tests/integration_tests.rs | 37 |
2 files changed, 30 insertions, 17 deletions
diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index bd49994..45df101 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -169,12 +169,12 @@ fn run() -> Result<(), AnyhowError> { .arg( Arg::new("group_bytes") .short('g') + .long("group-bytes") .num_args(1) .value_name("N") .help( "Sets the number of octets per group to be displayed. \ - The possible option will be 1, 2, 4, 8, otherwise it will be set \ - to 1 by default.", + The possible options are 1, 2, 4, 8. The default is 1", ), ) .arg( @@ -352,7 +352,7 @@ fn run() -> Result<(), AnyhowError> { .get_one::<String>("group_bytes") .map(|s| { s.parse::<NonZeroU8>().map(u8::from).context(anyhow!( - "failed to parse `-g` arg {:?} as unsigned nonzero integer", + "failed to parse `--group-bytes` arg {:?} as unsigned nonzero integer", s )) }) @@ -361,7 +361,9 @@ fn run() -> Result<(), AnyhowError> { if (group_bytes <= 8) && ((group_bytes & (group_bytes - 1)) == 0) { group_bytes } else { - 1 + return Err(anyhow!( + "the possible options of group_bytes could only be 1, 2, 4 or 8. " + )); } } else { 1 diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index a2befaf..dce506b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -280,13 +280,13 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("-g=2") + .arg("--group-bytes=2") .assert() .success() .stdout( "┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\ - │00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\ - └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", + │00000000│ 3031 3233 3435 3637 ┊ 3839 6162 6364 650a │01234567┊89abcde_│\n\ + └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", ); } @@ -295,13 +295,13 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("-g=4") + .arg("--group-bytes=4") .assert() .success() .stdout( "┌────────┬───────────────────┬───────────────────┬────────┬────────┐\n\ - │00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\ - └────────┴───────────────────┴───────────────────┴────────┴────────┘\n", + │00000000│ 30313233 34353637 ┊ 38396162 6364650a │01234567┊89abcde_│\n\ + └────────┴───────────────────┴───────────────────┴────────┴────────┘\n", ); } @@ -310,13 +310,13 @@ mod group { hexyl() .arg("ascii") .arg("--color=never") - .arg("-g=8") + .arg("--group-bytes=8") .assert() .success() .stdout( "┌────────┬──────────────────┬──────────────────┬────────┬────────┐\n\ - │00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\ - └────────┴──────────────────┴──────────────────┴────────┴────────┘\n", + │00000000│ 3031323334353637 ┊ 383961626364650a │01234567┊89abcde_│\n\ + └────────┴──────────────────┴──────────────────┴────────┴────────┘\n", ); } @@ -326,7 +326,7 @@ mod group { .arg("ascii") .arg("--color=never") .arg("--plain") - .arg("-g=2") + .arg("--group-bytes=2") .assert() .success() .stdout(" 3031 3233 3435 3637 3839 6162 6364 650a \n"); @@ -336,14 +336,25 @@ mod group { fn group_bytes_fill_space() { hexyl() .arg("--color=never") - .arg("-g=2") + .arg("--group-bytes=2") .write_stdin("abc") .assert() .success() .stdout( "┌────────┬─────────────────────┬─────────────────────┬────────┬────────┐\n\ - │00000000│ 6162 63 ┊ │abc ┊ │\n\ - └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", + │00000000│ 6162 63 ┊ │abc ┊ │\n\ + └────────┴─────────────────────┴─────────────────────┴────────┴────────┘\n", ); } + + #[test] + fn group_bytes_invalid() { + hexyl() + .arg("ascii") + .arg("--color=never") + .arg("--plain") + .arg("--group-bytes=3") + .assert() + .failure(); + } } |