Skip to content

Commit

Permalink
Merge pull request clap-rs#1180 from kbknapp/bash-completions
Browse files Browse the repository at this point in the history
Bash completions
  • Loading branch information
kbknapp authored Feb 13, 2018
2 parents 5b373f5 + 7428ea8 commit ebe1455
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 90 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<a name="v2.30.0"></a>
## v2.30.0 (2018-02-13)

#### Bug Fixes

* **YAML:** Adds a missing conversion from `Arg::last` when instantiating from a YAML file ([aab77c81a5](https://github.com/kbknapp/clap-rs/pull/1175/commits/aab77c81a519b045f95946ae0dd3e850f9b93070), closes [#1160](https://github.com/kbknapp/clap-rs/issues/1173))

#### Improvements

* **Bash Completions:** instead of completing a generic option name, all bash completions fall back to file completions UNLESS `Arg::possible_values` was used ([872f02ae](https://github.com/kbknapp/clap-rs/commit/872f02aea900ffa376850a279eb164645e1234fa))
* **Deps:** No longer needlessly compiles `ansi_term` on Windows since its not used ([b57ee946](https://github.com/kbknapp/clap-rs/commit/b57ee94609da3ddc897286cfba968f26ff961491), closes [#1155](https://github.com/kbknapp/clap-rs/issues/1155))
* **Help Message:** changes the `[values: foo bar baz]` array to `[possible values: foo bar baz]` for consistency with the API ([414707e4e97](https://github.com/kbknapp/clap-rs/pull/1176/commits/414707e4e979d07bfe555247e5d130c546673708), closes [#1160](https://github.com/kbknapp/clap-rs/issues/1160))


<a name="v2.29.4"></a>
### v2.29.4 (2018-02-06)

Expand Down
54 changes: 27 additions & 27 deletions CONTRIBUTORS.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "clap"
version = "2.29.4"
version = "2.30.0"
authors = ["Kevin K. <[email protected]>"]
exclude = ["examples/*", "clap-test/*", "tests/*", "benches/*", "*.png", "clap-perf/*", "*.dot"]
repository = "https://github.com/kbknapp/clap-rs"
Expand Down
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,27 @@ Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)

## What's New

Here's whats new in 2.29.4:
Here's whats new in 2.30.0:

* **Overrides Self:** fixes a bug where options with multiple values couldnt ever have multiple values ([d95907cf](https://github.com/kbknapp/clap-rs/commit/d95907cff6d011a901fe35fa00b0f4e18547a1fb))
* **Bash Completions:** instead of completing a generic option name, all bash completions fall back to file completions UNLESS `Arg::possible_values` was used
* **YAML:** Adds a missing conversion from `Arg::last` when instantiating from a YAML file
* **Deps:** No longer needlessly compiles `ansi_term` on Windows since its not used
* **Help Message:** changes the `[values: foo bar baz]` array to `[possible values: foo bar baz]` for consistency with the API

Here's whats new in 2.29.3:
Here's whats new in 2.29.x:

* **Overrides Self:** fixes a bug where options with multiple values couldnt ever have multiple values ([d95907cf](https://github.com/kbknapp/clap-rs/commit/d95907cff6d011a901fe35fa00b0f4e18547a1fb))
* **Self Overrides:** now supports arguments which override themselves (Allows true shell aliases, config files, etc!)
* **Requirements:** fixes an issue where conflicting args would still show up as required and missing
* Fixes a bug which disallows proper nesting of `--` for args that capture all values after the first `--`
* **AppSettings::AllArgsOverrideSelf:** adds a new convenience setting to allow all args to override themselves

Here's whats new in 2.29.2:

* **Many ZSH Completions Improvements** (Thanks to @segevfiner)
* Positional arguments will default to file completion when not using specific values!
* Implement postional argument possible values completion
* Removes redundant code from output
* Don't pass `-S` to `_arguments` if Zsh is too old
* Fix completions with mixed positionals and subcommands
* String escape possible values for options


Here's whats new in 2.29.1:

* Debloats clap by deduplicating logic and refactors for a ~57% decrease in code size! This is with zero functinoality lost, and a slight perf increase!
* Change the bash completion script code generation to support hyphens.
* Fix completion of long option values in ZSH completions
Expand Down Expand Up @@ -312,7 +309,7 @@ subcommands:
Since this feature requires additional dependencies that not everyone may want, it is *not* compiled in by default and we need to enable a feature flag in Cargo.toml:
Simply change your `clap = "2.29"` to `clap = {version = "2.29", features = ["yaml"]}`.
Simply change your `clap = "2.30"` to `clap = {version = "2.30", features = ["yaml"]}`.

Finally we create our `main.rs` file just like we would have with the previous two examples:

Expand Down Expand Up @@ -432,7 +429,7 @@ For full usage, add `clap` as a dependency in your `Cargo.toml` () to use from c

```toml
[dependencies]
clap = "~2.29"
clap = "~2.30"
```

(**note**: If you are concerned with supporting a minimum version of Rust that is *older* than the current stable Rust minus 2 stable releases, it's recommended to use the `~major.minor.patch` style versions in your `Cargo.toml` which will only update the patch version automatically. For more information see the [Compatibility Policy](#compatibility-policy))
Expand All @@ -448,22 +445,22 @@ Then run `cargo build` or `cargo update && cargo build` for your project.
#### Features enabled by default

* **"suggestions"**: Turns on the `Did you mean '--myoption'?` feature for when users make typos. (builds dependency `strsim`)
* **"color"**: Turns on colored error messages. This feature only works on non-Windows OSs. (builds dependency `ansi-term`)
* **"color"**: Turns on colored error messages. This feature only works on non-Windows OSs. (builds dependency `ansi-term` only on non-Windows targets)
* **"vec_map"**: Use [`VecMap`](https://crates.io/crates/vec_map) internally instead of a [`BTreeMap`](https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html). This feature provides a _slight_ performance improvement. (builds dependency `vec_map`)

To disable these, add this to your `Cargo.toml`:

```toml
[dependencies.clap]
version = "2.29"
version = "2.30"
default-features = false
```

You can also selectively enable only the features you'd like to include, by adding:

```toml
[dependencies.clap]
version = "2.29"
version = "2.30"
default-features = false
# Cherry-pick the features you'd like to use
Expand Down Expand Up @@ -511,7 +508,7 @@ In order to keep from being surprised of breaking changes, it is **highly** reco

```toml
[dependencies]
clap = "~2.29"
clap = "~2.30"
```

This will cause *only* the patch version to be updated upon a `cargo update` call, and therefore cannot break due to new features, or bumped minimum versions of Rust.
Expand All @@ -528,11 +525,11 @@ Right now Cargo's version resolution is pretty naive, it's just a brute-force se
# In one Cargo.toml
[dependencies]
clap = "~2.29.0"
clap = "~2.30.0"
# In another Cargo.toml
[dependencies]
clap = "2.29"
clap = "2.30"
```

This is inherently an unresolvable crate graph in Cargo right now. Cargo requires there's only one major version of a crate, and being in the same workspace these two crates must share a version. This is impossible in this location, though, as these version constraints cannot be met.
Expand Down
44 changes: 4 additions & 40 deletions src/completions/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::Write;

// Internal
use app::parser::Parser;
use args::{ArgSettings, OptBuilder};
use args::OptBuilder;
use completions;

pub struct BashGen<'a, 'b>
Expand Down Expand Up @@ -168,49 +168,13 @@ complete -F _{name} -o bashdefault -o default {name}
fn vals_for(&self, o: &OptBuilder) -> String {
debugln!("BashGen::vals_for: o={}", o.b.name);
use args::AnyArg;
let mut ret = String::new();
let mut needs_quotes = true;
if let Some(vals) = o.possible_vals() {
needs_quotes = false;
ret = format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "));
} else if let Some(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::Multiple) && 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::Multiple) && num == 1 {
ret = format!("{}...", ret);
}
format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "))
} else {
ret = format!("<{}>", o.name());
if o.is_set(ArgSettings::Multiple) {
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.p;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@
//! this repository for more information.
#![crate_type = "lib"]
#![doc(html_root_url = "https://docs.rs/clap/2.29.4")]
#![doc(html_root_url = "https://docs.rs/clap/2.30.0")]
#![deny(missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts,
unused_import_braces, unused_allocation)]
// Lints we'd like to deny but are currently failing for upstream crates
Expand Down
10 changes: 7 additions & 3 deletions tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static BASH: &'static str = r#"_myapp() {
case "${prev}" in
--case)
COMPREPLY=("<case>")
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
Expand Down Expand Up @@ -189,6 +189,7 @@ 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'
"#;

#[allow(dead_code)]
#[cfg(not(target_os="windows"))]
static POWERSHELL: &'static str = r#"
@('myapp', './myapp') | %{
Expand Down Expand Up @@ -240,6 +241,7 @@ static POWERSHELL: &'static str = r#"
}
"#;

#[allow(dead_code)]
#[cfg(target_os="windows")]
static POWERSHELL: &'static str = r#"
@('myapp', './myapp', 'myapp.exe', '.\myapp', '.\myapp.exe', './myapp.exe') | %{
Expand Down Expand Up @@ -280,6 +282,7 @@ static POWERSHELL: &'static str = r#"
}
"#;

#[allow(dead_code)]
#[cfg(not(target_os="windows"))]
static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
@('my_app', './my_app') | %{
Expand Down Expand Up @@ -340,6 +343,7 @@ static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
}
"#;

#[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') | %{
Expand Down Expand Up @@ -617,7 +621,7 @@ static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
case "${prev}" in
--config)
COMPREPLY=("<config>")
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
Expand All @@ -636,7 +640,7 @@ static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
case "${prev}" in
--case)
COMPREPLY=("<case>")
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
Expand Down

0 comments on commit ebe1455

Please sign in to comment.