mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Merge pull request #4268 from rivy/fix.sleep
Fix `sleep` argument parsing and errors
This commit is contained in:
commit
6a9660f9f6
2 changed files with 35 additions and 18 deletions
|
@ -9,8 +9,8 @@ use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use uucore::{
|
use uucore::{
|
||||||
error::{UResult, UUsageError},
|
error::{UResult, USimpleError, UUsageError},
|
||||||
format_usage,
|
format_usage, show,
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgAction, Command};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
|
@ -33,12 +33,21 @@ mod options {
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let matches = uu_app().try_get_matches_from(args)?;
|
let matches = uu_app().try_get_matches_from(args)?;
|
||||||
|
|
||||||
if let Some(values) = matches.get_many::<String>(options::NUMBER) {
|
let numbers = matches
|
||||||
let numbers = values.map(|s| s.as_str()).collect::<Vec<_>>();
|
.get_many::<String>(options::NUMBER)
|
||||||
return sleep(&numbers);
|
.ok_or_else(|| {
|
||||||
}
|
USimpleError::new(
|
||||||
|
1,
|
||||||
|
format!(
|
||||||
|
"missing operand\nTry '{} --help' for more information.",
|
||||||
|
uucore::execution_phrase()
|
||||||
|
),
|
||||||
|
)
|
||||||
|
})?
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
Ok(())
|
return sleep(&numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> Command {
|
pub fn uu_app() -> Command {
|
||||||
|
@ -52,20 +61,24 @@ pub fn uu_app() -> Command {
|
||||||
Arg::new(options::NUMBER)
|
Arg::new(options::NUMBER)
|
||||||
.help("pause for NUMBER seconds")
|
.help("pause for NUMBER seconds")
|
||||||
.value_name(options::NUMBER)
|
.value_name(options::NUMBER)
|
||||||
.action(ArgAction::Append)
|
.action(ArgAction::Append),
|
||||||
.required(true),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sleep(args: &[&str]) -> UResult<()> {
|
fn sleep(args: &[&str]) -> UResult<()> {
|
||||||
let sleep_dur =
|
let mut arg_error = false;
|
||||||
args.iter().try_fold(
|
let intervals = args.iter().map(|s| match uucore::parse_time::from_str(s) {
|
||||||
Duration::new(0, 0),
|
Ok(result) => result,
|
||||||
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
|
Err(err) => {
|
||||||
Ok(m) => Ok(m.saturating_add(result)),
|
arg_error = true;
|
||||||
Err(f) => Err(UUsageError::new(1, f)),
|
show!(USimpleError::new(1, err));
|
||||||
},
|
Duration::new(0, 0)
|
||||||
)?;
|
}
|
||||||
|
});
|
||||||
|
let sleep_dur = intervals.fold(Duration::new(0, 0), |acc, n| acc + n);
|
||||||
|
if arg_error {
|
||||||
|
return Err(UUsageError::new(1, ""));
|
||||||
|
};
|
||||||
thread::sleep(sleep_dur);
|
thread::sleep(sleep_dur);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,10 @@ fn test_invalid_time_interval() {
|
||||||
.arg("xyz")
|
.arg("xyz")
|
||||||
.fails()
|
.fails()
|
||||||
.usage_error("invalid time interval 'xyz'");
|
.usage_error("invalid time interval 'xyz'");
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--", "-1"])
|
||||||
|
.fails()
|
||||||
|
.usage_error("invalid time interval '-1'");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -75,7 +79,7 @@ fn test_sleep_zero_duration() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sleep_no_argument() {
|
fn test_sleep_no_argument() {
|
||||||
new_ucmd!().fails();
|
new_ucmd!().fails().usage_error("missing operand");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue