Skip to content

Commit

Permalink
Add command to generate shell completions (rust-lang#2507)
Browse files Browse the repository at this point in the history
Add the `--generate-shell-completions=shell` command which generates completions for the given shell, emits the completion script to `stdout` and exits. The supported shells are: `bash`, `elvish`, `fish`, `powershell` and `zsh`. We depend on the `clap_complete` crate so we can only support the shells they support.

For example, to generate `bash` completions you can run:

```bash
$ bindgen --generate-shell-completions=bash > /usr/share/bash-completion/completions/bindgen.bash
```
  • Loading branch information
pvdrz authored Apr 17, 2023
1 parent e6a9d70 commit ae87b33
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@

* Added the `--generate-cstr` CLI flag to generate string constants as `&CStr`
instead of `&[u8]`. (Requires Rust 1.59 or higher.)
* Added the `--generate-shell-completions` CLI flag to generate completions for
different shells.

## Changed

Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindgen-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ name = "bindgen"
bindgen = { path = "../bindgen", version = "=0.65.1", features = ["__cli", "experimental"] }
shlex = "1"
clap = { version = "4", features = ["derive"] }
clap_complete = "4"
env_logger = { version = "0.10.0", optional = true }
log = { version = "0.4", optional = true }

Expand Down
18 changes: 17 additions & 1 deletion bindgen-cli/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use bindgen::{
FieldVisibilityKind, Formatter, MacroTypeVariation, NonCopyUnionStyle,
RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, RUST_TARGET_STRINGS,
};
use clap::Parser;
use clap::{CommandFactory, Parser};
use std::fs::File;
use std::io::{self, Error, ErrorKind};
use std::path::PathBuf;
use std::process::exit;

fn rust_target_help() -> String {
format!(
Expand Down Expand Up @@ -376,6 +377,9 @@ struct BindgenCommand {
/// Whether to emit diagnostics or not.
#[arg(long, requires = "experimental")]
emit_diagnostics: bool,
/// Generates completions for the specified SHELL, sends them to `stdout` and exits.
#[arg(long, value_name = "SHELL")]
generate_shell_completions: Option<clap_complete::Shell>,
/// Enables experimental features.
#[arg(long)]
experimental: bool,
Expand Down Expand Up @@ -504,11 +508,23 @@ where
wrap_static_fns_suffix,
default_visibility,
emit_diagnostics,
generate_shell_completions,
experimental: _,
version,
clang_args,
} = command;

if let Some(shell) = generate_shell_completions {
clap_complete::generate(
shell,
&mut BindgenCommand::command(),
"bindgen",
&mut std::io::stdout(),
);

exit(0);
}

if version {
println!(
"bindgen {}",
Expand Down
1 change: 1 addition & 0 deletions bindgen-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bindgen = { path = "../bindgen", features = ["__cli", "experimental"] }
diff = "0.1"
shlex = "1"
clap = { version = "4", features = ["derive"] }
clap_complete = "4"
tempfile = "3"

[features]
Expand Down

0 comments on commit ae87b33

Please sign in to comment.