mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 23:02:38 +00:00
Merge pull request #3784 from jarkonik/main
Use `clap::ArgAction` in `true` and `false`
This commit is contained in:
commit
5ecabb8467
2 changed files with 34 additions and 22 deletions
|
@ -4,8 +4,8 @@
|
||||||
// *
|
// *
|
||||||
// * For the full copyright and license information, please view the LICENSE
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, ArgAction, Command};
|
||||||
use std::io::Write;
|
use std::{ffi::OsString, io::Write};
|
||||||
use uucore::error::{set_exit_code, UResult};
|
use uucore::error::{set_exit_code, UResult};
|
||||||
|
|
||||||
static ABOUT: &str = "\
|
static ABOUT: &str = "\
|
||||||
|
@ -26,13 +26,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
// and unwind through the standard library allocation handling machinery.
|
// and unwind through the standard library allocation handling machinery.
|
||||||
set_exit_code(1);
|
set_exit_code(1);
|
||||||
|
|
||||||
if let Ok(matches) = command.try_get_matches_from_mut(args) {
|
let args: Vec<OsString> = args.collect();
|
||||||
let error = if matches.index_of("help").is_some() {
|
if args.len() > 2 {
|
||||||
command.print_help()
|
return Ok(());
|
||||||
} else if matches.index_of("version").is_some() {
|
}
|
||||||
|
|
||||||
|
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())
|
writeln!(std::io::stdout(), "{}", command.render_version())
|
||||||
} else {
|
}
|
||||||
Ok(())
|
_ => Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to display this error.
|
// Try to display this error.
|
||||||
|
@ -56,11 +61,12 @@ pub fn uu_app<'a>() -> Command<'a> {
|
||||||
Arg::new("help")
|
Arg::new("help")
|
||||||
.long("help")
|
.long("help")
|
||||||
.help("Print help information")
|
.help("Print help information")
|
||||||
.exclusive(true),
|
.action(ArgAction::Help),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("version")
|
Arg::new("version")
|
||||||
.long("version")
|
.long("version")
|
||||||
.help("Print version information"),
|
.help("Print version information")
|
||||||
|
.action(ArgAction::Version),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
// *
|
// *
|
||||||
// * For the full copyright and license information, please view the LICENSE
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, ArgAction, Command};
|
||||||
use std::io::Write;
|
use std::{ffi::OsString, io::Write};
|
||||||
use uucore::error::{set_exit_code, UResult};
|
use uucore::error::{set_exit_code, UResult};
|
||||||
|
|
||||||
static ABOUT: &str = "\
|
static ABOUT: &str = "\
|
||||||
|
@ -20,13 +20,18 @@ operation causes the program to return `1` instead.
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let mut command = uu_app();
|
let mut command = uu_app();
|
||||||
|
|
||||||
if let Ok(matches) = command.try_get_matches_from_mut(args) {
|
let args: Vec<OsString> = args.collect();
|
||||||
let error = if matches.index_of("help").is_some() {
|
if args.len() > 2 {
|
||||||
command.print_help()
|
return Ok(());
|
||||||
} else if matches.index_of("version").is_some() {
|
}
|
||||||
|
|
||||||
|
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())
|
writeln!(std::io::stdout(), "{}", command.render_version())
|
||||||
} else {
|
}
|
||||||
Ok(())
|
_ => Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(print_fail) = error {
|
if let Err(print_fail) = error {
|
||||||
|
@ -53,11 +58,12 @@ pub fn uu_app<'a>() -> Command<'a> {
|
||||||
Arg::new("help")
|
Arg::new("help")
|
||||||
.long("help")
|
.long("help")
|
||||||
.help("Print help information")
|
.help("Print help information")
|
||||||
.exclusive(true),
|
.action(ArgAction::Help),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("version")
|
Arg::new("version")
|
||||||
.long("version")
|
.long("version")
|
||||||
.help("Print version information"),
|
.help("Print version information")
|
||||||
|
.action(ArgAction::Version),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue