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

Merge pull request #2150 from jhscheer/fix_clap_short

tr/dirname: fix clap short_alias
This commit is contained in:
Sylvestre Ledru 2021-05-01 17:39:15 +02:00 committed by GitHub
commit 34bf7cc5ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 25 deletions

View file

@ -12,37 +12,42 @@ use clap::{App, Arg};
use std::path::Path; use std::path::Path;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static NAME: &str = "dirname"; static ABOUT: &str = "strip last component from file name";
static SYNTAX: &str = "[OPTION] NAME...";
static SUMMARY: &str = "strip last component from file name";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static LONG_HELP: &str = "
Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current
directory).
";
mod options { mod options {
pub const ZERO: &str = "zero"; pub const ZERO: &str = "zero";
pub const DIR: &str = "dir"; pub const DIR: &str = "dir";
} }
fn get_usage() -> String {
format!("{0} [OPTION] NAME...", executable!())
}
fn get_long_usage() -> String {
String::from(
"Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current directory).",
)
}
pub fn uumain(args: impl uucore::Args) -> i32 { pub fn uumain(args: impl uucore::Args) -> i32 {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
let usage = get_usage();
let after_help = get_long_usage();
let matches = App::new(executable!()) let matches = App::new(executable!())
.name(NAME) .about(ABOUT)
.usage(SYNTAX) .usage(&usage[..])
.about(SUMMARY) .after_help(&after_help[..])
.after_help(LONG_HELP)
.version(VERSION) .version(VERSION)
.arg( .arg(
Arg::with_name(options::ZERO) Arg::with_name(options::ZERO)
.short(options::ZERO) .long(options::ZERO)
.short("z") .short("z")
.takes_value(false)
.help("separate output with NUL rather than newline"), .help("separate output with NUL rather than newline"),
) )
.arg(Arg::with_name(options::DIR).hidden(true).multiple(true)) .arg(Arg::with_name(options::DIR).hidden(true).multiple(true))

View file

@ -23,11 +23,9 @@ use std::io::{stdin, stdout, BufRead, BufWriter, Write};
use crate::expand::ExpandSet; use crate::expand::ExpandSet;
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static NAME: &str = "tr";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "translate or delete characters"; static ABOUT: &str = "translate or delete characters";
static LONG_HELP: &str = "Translate, squeeze, and/or delete characters from standard input,
writing to standard output.";
const BUFFER_LEN: usize = 1024; const BUFFER_LEN: usize = 1024;
mod options { mod options {
@ -186,24 +184,38 @@ fn get_usage() -> String {
format!("{} [OPTION]... SET1 [SET2]", executable!()) format!("{} [OPTION]... SET1 [SET2]", executable!())
} }
fn get_long_usage() -> String {
String::from(
"Translate, squeeze, and/or delete characters from standard input,
writing to standard output.",
)
}
pub fn uumain(args: impl uucore::Args) -> i32 { pub fn uumain(args: impl uucore::Args) -> i32 {
let usage = get_usage();
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
let usage = get_usage();
let after_help = get_long_usage();
let matches = App::new(executable!()) let matches = App::new(executable!())
.version(VERSION) .version(VERSION)
.about(ABOUT) .about(ABOUT)
.usage(&usage[..]) .usage(&usage[..])
.after_help(LONG_HELP) .after_help(&after_help[..])
.arg( .arg(
Arg::with_name(options::COMPLEMENT) Arg::with_name(options::COMPLEMENT)
.short("C") // .visible_short_alias('C') // TODO: requires clap "3.0.0-beta.2"
.short("c") .short("c")
.long(options::COMPLEMENT) .long(options::COMPLEMENT)
.help("use the complement of SET1"), .help("use the complement of SET1"),
) )
.arg(
Arg::with_name("C") // work around for `Arg::visible_short_alias`
.short("C")
.help("same as -c"),
)
.arg( .arg(
Arg::with_name(options::DELETE) Arg::with_name(options::DELETE)
.short("d") .short("d")
@ -216,8 +228,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.short("s") .short("s")
.help( .help(
"replace each sequence of a repeated character that is "replace each sequence of a repeated character that is
listed in the last specified SET, with a single occurrence listed in the last specified SET, with a single occurrence
of that character", of that character",
), ),
) )
.arg( .arg(
@ -230,7 +242,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.get_matches_from(args); .get_matches_from(args);
let delete_flag = matches.is_present(options::DELETE); let delete_flag = matches.is_present(options::DELETE);
let complement_flag = matches.is_present(options::COMPLEMENT); let complement_flag = matches.is_present(options::COMPLEMENT) || matches.is_present("C");
let squeeze_flag = matches.is_present(options::SQUEEZE); let squeeze_flag = matches.is_present(options::SQUEEZE);
let truncate_flag = matches.is_present(options::TRUNCATE); let truncate_flag = matches.is_present(options::TRUNCATE);
@ -242,7 +254,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
if sets.is_empty() { if sets.is_empty() {
show_error!( show_error!(
"missing operand\nTry `{} --help` for more information.", "missing operand\nTry `{} --help` for more information.",
NAME executable!()
); );
return 1; return 1;
} }
@ -251,7 +263,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
show_error!( show_error!(
"missing operand after {}\nTry `{} --help` for more information.", "missing operand after {}\nTry `{} --help` for more information.",
sets[0], sets[0],
NAME executable!()
); );
return 1; return 1;
} }

View file

@ -16,6 +16,21 @@ fn test_path_without_trailing_slashes() {
.stdout_is("/root/alpha/beta/gamma/delta/epsilon\n"); .stdout_is("/root/alpha/beta/gamma/delta/epsilon\n");
} }
#[test]
fn test_path_without_trailing_slashes_and_zero() {
new_ucmd!()
.arg("-z")
.arg("/root/alpha/beta/gamma/delta/epsilon/omega")
.succeeds()
.stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}");
new_ucmd!()
.arg("--zero")
.arg("/root/alpha/beta/gamma/delta/epsilon/omega")
.succeeds()
.stdout_is("/root/alpha/beta/gamma/delta/epsilon\u{0}");
}
#[test] #[test]
fn test_root() { fn test_root() {
new_ucmd!().arg("/").run().stdout_is("/\n"); new_ucmd!().arg("/").run().stdout_is("/\n");

View file

@ -45,6 +45,20 @@ fn test_delete_complement() {
.stdout_is("ac"); .stdout_is("ac");
} }
#[test]
fn test_delete_complement_2() {
new_ucmd!()
.args(&["-d", "-C", "0-9"])
.pipe_in("Phone: 01234 567890")
.succeeds()
.stdout_is("01234567890");
new_ucmd!()
.args(&["-d", "--complement", "0-9"])
.pipe_in("Phone: 01234 567890")
.succeeds()
.stdout_is("01234567890");
}
#[test] #[test]
fn test_squeeze() { fn test_squeeze() {
new_ucmd!() new_ucmd!()