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

Merge pull request #1082 from flyrry/finish_basename

basename: add -a, -s and -z flags
This commit is contained in:
Alex Lyon 2017-10-09 11:33:06 -07:00 committed by GitHub
commit f2b952db54
2 changed files with 26 additions and 12 deletions

View file

@ -26,6 +26,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
// Argument parsing // Argument parsing
// //
let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP) let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP)
.optflag("a", "multiple", "Support more than one argument. Treat every argument as a name.")
.optopt("s", "suffix", "Remove a trailing suffix. This option implies the -a option.", "SUFFIX")
.optflag("z", "zero", "Output a zero byte (ASCII NUL) at the end of each line, rather than a newline.")
.parse(args); .parse(args);
// too few arguments // too few arguments
@ -37,8 +40,12 @@ pub fn uumain(args: Vec<String>) -> i32 {
"missing operand" "missing operand"
); );
} }
let opt_s = matches.opt_present("s");
let opt_a = matches.opt_present("a");
let opt_z = matches.opt_present("z");
let multiple_paths = opt_s || opt_a;
// too many arguments // too many arguments
else if matches.free.len() > 2 { if !multiple_paths && matches.free.len() > 2 {
crash!( crash!(
1, 1,
"{0}: extra operand '{1}'\nTry '{0} --help' for more information.", "{0}: extra operand '{1}'\nTry '{0} --help' for more information.",
@ -47,23 +54,33 @@ pub fn uumain(args: Vec<String>) -> i32 {
); );
} }
let suffix = if opt_s {
matches.opt_str("s").unwrap()
} else if !opt_a && matches.free.len() > 1 {
matches.free[1].clone()
} else {
"".to_owned()
};
// //
// Main Program Processing // Main Program Processing
// //
let mut name = strip_dir(&matches.free[0]); let paths = if multiple_paths {
&matches.free[..]
} else {
&matches.free[0..1]
};
if matches.free.len() > 1 { let line_ending = if opt_z { "\0" } else { "\n" };
let suffix = matches.free[1].clone(); for path in paths {
name = strip_suffix(name.as_ref(), suffix.as_ref()); print!("{}{}", basename(&path, &suffix), line_ending);
} }
println!("{}", name);
0 0
} }
fn strip_dir(fullname: &str) -> String { fn basename(fullname: &str, suffix: &str) -> String {
// Remove all platform-specific path separators from the end // Remove all platform-specific path separators from the end
let mut path: String = fullname.chars().rev().skip_while(|&ch| is_separator(ch)).collect(); let mut path: String = fullname.chars().rev().skip_while(|&ch| is_separator(ch)).collect();
@ -73,7 +90,7 @@ fn strip_dir(fullname: &str) -> String {
// Convert to path buffer and get last path component // Convert to path buffer and get last path component
let pb = PathBuf::from(path); let pb = PathBuf::from(path);
match pb.components().last() { match pb.components().last() {
Some(c) => c.as_os_str().to_str().unwrap().to_owned(), Some(c) => strip_suffix(c.as_os_str().to_str().unwrap(), suffix),
None => "".to_owned() None => "".to_owned()
} }
} }

View file

@ -23,7 +23,6 @@ fn test_dont_remove_suffix() {
new_ucmd!().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz"); new_ucmd!().args(&["/foo/bar/baz", "baz"]).succeeds().stdout_only( "baz");
} }
#[cfg_attr(not(feature="test_unimplemented"),ignore)]
#[test] #[test]
fn test_multiple_param() { fn test_multiple_param() {
for multiple_param in vec!["-a", "--multiple"] { for multiple_param in vec!["-a", "--multiple"] {
@ -33,7 +32,6 @@ fn test_multiple_param() {
} }
} }
#[cfg_attr(not(feature="test_unimplemented"),ignore)]
#[test] #[test]
fn test_suffix_param() { fn test_suffix_param() {
for suffix_param in vec!["-s", "--suffix"] { for suffix_param in vec!["-s", "--suffix"] {
@ -44,7 +42,6 @@ fn test_suffix_param() {
} }
} }
#[cfg_attr(not(feature="test_unimplemented"),ignore)]
#[test] #[test]
fn test_zero_param() { fn test_zero_param() {
for zero_param in vec!["-z", "--zero"] { for zero_param in vec!["-z", "--zero"] {