fix -h and --help discrepancies

addresses https://github.com/uutils/coreutils/issues/3370
This commit is contained in:
ndd7xv 2022-04-26 20:02:59 -04:00
parent b70a61aa11
commit 88752ecc3e
7 changed files with 46 additions and 37 deletions

View file

@ -469,14 +469,25 @@ pub fn uu_app<'a>() -> Command<'a> {
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app()
.after_help(&*format!(
let after_help = &*format!(
"{}\n{}",
LONG_HELP,
backup_control::BACKUP_CONTROL_LONG_HELP
))
.try_get_matches_from(args)?;
);
let matches = uu_app().after_help(after_help).try_get_matches_from(args);
// The error is parsed here because we do not want version or help being printed to stderr.
if let Err(e) = matches {
let mut app = uu_app().after_help(after_help);
match e.kind() {
clap::ErrorKind::DisplayHelp => {
app.print_help()?;
}
clap::ErrorKind::DisplayVersion => println!("{}", app.render_version()),
_ => return Err(Box::new(e)),
};
} else if let Ok(matches) = matches {
let options = Options::from_matches(&matches)?;
if options.overwrite == OverwriteMode::NoClobber && options.backup != BackupMode::NoBackup {
@ -501,6 +512,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};
set_exit_code(EXIT_ERR);
}
}
Ok(())
}

View file

@ -28,7 +28,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if let Ok(matches) = command.try_get_matches_from_mut(args) {
let error = if matches.index_of("help").is_some() {
command.print_long_help()
command.print_help()
} else if matches.index_of("version").is_some() {
writeln!(std::io::stdout(), "{}", command.render_version())
} else {

View file

@ -399,7 +399,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}
if matches.is_present(options::HELP) {
command.print_long_help()?;
command.print_help()?;
return Ok(());
}

View file

@ -1272,6 +1272,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.after_help(LONG_HELP_KEYS)
.override_usage(format_usage(USAGE))
.infer_long_args(true)
.arg(
@ -1421,7 +1422,6 @@ pub fn uu_app<'a>() -> Command<'a> {
.short('k')
.long(options::KEY)
.help("sort by a key")
.long_help(LONG_HELP_KEYS)
.multiple_occurrences(true)
.number_of_values(1)
.takes_value(true),
@ -1466,8 +1466,7 @@ pub fn uu_app<'a>() -> Command<'a> {
.arg(
Arg::new(options::COMPRESS_PROG)
.long(options::COMPRESS_PROG)
.help("compress temporary files with PROG, decompress with PROG -d")
.long_help("PROG has to take input from stdin and output to stdout")
.help("compress temporary files with PROG, decompress with PROG -d; PROG has to take input from stdin and output to stdout")
.value_name("PROG"),
)
.arg(

View file

@ -22,7 +22,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if let Ok(matches) = command.try_get_matches_from_mut(args) {
let error = if matches.index_of("help").is_some() {
command.print_long_help()
command.print_help()
} else if matches.index_of("version").is_some() {
writeln!(std::io::stdout(), "{}", command.render_version())
} else {

View file

@ -127,7 +127,7 @@ pub fn uu_app<'a>() -> Command<'a> {
Arg::new(options::TABS)
.short('t')
.long(options::TABS)
.long_help("use comma separated LIST of tab positions or have tabs N characters apart instead of 8 (enables -a)")
.help("use comma separated LIST of tab positions or have tabs N characters apart instead of 8 (enables -a)")
.takes_value(true)
)
.arg(

View file

@ -183,8 +183,6 @@ sed -i -e "s~ sed -n \"1s/'\\\/'/'OPT'/p\" < err >> pat || framework_failure_~
sed -i -e "s/rcexp=1$/rcexp=2\n case \"\$prg\" in chcon|runcon) return;; esac/" -e "s/rcexp=125 ;;/rcexp=2 ;;\ncp|truncate|pr) rcexp=1;;/" tests/misc/usage_vs_getopt.sh
# GNU has option=[SUFFIX], clap is <SUFFIX>
sed -i -e "s/cat opts/sed -i -e \"s| <.\*>$||g\" opts/" tests/misc/usage_vs_getopt.sh
# Strip empty lines for the diff - see https://github.com/uutils/coreutils/issues/3370
sed -i -e "s~out 2>err1~out 2>err1\nsed '/^$/d' out > out\nsed '/^$/d' help > help~" tests/misc/usage_vs_getopt.sh
# for some reasons, some stuff are duplicated, strip that
sed -i -e "s/provoked error./provoked error\ncat pat |sort -u > pat/" tests/misc/usage_vs_getopt.sh