diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index 0ebbb5be2..8b487847d 100644 --- a/src/uu/false/src/false.rs +++ b/src/uu/false/src/false.rs @@ -4,7 +4,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -use clap::{App, Arg, ArgSettings, ErrorKind}; +use clap::{App, AppSettings, Arg}; use std::io::Write; use uucore::error::{set_exit_code, UResult}; @@ -18,7 +18,7 @@ static ABOUT: &str = " #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let app = uu_app(); + let mut app = uu_app(); // Mirror GNU options, always return `1`. In particular even the 'successful' cases of no-op, // and the interrupted display of help and version should return `1`. Also, we return Ok in all @@ -26,12 +26,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // and unwind through the standard library allocation handling machinery. set_exit_code(1); - if let Err(err) = app.try_get_matches_from(args) { - if let ErrorKind::DisplayHelp | ErrorKind::DisplayVersion = err.kind { - if let Err(print_fail) = err.print() { - // Try to display this error. - let _ = writeln!(std::io::stderr(), "{}: {}", uucore::util_name(), print_fail); - } + if let Ok(matches) = app.try_get_matches_from_mut(args) { + let error = if matches.index_of("help").is_some() { + app.print_long_help() + } else if matches.index_of("version").is_some() { + writeln!(std::io::stdout(), "{}", app.render_version()) + } else { + Ok(()) + }; + + // Try to display this error. + if let Err(print_fail) = error { + // Completely ignore any error here, no more failover and we will fail in any case. + let _ = writeln!(std::io::stderr(), "{}: {}", uucore::util_name(), print_fail); } } @@ -42,16 +49,17 @@ pub fn uu_app<'a>() -> App<'a> { App::new(uucore::util_name()) .version(clap::crate_version!()) .about(ABOUT) - // Hide the default -V and -h for version and help. - // This requires us to overwrite short, not short_aliases. + // We provide our own help and version options, to ensure maximum compatibility with GNU. + .setting(AppSettings::DisableHelpFlag | AppSettings::DisableVersionFlag) .arg( - Arg::new("dummy-help") - .short('h') - .setting(ArgSettings::Hidden), + Arg::new("help") + .long("help") + .help("Print help information") + .exclusive(true), ) .arg( - Arg::new("dummy-version") - .short('V') - .setting(ArgSettings::Hidden), + Arg::new("version") + .long("version") + .help("Print version information"), ) } diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index 20ee100b7..c3026e684 100644 --- a/src/uu/true/src/true.rs +++ b/src/uu/true/src/true.rs @@ -4,7 +4,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -use clap::{App, Arg, ArgSettings, ErrorKind}; +use clap::{App, AppSettings, Arg}; use std::io::Write; use uucore::error::{set_exit_code, UResult}; @@ -18,18 +18,24 @@ static ABOUT: &str = " #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let app = uu_app(); + let mut app = uu_app(); - if let Err(err) = app.try_get_matches_from(args) { - if let ErrorKind::DisplayHelp | ErrorKind::DisplayVersion = err.kind { - if let Err(print_fail) = err.print() { - // Try to display this error. - let _ = writeln!(std::io::stderr(), "{}: {}", uucore::util_name(), print_fail); - // Mirror GNU options. When failing to print warnings or version flags, then we exit - // with FAIL. This avoids allocation some error information which may result in yet - // other types of failure. - set_exit_code(1); - } + if let Ok(matches) = app.try_get_matches_from_mut(args) { + let error = if matches.index_of("help").is_some() { + app.print_long_help() + } else if matches.index_of("version").is_some() { + writeln!(std::io::stdout(), "{}", app.render_version()) + } else { + Ok(()) + }; + + if let Err(print_fail) = error { + // Try to display this error. + let _ = writeln!(std::io::stderr(), "{}: {}", uucore::util_name(), print_fail); + // Mirror GNU options. When failing to print warnings or version flags, then we exit + // with FAIL. This avoids allocation some error information which may result in yet + // other types of failure. + set_exit_code(1); } } @@ -40,16 +46,17 @@ pub fn uu_app<'a>() -> App<'a> { App::new(uucore::util_name()) .version(clap::crate_version!()) .about(ABOUT) - // Hide the default -V and -h for version and help. - // This requires us to overwrite short, not short_aliases. + // We provide our own help and version options, to ensure maximum compatibility with GNU. + .setting(AppSettings::DisableHelpFlag | AppSettings::DisableVersionFlag) .arg( - Arg::new("dummy-help") - .short('h') - .setting(ArgSettings::Hidden), + Arg::new("help") + .long("help") + .help("Print help information") + .exclusive(true), ) .arg( - Arg::new("dummy-version") - .short('V') - .setting(ArgSettings::Hidden), + Arg::new("version") + .long("version") + .help("Print version information"), ) } diff --git a/tests/by-util/test_false.rs b/tests/by-util/test_false.rs index 366dd277b..5ce64e7a8 100644 --- a/tests/by-util/test_false.rs +++ b/tests/by-util/test_false.rs @@ -25,11 +25,19 @@ fn test_help() { #[test] fn test_short_options() { - for option in ["-h", "-v"] { + for option in ["-h", "-V"] { new_ucmd!().arg(option).fails().stdout_is(""); } } +#[test] +fn test_conflict() { + new_ucmd!() + .args(&["--help", "--version"]) + .fails() + .stdout_is(""); +} + #[test] #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] fn test_full() { diff --git a/tests/by-util/test_true.rs b/tests/by-util/test_true.rs index d8ac2003b..aba32578b 100644 --- a/tests/by-util/test_true.rs +++ b/tests/by-util/test_true.rs @@ -25,11 +25,19 @@ fn test_help() { #[test] fn test_short_options() { - for option in ["-h", "-v"] { + for option in ["-h", "-V"] { new_ucmd!().arg(option).succeeds().stdout_is(""); } } +#[test] +fn test_conflict() { + new_ucmd!() + .args(&["--help", "--version"]) + .succeeds() + .stdout_is(""); +} + #[test] #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] fn test_full() {