Don't copy-paste index argument
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 8 Mar 2018 10:31:21 +0000 (13:31 +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/publish.rs

index c5ab7551dfad90d52245f20048ef6ccdfdcc4cc8..08d67fe82c7c30541fd96ffce533b0946459d8ca 100644 (file)
@@ -165,6 +165,33 @@ pub fn do_main(config: &mut Config) -> Result<(), CliError> {
         }
     }
 
+    fn index_from_args(config: &Config, args: &ArgMatches) -> CargoResult<Option<String>> {
+        // TODO: Deprecated
+        // remove once it has been decided --host can be removed
+        // We may instead want to repurpose the host flag, as
+        // mentioned in this issue
+        // https://github.com/rust-lang/cargo/issues/4208
+        let msg = "The flag '--host' is no longer valid.
+
+Previous versions of Cargo accepted this flag, but it is being
+deprecated. The flag is being renamed to 'index', as the flag
+wants the location of the index to which to publish. Please
+use '--index' instead.
+
+This will soon become a hard error, so it's either recommended
+to update to a fixed version or contact the upstream maintainer
+about this warning.";
+
+        let index = match args.value_of("host") {
+            Some(host) => {
+                config.shell().warn(&msg)?;
+                Some(host.to_string())
+            }
+            None => args.value_of("index").map(|s| s.to_string())
+        };
+        Ok(index)
+    }
+
     config_from_args(config, &args)?;
     match args.subcommand() {
         ("bench", Some(args)) => {
@@ -429,30 +456,7 @@ pub fn do_main(config: &mut Config) -> Result<(), CliError> {
         ("publish", Some(args)) => {
             let registry = registry_from_args(config, args)?;
             let ws = workspace_from_args(config, args)?;
-
-            // TODO: Deprecated
-            // remove once it has been decided --host can be removed
-            // We may instead want to repurpose the host flag, as
-            // mentioned in this issue
-            // https://github.com/rust-lang/cargo/issues/4208
-            let msg = "The flag '--host' is no longer valid.
-
-Previous versions of Cargo accepted this flag, but it is being
-deprecated. The flag is being renamed to 'index', as the flag
-wants the location of the index to which to publish. Please
-use '--index' instead.
-
-This will soon become a hard error, so it's either recommended
-to update to a fixed version or contact the upstream maintainer
-about this warning.";
-
-            let index = match args.value_of("host") {
-                Some(host) => {
-                    config.shell().warn(&msg)?;
-                    Some(host.to_string())
-                }
-                None => args.value_of("index").map(|s| s.to_string())
-            };
+            let index = index_from_args(config, args)?;
 
             ops::publish(&ws, &ops::PublishOpts {
                 config,
@@ -791,6 +795,19 @@ a global configuration.")
                         .value_name("NAME")
                 )
         }
+
+        fn arg_index(self) -> Self {
+            self
+                ._arg(
+                    opt("index", "Registry index to upload the package to")
+                        .value_name("INDEX")
+                )
+                ._arg(
+                    opt("host", "DEPRECATED, renamed to '--index'")
+                        .value_name("HOST")
+                        .hidden(true)
+                )
+        }
     }
 
     impl CommonArgs for App {
index 17ffa97cfaa296e56c9252675eefa2586ff836cd..f52f0ee598db6eb6be110def3081c8b03d7a011c 100644 (file)
@@ -3,15 +3,7 @@ use super::utils::*;
 pub fn cli() -> App {
     subcommand("publish")
         .about("Upload a package to the registry")
-        .arg(
-            opt("index", "Registry index to upload the package to")
-                .value_name("INDEX")
-        )
-        .arg(
-            opt("host", "DEPRECATED, renamed to '--index'")
-                .value_name("HOST")
-                .hidden(true)
-        )
+        .arg_index()
         .arg(opt("token", "Token to use when uploading").value_name("TOKEN"))
         .arg(opt("no-verify", "Don't verify the contents by building them"))
         .arg(opt("allow-dirty", "Allow dirty working directories to be packaged"))