From 025a0dfa9e0d455b0beaebf0cec7350bd092a8fb Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Sat, 6 Aug 2022 16:16:09 +0200 Subject: [PATCH 1/2] Use clap::ArgAction in `true` and `false` --- src/uu/false/src/false.rs | 22 ++++++++++++---------- src/uu/true/src/true.rs | 22 ++++++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index 0ee6128b2..c9447598b 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::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use std::io::Write; use uucore::error::{set_exit_code, UResult}; @@ -26,13 +26,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // and unwind through the standard library allocation handling machinery. set_exit_code(1); - if let Ok(matches) = command.try_get_matches_from_mut(args) { - let error = if matches.index_of("help").is_some() { - command.print_help() - } else if matches.index_of("version").is_some() { - writeln!(std::io::stdout(), "{}", command.render_version()) - } else { - Ok(()) + if let Err(e) = command.try_get_matches_from_mut(args) { + let error = match e.kind() { + clap::ErrorKind::DisplayHelp => command.print_help(), + clap::ErrorKind::DisplayVersion => { + writeln!(std::io::stdout(), "{}", command.render_version()) + } + _ => Ok(()), }; // Try to display this error. @@ -56,11 +56,13 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new("help") .long("help") .help("Print help information") - .exclusive(true), + .exclusive(true) + .action(ArgAction::Help), ) .arg( Arg::new("version") .long("version") - .help("Print version information"), + .help("Print version information") + .action(ArgAction::Version), ) } diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index 7742e9ee1..20a9f39f0 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::{Arg, Command}; +use clap::{Arg, ArgAction, Command}; use std::io::Write; use uucore::error::{set_exit_code, UResult}; @@ -20,13 +20,13 @@ operation causes the program to return `1` instead. pub fn uumain(args: impl uucore::Args) -> UResult<()> { let mut command = uu_app(); - if let Ok(matches) = command.try_get_matches_from_mut(args) { - let error = if matches.index_of("help").is_some() { - command.print_help() - } else if matches.index_of("version").is_some() { - writeln!(std::io::stdout(), "{}", command.render_version()) - } else { - Ok(()) + if let Err(e) = command.try_get_matches_from_mut(args) { + let error = match e.kind() { + clap::ErrorKind::DisplayHelp => command.print_help(), + clap::ErrorKind::DisplayVersion => { + writeln!(std::io::stdout(), "{}", command.render_version()) + } + _ => Ok(()), }; if let Err(print_fail) = error { @@ -53,11 +53,13 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new("help") .long("help") .help("Print help information") - .exclusive(true), + .exclusive(true) + .action(ArgAction::Help), ) .arg( Arg::new("version") .long("version") - .help("Print version information"), + .help("Print version information") + .action(ArgAction::Version), ) } From 710e2af5fdd58475e7057de9f6f2ae73569c2e91 Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Mon, 15 Aug 2022 18:36:26 +0200 Subject: [PATCH 2/2] Check argc instead of settings exclusive flag --- src/uu/false/src/false.rs | 8 ++++++-- src/uu/true/src/true.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index c9447598b..4b8ef36e2 100644 --- a/src/uu/false/src/false.rs +++ b/src/uu/false/src/false.rs @@ -5,7 +5,7 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. use clap::{Arg, ArgAction, Command}; -use std::io::Write; +use std::{ffi::OsString, io::Write}; use uucore::error::{set_exit_code, UResult}; static ABOUT: &str = "\ @@ -26,6 +26,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // and unwind through the standard library allocation handling machinery. set_exit_code(1); + let args: Vec = args.collect(); + if args.len() > 2 { + return Ok(()); + } + if let Err(e) = command.try_get_matches_from_mut(args) { let error = match e.kind() { clap::ErrorKind::DisplayHelp => command.print_help(), @@ -56,7 +61,6 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new("help") .long("help") .help("Print help information") - .exclusive(true) .action(ArgAction::Help), ) .arg( diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index 20a9f39f0..50a0636aa 100644 --- a/src/uu/true/src/true.rs +++ b/src/uu/true/src/true.rs @@ -5,7 +5,7 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. use clap::{Arg, ArgAction, Command}; -use std::io::Write; +use std::{ffi::OsString, io::Write}; use uucore::error::{set_exit_code, UResult}; static ABOUT: &str = "\ @@ -20,6 +20,11 @@ operation causes the program to return `1` instead. pub fn uumain(args: impl uucore::Args) -> UResult<()> { let mut command = uu_app(); + let args: Vec = args.collect(); + if args.len() > 2 { + return Ok(()); + } + if let Err(e) = command.try_get_matches_from_mut(args) { let error = match e.kind() { clap::ErrorKind::DisplayHelp => command.print_help(), @@ -53,7 +58,6 @@ pub fn uu_app<'a>() -> Command<'a> { Arg::new("help") .long("help") .help("Print help information") - .exclusive(true) .action(ArgAction::Help), ) .arg(