Don't copy-paste package argument
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 8 Mar 2018 10:26:01 +0000 (13:26 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 8 Mar 2018 20:30:47 +0000 (23:30 +0300)
src/bin/cli/mod.rs
src/bin/cli/pkgid.rs
src/bin/cli/run.rs
src/bin/cli/rustc.rs
src/bin/cli/rustdoc.rs

index a1b0125cef7a4fb8cd2c4422f1baae0306ec2ae4..c5ab7551dfad90d52245f20048ef6ccdfdcc4cc8 100644 (file)
@@ -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<CompileOptions<'a>> {
+        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<NewOptions<'a>> {
         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")
index 5c4cd84424b010e6bfb63bafee11475870f8e881..81ade796a33f38f68f3cc69c12c004165a0d81fd 100644 (file)
@@ -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 <spec> argument, print out the fully qualified package id specifier.
index 4f034fbddca4e594aae5c9da8185906cf5040a7e..8b2bee2bd428f81b0fd2adf213a13324bac4e044 100644 (file)
@@ -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()
index 726d1eb22a342f2ad41412ea530ad5f27ae1b95e..01315415b400e1d7ee09c85b863591fe4d3b5871 100644 (file)
@@ -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",
index 031dbbbd128316782fafc62678628d37d5046eb1..8570b029eda8e138fe230374b0eb07e46b027f5c 100644 (file)
@@ -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",