1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Basename arguments simple format (#3736)

* basename: support simple format

* tests/basename: add tests for simple format

* basename: follow clippy advice
This commit is contained in:
Niyaz Nigmatullin 2022-07-22 14:28:54 +03:00 committed by GitHub
parent 9a1b4b537a
commit 5f3f1112d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View file

@ -31,6 +31,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
// Since options have to go before names,
// if the first argument is not an option, then there is no option,
// and that implies there is exactly one name (no option => no -a option),
// so simple format is used
if args.len() > 1 && !args[1].starts_with('-') {
if args.len() > 3 {
return Err(UUsageError::new(
1,
format!("extra operand {}", args[3].to_string().quote()),
));
}
let suffix = if args.len() > 2 { args[2].as_ref() } else { "" };
println!("{}", basename(&args[1], suffix));
return Ok(());
}
// //
// Argument parsing // Argument parsing
// //

View file

@ -174,3 +174,21 @@ fn test_triple_slash() {
let expected = if cfg!(windows) { "\\\n" } else { "/\n" }; let expected = if cfg!(windows) { "\\\n" } else { "/\n" };
new_ucmd!().arg("///").succeeds().stdout_is(expected); new_ucmd!().arg("///").succeeds().stdout_is(expected);
} }
#[test]
fn test_simple_format() {
new_ucmd!().args(&["a-a", "-a"]).succeeds().stdout_is("a\n");
new_ucmd!()
.args(&["a--help", "--help"])
.succeeds()
.stdout_is("a\n");
new_ucmd!().args(&["a-h", "-h"]).succeeds().stdout_is("a\n");
new_ucmd!().args(&["f.s", ".s"]).succeeds().stdout_is("f\n");
new_ucmd!().args(&["a-s", "-s"]).succeeds().stdout_is("a\n");
new_ucmd!().args(&["a-z", "-z"]).succeeds().stdout_is("a\n");
new_ucmd!()
.args(&["a", "b", "c"])
.fails()
.code_is(1)
.stderr_contains("extra operand 'c'");
}