Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --with option to cargo run #1763

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bin/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Options {
flag_quiet: bool,
flag_color: Option<String>,
flag_release: bool,
flag_with: Option<String>,
arg_args: Vec<String>,
}

Expand All @@ -34,6 +35,7 @@ Options:
--no-default-features Do not build the `default` feature
--target TRIPLE Build for the target triple
--manifest-path PATH Path to the manifest to execute
-w, --with CMD Run CMD and pass the path to the binary as its argument (see below)
-v, --verbose Use verbose output
-q, --quiet No output printed to stdout
--color WHEN Coloring: auto, always, never
Expand All @@ -46,6 +48,9 @@ and `--example` specifies the example target to run. At most one of `--bin` or
All of the trailing arguments are passed to the binary to run. If you're passing
arguments to both Cargo and the binary, the ones after `--` go to the binary,
the ones before go to Cargo.
If the -w option is used, arguments are passed to CMD instead;
all arguments equal to \"{}\" are replaced with the binary path or,
if no such argument is passed, the binary path is appended as the last argument.
";

pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
Expand Down Expand Up @@ -86,6 +91,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {

let err = try!(ops::run(&root,
&compile_opts,
options.flag_with,
&options.arg_args).map_err(|err| {
CliError::from_boxed(err, 101)
}));
Expand Down
34 changes: 29 additions & 5 deletions src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::Package;

pub fn run(manifest_path: &Path,
options: &ops::CompileOptions,
with: Option<String>,
args: &[String]) -> CargoResult<Option<ProcessError>> {
let config = options.config;
let root = try!(Package::for_path(manifest_path, config));
Expand Down Expand Up @@ -49,10 +50,33 @@ pub fn run(manifest_path: &Path,
Some(path) => path.to_path_buf(),
None => exe.to_path_buf(),
};
let mut process = try!(compile.target_process(exe, &root))
.into_process_builder();
process.args(args).cwd(config.cwd());

try!(config.shell().status("Running", process.to_string()));
Ok(process.exec().err())
match with {
Some(cmd) => {
let mut process = try!(compile.target_process(cmd, &root))
.into_process_builder();

let mut exe_passed = false;
for arg in args {
if arg == "{}" {
exe_passed = true;
process.arg(exe.clone());
}
else { process.arg(arg); }
}
if !exe_passed { process.arg(exe); }

try!(config.shell().status("Running", process.to_string()));
Ok(process.exec().err())
},
None => {
let mut process = try!(compile.target_process(exe, &root))
.into_process_builder();

process.args(args).cwd(config.cwd());

try!(config.shell().status("Running", process.to_string()));
Ok(process.exec().err())
}
}
}