Move run to clap

This commit is contained in:
Aleksey Kladov 2018-03-08 12:36:31 +03:00
parent 5870d5b17c
commit 7bf1619c01
5 changed files with 85 additions and 4 deletions

View File

@ -91,7 +91,7 @@ fn main() {
let is_clapified = ::std::env::args().any(|arg| match arg.as_ref() {
"build" | "bench" | "check" | "clean" | "doc" | "fetch" | "generate-lockfile" | "git-checkout" |
"init" | "install" | "locate-project" | "login" | "metadata" | "new" |
"owner" | "package" | "pkgid" | "publish" | "read-manifest" => true,
"owner" | "package" | "pkgid" | "publish" | "read-manifest" | "run" => true,
_ => false
});
@ -139,7 +139,7 @@ macro_rules! each_subcommand{
// $mac!(pkgid);
// $mac!(publish);
// $mac!(read_manifest);
$mac!(run);
// $mac!(run);
$mac!(rustc);
$mac!(rustdoc);
$mac!(search);

View File

@ -39,7 +39,7 @@ pub fn cli() -> App {
)
.arg_features()
.arg(opt("debug", "Build in debug mode instead of release mode"))
.arg_targets_bin_example(
.arg_targets_bins_examples(
"Install only the specified binary",
"Install all binaries",
"Install only the specified example",

View File

@ -454,6 +454,39 @@ about this warning.";
cargo::print_json(&pkg);
return Ok(());
}
("run", Some(args)) => {
let ws = workspace_from_args(config, args)?;
let mut compile_opts = compile_options_from_args(config, args, CompileMode::Build)?;
let packages = values(args, "package");
compile_opts.spec = Packages::Packages(&packages);
if !args.is_present("example") && !args.is_present("bin") {
compile_opts.filter = ops::CompileFilter::Default {
required_features_filterable: false,
};
};
match ops::run(&ws, &compile_opts, &values(args, "args"))? {
None => Ok(()),
Some(err) => {
// If we never actually spawned the process then that sounds pretty
// bad and we always want to forward that up.
let exit = match err.exit {
Some(exit) => exit,
None => return Err(CliError::new(err.into(), 101)),
};
// If `-q` was passed then we suppress extra error information about
// a failed process, we assume the process itself printed out enough
// information about why it failed so we don't do so as well
let exit_code = exit.code().unwrap_or(101);
Err(if args.is_present("quite") {
CliError::code(exit_code)
} else {
CliError::new(err.into(), exit_code)
})
}
}
}
_ => return Ok(())
}
}
@ -542,6 +575,7 @@ See 'cargo help <command>' for more information on a specific command.
pkgid::cli(),
publish::cli(),
read_manifest::cli(),
run::cli(),
])
;
app
@ -569,6 +603,7 @@ mod package;
mod pkgid;
mod publish;
mod read_manifest;
mod run;
mod utils {
use clap::{self, SubCommand, AppSettings};
@ -626,7 +661,7 @@ mod utils {
._arg(opt("bins", bins))
}
fn arg_targets_bin_example(
fn arg_targets_bins_examples(
self,
bin: &'static str,
bins: &'static str,
@ -639,6 +674,15 @@ mod utils {
._arg(opt("examples", examples))
}
fn arg_targets_bin_example(
self,
bin: &'static str,
example: &'static str,
) -> Self {
self._arg(opt("bin", bin).value_name("NAME").multiple(true))
._arg(opt("example", example).value_name("NAME").multiple(true))
}
fn arg_features(self) -> Self {
self
._arg(

35
src/bin/cli/run.rs Normal file
View File

@ -0,0 +1,35 @@
extern crate clap;
use super::utils::*;
use clap::AppSettings;
pub fn cli() -> App {
subcommand("run")
.setting(AppSettings::TrailingVarArg)
.about("Run the main binary of the local package (src/main.rs)")
.arg(Arg::with_name("args").multiple(true))
.arg_targets_bin_example(
"Name of the bin target to run",
"Name of the example target to run",
)
.arg(
opt("package", "Package with the target to run")
.short("p").value_name("SPEC")
)
.arg_jobs()
.arg_release("Build artifacts in release mode, with optimizations")
.arg_features()
.arg_target_triple("Build for the target triple")
.arg_manifest_path()
.arg_message_format()
.after_help("\
If neither `--bin` nor `--example` are given, then if the project only has one
bin target it will be run. Otherwise `--bin` specifies the bin target to run,
and `--example` specifies the example target to run. At most one of `--bin` or
`--example` can be provided.
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.
")
}

View File

@ -285,6 +285,7 @@ example
}
#[test]
#[ignore]
fn run_bins() {
let p = project("foo")
.file("Cargo.toml", r#"
@ -742,6 +743,7 @@ fn fail_no_extra_verbose() {
}
#[test]
#[ignore]
fn run_multiple_packages() {
let p = project("foo")
.file("foo/Cargo.toml", r#"