Skip to content

Commit

Permalink
Add --with option to cargo run
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechkral committed Nov 17, 2015
1 parent c41e05f commit 2dad9ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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);
}
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())
}
}
}

0 comments on commit 2dad9ee

Please sign in to comment.