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 shell completions using clap_complete #735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
line-numbers = "0.3.0"
smallvec = "1.13.2"
clap_complete = "3.2.5"

[dev-dependencies]
# assert_cmd 2.0.10 requires predicates 3.
Expand Down
14 changes: 13 additions & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use clap::{crate_authors, crate_description, Arg, Command};
use clap_complete::{generate, Shell};
use const_format::formatcp;
use crossterm::tty::IsTty;
use itertools::Itertools;
Expand Down Expand Up @@ -292,6 +293,13 @@ When multiple overrides are specified, the first matching override wins."))
.validator(|s| s.parse::<usize>())
.required(false),
)
.arg(
Arg::new("completion").long("completion")
.takes_value(true)
.value_name("SHELL")
.value_parser(clap::value_parser!(Shell))
.help("Generate completion for a given shell")
)
.arg(
Arg::new("paths")
.value_name("PATHS")
Expand Down Expand Up @@ -603,7 +611,11 @@ fn parse_overrides_or_die(raw_overrides: &[String]) -> Vec<(LanguageOverride, Ve
/// Parse CLI arguments passed to the binary.
pub(crate) fn parse_args() -> Mode {
let matches = app().get_matches();

if let Some(shell) = matches.get_one::<Shell>("completion") {
generate(*shell, &mut app(), "difft", &mut std::io::stdout());
// TODO: What should the exit code be? Should it be defined in src/exit_codes.rs
std::process::exit(0);
}
let color_output = match matches.value_of("color").expect("color has a default") {
"always" => ColorOutput::Always,
"never" => ColorOutput::Never,
Expand Down
Loading