mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
hashsum: add --no-names option from official b3sum tool (#3361)
* hashsum: add --no-names option from official b3sum tool The official b3sum tool has a --no-names option for only printing the hashes, omitting the filenames. This is quite handy when used from scripts because it spares the postprocessing with "cut" or "awk". Since the installed b3sum symlink would also serve as a drop-in for the official tool, the --no-names option is expected to exist for compatibility. Add a --no-names option not only for b3sum but for hashsum in general (and maybe GNU coreutils will also feel inspired to add this option). Closes https://github.com/uutils/coreutils/issues/3360
This commit is contained in:
parent
bc7c9c1659
commit
e894e40c56
2 changed files with 22 additions and 2 deletions
|
@ -7,7 +7,7 @@
|
||||||
// * For the full copyright and license information, please view the LICENSE
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
|
|
||||||
// spell-checker:ignore (ToDO) algo, algoname, regexes, nread
|
// spell-checker:ignore (ToDO) algo, algoname, regexes, nread, nonames
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
@ -46,6 +46,7 @@ struct Options {
|
||||||
binary: bool,
|
binary: bool,
|
||||||
check: bool,
|
check: bool,
|
||||||
tag: bool,
|
tag: bool,
|
||||||
|
nonames: bool,
|
||||||
status: bool,
|
status: bool,
|
||||||
quiet: bool,
|
quiet: bool,
|
||||||
strict: bool,
|
strict: bool,
|
||||||
|
@ -316,6 +317,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
|
||||||
};
|
};
|
||||||
let check = matches.is_present("check");
|
let check = matches.is_present("check");
|
||||||
let tag = matches.is_present("tag");
|
let tag = matches.is_present("tag");
|
||||||
|
let nonames = matches.is_present("no-names");
|
||||||
let status = matches.is_present("status");
|
let status = matches.is_present("status");
|
||||||
let quiet = matches.is_present("quiet") || status;
|
let quiet = matches.is_present("quiet") || status;
|
||||||
let strict = matches.is_present("strict");
|
let strict = matches.is_present("strict");
|
||||||
|
@ -328,6 +330,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
|
||||||
binary,
|
binary,
|
||||||
check,
|
check,
|
||||||
tag,
|
tag,
|
||||||
|
nonames,
|
||||||
status,
|
status,
|
||||||
quiet,
|
quiet,
|
||||||
strict,
|
strict,
|
||||||
|
@ -370,6 +373,11 @@ pub fn uu_app_common<'a>() -> Command<'a> {
|
||||||
.long("tag")
|
.long("tag")
|
||||||
.help("create a BSD-style checksum"),
|
.help("create a BSD-style checksum"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("no-names")
|
||||||
|
.long("no-names")
|
||||||
|
.help("Omits filenames in the output (option not present in GNU/Coreutils)"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("text")
|
Arg::new("text")
|
||||||
.short('t')
|
.short('t')
|
||||||
|
@ -602,6 +610,8 @@ where
|
||||||
.map_err_context(|| "failed to read input".to_string())?;
|
.map_err_context(|| "failed to read input".to_string())?;
|
||||||
if options.tag {
|
if options.tag {
|
||||||
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
|
println!("{} ({}) = {}", options.algoname, filename.display(), sum);
|
||||||
|
} else if options.nonames {
|
||||||
|
println!("{}", sum);
|
||||||
} else {
|
} else {
|
||||||
println!("{} {}{}", sum, binary_marker, filename.display());
|
println!("{} {}{}", sum, binary_marker, filename.display());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// spell-checker:ignore checkfile
|
// spell-checker:ignore checkfile, nonames
|
||||||
macro_rules! get_hash(
|
macro_rules! get_hash(
|
||||||
($str:expr) => (
|
($str:expr) => (
|
||||||
$str.split(' ').collect::<Vec<&str>>()[0]
|
$str.split(' ').collect::<Vec<&str>>()[0]
|
||||||
|
@ -29,6 +29,16 @@ macro_rules! test_digest {
|
||||||
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str()));
|
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_nonames() {
|
||||||
|
let ts = TestScenario::new("hashsum");
|
||||||
|
// EXPECTED_FILE has no newline character at the end
|
||||||
|
assert_eq!(format!("{0}\n{0}\n", ts.fixtures.read(EXPECTED_FILE)),
|
||||||
|
ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--no-names").arg("input.txt").arg("-").pipe_in_fixture("input.txt")
|
||||||
|
.succeeds().no_stderr().stdout_str()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_check() {
|
fn test_check() {
|
||||||
let ts = TestScenario::new("hashsum");
|
let ts = TestScenario::new("hashsum");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue