mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
timeout: handle arguments for the command to run
To prevent clap from parsing flags for the command to run as flags for timeout, remove the "args" positional argument, but allow to pass flags via the "command" positional arg.
This commit is contained in:
parent
3e8c009a50
commit
c5d7cbda32
2 changed files with 18 additions and 18 deletions
|
@ -37,7 +37,6 @@ pub mod options {
|
||||||
// Positional args.
|
// Positional args.
|
||||||
pub static DURATION: &str = "duration";
|
pub static DURATION: &str = "duration";
|
||||||
pub static COMMAND: &str = "command";
|
pub static COMMAND: &str = "command";
|
||||||
pub static ARGS: &str = "args";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
|
@ -47,8 +46,7 @@ struct Config {
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
preserve_status: bool,
|
preserve_status: bool,
|
||||||
|
|
||||||
command: String,
|
command: Vec<String>,
|
||||||
command_args: Vec<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
@ -77,12 +75,11 @@ impl Config {
|
||||||
let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
|
let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
|
||||||
let foreground = options.is_present(options::FOREGROUND);
|
let foreground = options.is_present(options::FOREGROUND);
|
||||||
|
|
||||||
let command: String = options.value_of(options::COMMAND).unwrap().to_string();
|
let command = options
|
||||||
|
.values_of(options::COMMAND)
|
||||||
let command_args: Vec<String> = match options.values_of(options::ARGS) {
|
.unwrap()
|
||||||
Some(values) => values.map(|x| x.to_owned()).collect(),
|
.map(String::from)
|
||||||
None => vec![],
|
.collect::<Vec<_>>();
|
||||||
};
|
|
||||||
|
|
||||||
Config {
|
Config {
|
||||||
foreground,
|
foreground,
|
||||||
|
@ -91,7 +88,6 @@ impl Config {
|
||||||
duration,
|
duration,
|
||||||
preserve_status,
|
preserve_status,
|
||||||
command,
|
command,
|
||||||
command_args,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,9 +133,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
Arg::with_name(options::COMMAND)
|
Arg::with_name(options::COMMAND)
|
||||||
.index(2)
|
.index(2)
|
||||||
.required(true)
|
.required(true)
|
||||||
)
|
.multiple(true)
|
||||||
.arg(
|
|
||||||
Arg::with_name(options::ARGS).multiple(true)
|
|
||||||
)
|
)
|
||||||
.setting(AppSettings::TrailingVarArg);
|
.setting(AppSettings::TrailingVarArg);
|
||||||
|
|
||||||
|
@ -148,7 +142,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let config = Config::from(matches);
|
let config = Config::from(matches);
|
||||||
timeout(
|
timeout(
|
||||||
&config.command,
|
&config.command,
|
||||||
&config.command_args,
|
|
||||||
config.duration,
|
config.duration,
|
||||||
config.signal,
|
config.signal,
|
||||||
config.kill_after,
|
config.kill_after,
|
||||||
|
@ -160,8 +153,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
/// TODO: Improve exit codes, and make them consistent with the GNU Coreutils exit codes.
|
/// TODO: Improve exit codes, and make them consistent with the GNU Coreutils exit codes.
|
||||||
|
|
||||||
fn timeout(
|
fn timeout(
|
||||||
cmdname: &str,
|
cmd: &[String],
|
||||||
args: &[String],
|
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
signal: usize,
|
signal: usize,
|
||||||
kill_after: Duration,
|
kill_after: Duration,
|
||||||
|
@ -171,8 +163,8 @@ fn timeout(
|
||||||
if !foreground {
|
if !foreground {
|
||||||
unsafe { libc::setpgid(0, 0) };
|
unsafe { libc::setpgid(0, 0) };
|
||||||
}
|
}
|
||||||
let mut process = match Command::new(cmdname)
|
let mut process = match Command::new(&cmd[0])
|
||||||
.args(args)
|
.args(&cmd[1..])
|
||||||
.stdin(Stdio::inherit())
|
.stdin(Stdio::inherit())
|
||||||
.stdout(Stdio::inherit())
|
.stdout(Stdio::inherit())
|
||||||
.stderr(Stdio::inherit())
|
.stderr(Stdio::inherit())
|
||||||
|
|
|
@ -9,3 +9,11 @@ fn test_subcommand_return_code() {
|
||||||
|
|
||||||
new_ucmd!().arg("1").arg("false").run().status_code(1);
|
new_ucmd!().arg("1").arg("false").run().status_code(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_command_with_args() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["1700", "echo", "-n", "abcd"])
|
||||||
|
.succeeds()
|
||||||
|
.stdout_only("abcd");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue