mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
uptime: use clap to handle too many path args
This commit is contained in:
parent
cd93931f82
commit
b78f78bedf
2 changed files with 8 additions and 27 deletions
|
@ -44,14 +44,10 @@ pub enum UptimeError {
|
|||
// io::Error wrapper
|
||||
#[error("couldn't get boot time: {0}")]
|
||||
IoErr(#[from] io::Error),
|
||||
|
||||
#[error("couldn't get boot time: Is a directory")]
|
||||
TargetIsDir,
|
||||
|
||||
#[error("couldn't get boot time: Illegal seek")]
|
||||
TargetIsFifo,
|
||||
#[error("extra operand '{0}'")]
|
||||
ExtraOperandError(String),
|
||||
}
|
||||
|
||||
impl UError for UptimeError {
|
||||
|
@ -70,30 +66,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
#[cfg(unix)]
|
||||
{
|
||||
use std::ffi::OsString;
|
||||
use uucore::error::set_exit_code;
|
||||
use uucore::show_error;
|
||||
|
||||
let argument = matches.get_many::<OsString>(options::PATH);
|
||||
|
||||
// Switches to default uptime behaviour if there is no argument
|
||||
if argument.is_none() {
|
||||
return default_uptime(&matches);
|
||||
}
|
||||
let mut arg_iter = argument.unwrap();
|
||||
|
||||
let file_path = arg_iter.next().unwrap();
|
||||
if let Some(path) = arg_iter.next() {
|
||||
// Uptime doesn't attempt to calculate boot time if there is extra arguments.
|
||||
// Its a fatal error
|
||||
show_error!(
|
||||
"{}",
|
||||
UptimeError::ExtraOperandError(path.to_owned().into_string().unwrap())
|
||||
);
|
||||
set_exit_code(1);
|
||||
return Ok(());
|
||||
}
|
||||
let argument = matches.get_one::<OsString>(options::PATH);
|
||||
|
||||
if let Some(file_path) = argument {
|
||||
uptime_with_file(file_path)
|
||||
} else {
|
||||
default_uptime(&matches)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,7 +93,8 @@ pub fn uu_app() -> Command {
|
|||
.arg(
|
||||
Arg::new(options::PATH)
|
||||
.help("file to search boot time from")
|
||||
.action(ArgAction::Append)
|
||||
.action(ArgAction::Set)
|
||||
.num_args(0..=1)
|
||||
.value_parser(ValueParser::os_string())
|
||||
.value_hint(ValueHint::AnyPath),
|
||||
)
|
||||
|
|
|
@ -251,7 +251,7 @@ fn test_uptime_with_extra_argument() {
|
|||
.arg("a")
|
||||
.arg("b")
|
||||
.fails()
|
||||
.stderr_contains("extra operand 'b'");
|
||||
.stderr_contains("unexpected value 'b'");
|
||||
}
|
||||
/// Checks whether uptime displays the correct stderr msg when its called with a directory
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue