Move `index` & `registry` to `ArgMatchesExt`
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 10 Mar 2018 14:08:39 +0000 (17:08 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 10 Mar 2018 14:08:39 +0000 (17:08 +0300)
src/bin/cli.rs
src/bin/command_prelude.rs

index 7c8e5b31b53274d81a12c7fac2f09e391c9c3ec3..7e5c45538f24098e31dc1d2fd3e40ae88decc08c 100644 (file)
@@ -242,7 +242,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
             Ok(())
         }
         ("login", Some(args)) => {
-            let registry = registry_from_args(config, args)?;
+            let registry = args.registry(config)?;
 
             let token = match args.value_of("token") {
                 Some(token) => token.to_string(),
@@ -308,7 +308,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
             Ok(())
         }
         ("owner", Some(args)) => {
-            let registry = registry_from_args(config, args)?;
+            let registry = args.registry(config)?;
             let opts = ops::OwnersOptions {
                 krate: args.value_of("crate").map(|s| s.to_string()),
                 token: args.value_of("token").map(|s| s.to_string()),
@@ -345,9 +345,9 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
             Ok(())
         }
         ("publish", Some(args)) => {
-            let registry = registry_from_args(config, args)?;
+            let registry = args.registry(config)?;
             let ws = args.workspace(config)?;
-            let index = index_from_args(config, args)?;
+            let index = args.index(config)?;
 
             ops::publish(&ws, &ops::PublishOpts {
                 config,
@@ -435,8 +435,8 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
             Ok(())
         }
         ("search", Some(args)) => {
-            let registry = registry_from_args(config, args)?;
-            let index = index_from_args(config, args)?;
+            let registry = args.registry(config)?;
+            let index = args.index(config)?;
             let limit: Option<u8> = args.value_of("limit")
                 .and_then(|v| v.parse().ok()); //FIXME: validation
             let limit = min(100, limit.unwrap_or(10));
@@ -535,7 +535,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
             Ok(())
         }
         ("yank", Some(args)) => {
-            let registry = registry_from_args(config, args)?;
+            let registry = args.registry(config)?;
 
             ops::yank(config,
                       args.value_of("crate").map(|s| s.to_string()),
index a51be4a0b7ce8ba34e571cb991dde7ceed45be37..569c8423b8df0ef3822d6a63af27e7772bfb695b 100644 (file)
@@ -270,6 +270,45 @@ pub trait ArgMatchesExt {
                         self._value_of("name").map(|s| s.to_string()))
     }
 
+    fn registry(&self, config: &Config) -> CargoResult<Option<String>> {
+        match self._value_of("registry") {
+            Some(registry) => {
+                if !config.cli_unstable().unstable_options {
+                    return Err(format_err!("registry option is an unstable feature and \
+                            requires -Zunstable-options to use.").into());
+                }
+                Ok(Some(registry.to_string()))
+            }
+            None => Ok(None),
+        }
+    }
+
+    fn index(&self, config: &Config) -> 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. 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 self._value_of("host") {
+            Some(host) => {
+                config.shell().warn(&msg)?;
+                Some(host.to_string())
+            }
+            None => self._value_of("index").map(|s| s.to_string())
+        };
+        Ok(index)
+    }
+
     fn _value_of(&self, name: &str) -> Option<&str>;
 
     fn _values_of(&self, name: &str) -> Vec<String>;
@@ -298,42 +337,3 @@ pub fn values(args: &ArgMatches, name: &str) -> Vec<String> {
         .map(|s| s.to_string())
         .collect()
 }
-
-pub fn registry_from_args(config: &Config, args: &ArgMatches) -> CargoResult<Option<String>> {
-    match args.value_of("registry") {
-        Some(registry) => {
-            if !config.cli_unstable().unstable_options {
-                return Err(format_err!("registry option is an unstable feature and \
-                            requires -Zunstable-options to use.").into());
-            }
-            Ok(Some(registry.to_string()))
-        }
-        None => Ok(None),
-    }
-}
-
-pub 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. 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)
-}