Skip to content

Commit b78f78b

Browse files
committed
uptime: use clap to handle too many path args
1 parent cd93931 commit b78f78b

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

src/uu/uptime/src/uptime.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,10 @@ pub enum UptimeError {
4444
// io::Error wrapper
4545
#[error("couldn't get boot time: {0}")]
4646
IoErr(#[from] io::Error),
47-
4847
#[error("couldn't get boot time: Is a directory")]
4948
TargetIsDir,
50-
5149
#[error("couldn't get boot time: Illegal seek")]
5250
TargetIsFifo,
53-
#[error("extra operand '{0}'")]
54-
ExtraOperandError(String),
5551
}
5652

5753
impl UError for UptimeError {
@@ -70,30 +66,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
7066
#[cfg(unix)]
7167
{
7268
use std::ffi::OsString;
73-
use uucore::error::set_exit_code;
74-
use uucore::show_error;
7569

76-
let argument = matches.get_many::<OsString>(options::PATH);
70+
let argument = matches.get_one::<OsString>(options::PATH);
7771

78-
// Switches to default uptime behaviour if there is no argument
79-
if argument.is_none() {
80-
return default_uptime(&matches);
81-
}
82-
let mut arg_iter = argument.unwrap();
83-
84-
let file_path = arg_iter.next().unwrap();
85-
if let Some(path) = arg_iter.next() {
86-
// Uptime doesn't attempt to calculate boot time if there is extra arguments.
87-
// Its a fatal error
88-
show_error!(
89-
"{}",
90-
UptimeError::ExtraOperandError(path.to_owned().into_string().unwrap())
91-
);
92-
set_exit_code(1);
93-
return Ok(());
72+
if let Some(file_path) = argument {
73+
uptime_with_file(file_path)
74+
} else {
75+
default_uptime(&matches)
9476
}
95-
96-
uptime_with_file(file_path)
9777
}
9878
}
9979

@@ -113,7 +93,8 @@ pub fn uu_app() -> Command {
11393
.arg(
11494
Arg::new(options::PATH)
11595
.help("file to search boot time from")
116-
.action(ArgAction::Append)
96+
.action(ArgAction::Set)
97+
.num_args(0..=1)
11798
.value_parser(ValueParser::os_string())
11899
.value_hint(ValueHint::AnyPath),
119100
)

tests/by-util/test_uptime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn test_uptime_with_extra_argument() {
251251
.arg("a")
252252
.arg("b")
253253
.fails()
254-
.stderr_contains("extra operand 'b'");
254+
.stderr_contains("unexpected value 'b'");
255255
}
256256
/// Checks whether uptime displays the correct stderr msg when its called with a directory
257257
#[test]

0 commit comments

Comments
 (0)