Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to generate shell completions #2507

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add command to generate shell completions
  • Loading branch information
pvdrz committed Apr 17, 2023
commit 4d28f4f1c177a861cbe46562d0841c571fc91bf9
10 changes: 10 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