1
Fork 0
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:
Daniel Hofstetter 2025-05-06 16:05:23 +02:00
parent cd93931f82
commit b78f78bedf
2 changed files with 8 additions and 27 deletions

View file

@ -44,14 +44,10 @@ pub enum UptimeError {
// io::Error wrapper // io::Error wrapper
#[error("couldn't get boot time: {0}")] #[error("couldn't get boot time: {0}")]
IoErr(#[from] io::Error), IoErr(#[from] io::Error),
#[error("couldn't get boot time: Is a directory")] #[error("couldn't get boot time: Is a directory")]
TargetIsDir, TargetIsDir,
#[error("couldn't get boot time: Illegal seek")] #[error("couldn't get boot time: Illegal seek")]
TargetIsFifo, TargetIsFifo,
#[error("extra operand '{0}'")]
ExtraOperandError(String),
} }
impl UError for UptimeError { impl UError for UptimeError {
@ -70,30 +66,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
#[cfg(unix)] #[cfg(unix)]
{ {
use std::ffi::OsString; use std::ffi::OsString;
use uucore::error::set_exit_code;
use uucore::show_error;
let argument = matches.get_many::<OsString>(options::PATH); let argument = matches.get_one::<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(());
}
if let Some(file_path) = argument {
uptime_with_file(file_path) uptime_with_file(file_path)
} else {
default_uptime(&matches)
}
} }
} }
@ -113,7 +93,8 @@ pub fn uu_app() -> Command {
.arg( .arg(
Arg::new(options::PATH) Arg::new(options::PATH)
.help("file to search boot time from") .help("file to search boot time from")
.action(ArgAction::Append) .action(ArgAction::Set)
.num_args(0..=1)
.value_parser(ValueParser::os_string()) .value_parser(ValueParser::os_string())
.value_hint(ValueHint::AnyPath), .value_hint(ValueHint::AnyPath),
) )

View file

@ -251,7 +251,7 @@ fn test_uptime_with_extra_argument() {
.arg("a") .arg("a")
.arg("b") .arg("b")
.fails() .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 /// Checks whether uptime displays the correct stderr msg when its called with a directory
#[test] #[test]