mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07: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:
@@ -37,7 +37,6 @@ pub mod options {
|
||||
// Positional args.
|
||||
pub static DURATION: &str = "duration";
|
||||
pub static COMMAND: &str = "command";
|
||||
pub static ARGS: &str = "args";
|
||||
}
|
||||
|
||||
struct Config {
|
||||
@@ -47,8 +46,7 @@ struct Config {
|
||||
duration: Duration,
|
||||
preserve_status: bool,
|
||||
|
||||
command: String,
|
||||
command_args: Vec<String>,
|
||||
command: Vec<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -77,12 +75,11 @@ impl Config {
|
||||
let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
|
||||
let foreground = options.is_present(options::FOREGROUND);
|
||||
|
||||
let command: String = options.value_of(options::COMMAND).unwrap().to_string();
|
||||
|
||||
let command_args: Vec<String> = match options.values_of(options::ARGS) {
|
||||
Some(values) => values.map(|x| x.to_owned()).collect(),
|
||||
None => vec![],
|
||||
};
|
||||
let command = options
|
||||
.values_of(options::COMMAND)
|
||||
.unwrap()
|
||||
.map(String::from)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Config {
|
||||
foreground,
|
||||
@@ -91,7 +88,6 @@ impl Config {
|
||||
duration,
|
||||
preserve_status,
|
||||
command,
|
||||
command_args,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,9 +133,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
Arg::with_name(options::COMMAND)
|
||||
.index(2)
|
||||
.required(true)
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name(options::ARGS).multiple(true)
|
||||
.multiple(true)
|
||||
)
|
||||
.setting(AppSettings::TrailingVarArg);
|
||||
|
||||
@@ -148,7 +142,6 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let config = Config::from(matches);
|
||||
timeout(
|
||||
&config.command,
|
||||
&config.command_args,
|
||||
config.duration,
|
||||
config.signal,
|
||||
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.
|
||||
|
||||
fn timeout(
|
||||
cmdname: &str,
|
||||
args: &[String],
|
||||
cmd: &[String],
|
||||
duration: Duration,
|
||||
signal: usize,
|
||||
kill_after: Duration,
|
||||
@@ -171,8 +163,8 @@ fn timeout(
|
||||
if !foreground {
|
||||
unsafe { libc::setpgid(0, 0) };
|
||||
}
|
||||
let mut process = match Command::new(cmdname)
|
||||
.args(args)
|
||||
let mut process = match Command::new(&cmd[0])
|
||||
.args(&cmd[1..])
|
||||
.stdin(Stdio::inherit())
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
|
||||
@@ -9,3 +9,11 @@ fn test_subcommand_return_code() {
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user