From ce249e32390d2073dab377928d7fbeef52630bfd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 8 Mar 2018 13:26:01 +0300 Subject: [PATCH] Don't copy-paste package argument --- src/bin/cli/mod.rs | 35 +++++++++++++++++++++++++---------- src/bin/cli/pkgid.rs | 5 +---- src/bin/cli/run.rs | 5 +---- src/bin/cli/rustc.rs | 5 +---- src/bin/cli/rustdoc.rs | 5 +---- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs index a1b0125ce..c5ab7551d 100644 --- a/src/bin/cli/mod.rs +++ b/src/bin/cli/mod.rs @@ -124,6 +124,18 @@ pub fn do_main(config: &mut Config) -> Result<(), CliError> { Ok(opts) } + fn compile_options_from_args_for_single_package<'a>( + config: &'a Config, + args: &'a ArgMatches<'a>, + mode: CompileMode, + ) -> CargoResult> { + let mut compile_opts = compile_options_from_args(config, args, mode)?; + let packages = values(args, "package"); + compile_opts.spec = Packages::Packages(&packages); + Ok(compile_opts) + } + + fn new_opts_from_args<'a>(args: &'a ArgMatches<'a>, path: &'a str) -> CargoResult> { let vcs = args.value_of("vcs").map(|vcs| match vcs { "git" => VersionControl::Git, @@ -464,9 +476,9 @@ about this warning."; ("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); + let mut compile_opts = compile_options_from_args_for_single_package( + config, args, CompileMode::Build + )?; if !args.is_present("example") && !args.is_present("bin") { compile_opts.filter = ops::CompileFilter::Default { required_features_filterable: false, @@ -507,19 +519,18 @@ about this warning."; return Err(CliError::new(err, 101)) } }; - let mut compile_opts = compile_options_from_args(config, args, mode)?; - let packages = values(args, "package"); - compile_opts.spec = Packages::Packages(&packages); + let mut compile_opts = compile_options_from_args_for_single_package( + config, args, mode + )?; compile_opts.target_rustc_args = Some(&values(args, "args")); ops::compile(&ws, &compile_opts)?; return Ok(()); } ("rustdoc", Some(args)) => { let ws = workspace_from_args(config, args)?; - let mode = CompileMode::Doc { deps: false }; - let mut compile_opts = compile_options_from_args(config, args, mode)?; - let packages = values(args, "package"); - compile_opts.spec = Packages::Packages(&packages); + let mut compile_opts = compile_options_from_args_for_single_package( + config, args, CompileMode::Doc { deps: false } + )?; compile_opts.target_rustdoc_args = Some(&values(args, "args")); let doc_opts = ops::DocOptions { open_result: args.is_present("open"), @@ -665,6 +676,10 @@ mod utils { ._arg(opt("exclude", exclude).value_name("SPEC").multiple(true)) } + fn arg_single_package(self, package: &'static str) -> Self { + self._arg(opt("package", package).short("p").value_name("SPEC")) + } + fn arg_jobs(self) -> Self { self._arg( opt("jobs", "Number of parallel jobs, defaults to # of CPUs") diff --git a/src/bin/cli/pkgid.rs b/src/bin/cli/pkgid.rs index 5c4cd8442..81ade796a 100644 --- a/src/bin/cli/pkgid.rs +++ b/src/bin/cli/pkgid.rs @@ -4,10 +4,7 @@ pub fn cli() -> App { subcommand("pkgid") .about("Print a fully qualified package specification") .arg(Arg::with_name("spec")) - .arg( - opt("package", "Argument to get the package id specifier for") - .short("p").value_name("SPEC") - ) + .arg_single_package("Argument to get the package id specifier for") .arg_manifest_path() .after_help("\ Given a argument, print out the fully qualified package id specifier. diff --git a/src/bin/cli/run.rs b/src/bin/cli/run.rs index 4f034fbdd..8b2bee2bd 100644 --- a/src/bin/cli/run.rs +++ b/src/bin/cli/run.rs @@ -11,10 +11,7 @@ pub fn cli() -> App { "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_single_package("Package with the target to run") .arg_jobs() .arg_release("Build artifacts in release mode, with optimizations") .arg_features() diff --git a/src/bin/cli/rustc.rs b/src/bin/cli/rustc.rs index 726d1eb22..01315415b 100644 --- a/src/bin/cli/rustc.rs +++ b/src/bin/cli/rustc.rs @@ -7,10 +7,7 @@ pub fn cli() -> App { .setting(AppSettings::TrailingVarArg) .about("Compile a package and all of its dependencies") .arg(Arg::with_name("args").multiple(true)) - .arg( - opt("package", "Package to build") - .short("p").value_name("SPEC") - ) + .arg_single_package("Package to build") .arg_jobs() .arg_targets_all( "Build only this package's library", diff --git a/src/bin/cli/rustdoc.rs b/src/bin/cli/rustdoc.rs index 031dbbbd1..8570b029e 100644 --- a/src/bin/cli/rustdoc.rs +++ b/src/bin/cli/rustdoc.rs @@ -8,10 +8,7 @@ pub fn cli() -> App { .about("Build a package's documentation, using specified custom flags.") .arg(Arg::with_name("args").multiple(true)) .arg(opt("open", "Opens the docs in a browser after the operation")) - .arg( - opt("package", "Package to document") - .short("p").value_name("SPEC") - ) + .arg_single_package("Package to document") .arg_jobs() .arg_targets_all( "Build only this package's library", -- 2.30.2