Repository: kbknapp/clap-rs
Pull request #1182 (2018-02-13 21:49:23)

V3 dev
backporting issues/features from 2.x:

* #1155 
* #1179

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/kbknapp/clap-rs/1182)
<!-- Reviewable:end -->



Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 3aa321ea..7cef81e0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -53,13 +53,15 @@ unicode-width         = "0.1.4"
 textwrap              = "0.9.0"
 ordermap			  = "0.3.5"
 strsim    = { version = "0.7.0",  optional = true }
-ansi_term = { version = "0.10.0",  optional = true }
 yaml-rust = { version = "0.3.5",  optional = true }
 clippy    = { version = "~0.0.166", optional = true }
 atty      = { version = "0.2.2",  optional = true }
 vec_map   = { version = "0.8", optional = true }
 term_size = { version = "0.3.0", optional = true }
 
+[target.'cfg(not(windows))'.dependencies]
+ansi_term = { version = "0.10.0",  optional = true }
+
 [dev-dependencies]
 regex = "0.2"
 lazy_static = "1"
diff --git a/src/completions/bash.rs b/src/completions/bash.rs
index 0eca9cf8..c3db11e4 100644
--- a/src/completions/bash.rs
+++ b/src/completions/bash.rs
@@ -3,7 +3,7 @@ use std::io::Write;
 
 // Internal
 use app::App;
-use args::{Arg, ArgSettings};
+use args::Arg;
 use completions;
 
 pub struct BashGen<'a, 'b>(&'b App<'a, 'b>)
@@ -163,53 +163,13 @@ complete -F _{name} -o bashdefault -o default {name}
 
     fn vals_for(&self, o: &Arg) -> String {
         debugln!("BashGen::vals_for: o={}", o.name);
-        let mut ret = String::new();
-        let mut needs_quotes = true;
         if let Some(ref vals) = o.possible_vals {
-            needs_quotes = false;
-            ret = format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "));
-        } else if let Some(ref vec) = o.val_names {
-            let mut it = vec.iter().peekable();
-            while let Some((_, val)) = it.next() {
-                ret = format!(
-                    "{}<{}>{}",
-                    ret,
-                    val,
-                    if it.peek().is_some() { " " } else { "" }
-                );
-            }
-            let num = vec.len();
-            if (o.is_set(ArgSettings::MultipleValues) || o.is_set(ArgSettings::MultipleOccurrences))
-                && num == 1
-            {
-                ret = format!("{}...", ret);
-            }
-        } else if let Some(num) = o.num_vals {
-            let mut it = (0..num).peekable();
-            while let Some(_) = it.next() {
-                ret = format!(
-                    "{}<{}>{}",
-                    ret,
-                    o.name,
-                    if it.peek().is_some() { " " } else { "" }
-                );
-            }
-            if (o.is_set(ArgSettings::MultipleValues) || o.is_set(ArgSettings::MultipleOccurrences))
-                && num == 1
-            {
-                ret = format!("{}...", ret);
-            }
+            format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "))
         } else {
-            ret = format!("<{}>", o.name);
-            if o.is_set(ArgSettings::MultipleValues) || o.is_set(ArgSettings::MultipleOccurrences) {
-                ret = format!("{}...", ret);
-            }
+            String::from("$(compgen -f ${cur})")
         }
-        if needs_quotes {
-            ret = format!("\"{}\"", ret);
-        }
-        ret
     }
+
     fn all_options_for_path(&self, path: &str) -> String {
         debugln!("BashGen::all_options_for_path: path={}", path);
         let mut p = self.0;
diff --git a/src/lib.rs b/src/lib.rs
index 6e455c90..6f289ad3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -530,7 +530,7 @@
 #![cfg_attr(feature = "lints", allow(doc_markdown))]
 #![cfg_attr(feature = "lints", allow(explicit_iter_loop))]
 
-#[cfg(feature = "color")]
+#[cfg(all(feature = "color", not(target_os = "windows")))]
 extern crate ansi_term;
 #[cfg(feature = "color")]
 extern crate atty;


Test patch:
diff --git a/tests/completions.rs b/tests/completions.rs
index 504d271c..b6ad74ef 100644
--- a/tests/completions.rs
+++ b/tests/completions.rs
@@ -71,7 +71,7 @@ static BASH: &'static str = r#"_myapp() {
             case "${prev}" in
                 
                 --case)
-                    COMPREPLY=("<case>")
+                    COMPREPLY=($(compgen -f ${cur}))
                     return 0
                     ;;
                 *)
@@ -189,8 +189,8 @@ complete -c myapp -n "__fish_using_command myapp help" -s h -l help -d 'Prints h
 complete -c myapp -n "__fish_using_command myapp help" -s V -l version -d 'Prints version information'
 "#;
 
-#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
-#[cfg(not(target_os = "windows"))]
+#[allow(dead_code)]
+#[cfg(not(target_os="windows"))]
 static POWERSHELL: &'static str = r#"
 @('myapp', './myapp') | %{
     Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
@@ -241,8 +241,8 @@ static POWERSHELL: &'static str = r#"
 }
 "#;
 
-#[cfg_attr(target_os = "windows", allow(dead_code))]
-#[cfg(target_os = "windows")]
+#[allow(dead_code)]
+#[cfg(target_os="windows")]
 static POWERSHELL: &'static str = r#"
 @('myapp', './myapp', 'myapp.exe', '.\myapp', '.\myapp.exe', './myapp.exe') | %{
     Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
@@ -282,8 +282,8 @@ static POWERSHELL: &'static str = r#"
 }
 "#;
 
-#[cfg_attr(not(target_os = "windows"), allow(dead_code))]
-#[cfg(not(target_os = "windows"))]
+#[allow(dead_code)]
+#[cfg(not(target_os="windows"))]
 static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
 @('my_app', './my_app') | %{
     Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
@@ -343,8 +343,8 @@ static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
 }
 "#;
 
-#[cfg_attr(target_os = "windows", allow(dead_code))]
-#[cfg(target_os = "windows")]
+#[allow(dead_code)]
+#[cfg(target_os="windows")]
 static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
 @('my_app', './my_app', 'my_app.exe', '.\my_app', '.\my_app.exe', './my_app.exe') | %{
     Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
@@ -621,7 +621,7 @@ static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
             case "${prev}" in
                 
                 --config)
-                    COMPREPLY=("<config>")
+                    COMPREPLY=($(compgen -f ${cur}))
                     return 0
                     ;;
                 *)
@@ -640,7 +640,7 @@ static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
             case "${prev}" in
                 
                 --case)
-                    COMPREPLY=("<case>")
+                    COMPREPLY=($(compgen -f ${cur}))
                     return 0
                     ;;
                 *)
@@ -742,63 +742,45 @@ fn build_app_with_name(s: &'static str) -> App<'static, 'static> {
     App::new(s)
         .about("Tests completions")
         .arg(Arg::with_name("file").help("some input file"))
-        .subcommand(
-            SubCommand::with_name("test").about("tests things").arg(
-                Arg::with_name("case")
-                    .long("case")
-                    .takes_value(true)
-                    .help("the case to test"),
-            ),
-        )
+        .subcommand(SubCommand::with_name("test")
+            .about("tests things")
+            .arg(Arg::with_name("case")
+                .long("case")
+                .takes_value(true)
+                .help("the case to test")))
 }
 
 fn build_app_special_commands() -> App<'static, 'static> {
     build_app_with_name("my_app")
-        .subcommand(
-            SubCommand::with_name("some_cmd")
-                .about("tests other things")
-                .arg(
-                    Arg::with_name("config")
-                        .long("--config")
-                        .takes_value(true)
-                        .help("the other case to test"),
-                ),
-        )
+        .subcommand(SubCommand::with_name("some_cmd")
+                    .about("tests other things")
+                    .arg(Arg::with_name("config")
+                         .long("--config")
+                         .takes_value(true)
+                         .help("the other case to test")))
         .subcommand(SubCommand::with_name("some-cmd-with-hypens"))
 }
 
 fn build_app_special_help() -> App<'static, 'static> {
     App::new("my_app")
-        .arg(
-            Arg::with_name("single-quotes")
-                .long("single-quotes")
-                .help("Can be 'always', 'auto', or 'never'"),
-        )
-        .arg(
-            Arg::with_name("double-quotes")
-                .long("double-quotes")
-                .help("Can be \"always\", \"auto\", or \"never\""),
-        )
-        .arg(
-            Arg::with_name("backticks")
-                .long("backticks")
-                .help("For more information see `echo test`"),
-        )
-        .arg(
-            Arg::with_name("backslash")
-                .long("backslash")
-                .help("Avoid '\\n'"),
-        )
-        .arg(
-            Arg::with_name("brackets")
-                .long("brackets")
-                .help("List packages [filter]"),
-        )
-        .arg(
-            Arg::with_name("expansions")
-                .long("expansions")
-                .help("Execute the shell command with $SHELL"),
-        )
+        .arg(Arg::with_name("single-quotes")
+            .long("single-quotes")
+            .help("Can be 'always', 'auto', or 'never'"))
+        .arg(Arg::with_name("double-quotes")
+            .long("double-quotes")
+            .help("Can be \"always\", \"auto\", or \"never\""))
+        .arg(Arg::with_name("backticks")
+            .long("backticks")
+            .help("For more information see `echo test`"))
+        .arg(Arg::with_name("backslash")
+            .long("backslash")
+            .help("Avoid '\\n'"))
+        .arg(Arg::with_name("brackets")
+            .long("brackets")
+            .help("List packages [filter]"))
+        .arg(Arg::with_name("expansions")
+            .long("expansions")
+            .help("Execute the shell command with $SHELL"))
 }
 
 #[test]