Skip to content

Commit 3e5fd8f

Browse files
committed
Add shell completions using clap_complete
This uses the last 3.x clap_complete version, which matches the currently used clap version (regarding MSRV).
1 parent 7248e33 commit 3e5fd8f

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ serde = { version = "1.0", features = ["derive"] }
8282
serde_json = "1.0"
8383
line-numbers = "0.3.0"
8484
smallvec = "1.13.2"
85+
clap_complete = "3.2.5"
8586

8687
[dev-dependencies]
8788
# assert_cmd 2.0.10 requires predicates 3.

src/options.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
};
99

1010
use clap::{crate_authors, crate_description, Arg, Command};
11+
use clap_complete::{generate, Shell};
1112
use const_format::formatcp;
1213
use crossterm::tty::IsTty;
1314
use itertools::Itertools;
@@ -292,6 +293,13 @@ When multiple overrides are specified, the first matching override wins."))
292293
.validator(|s| s.parse::<usize>())
293294
.required(false),
294295
)
296+
.arg(
297+
Arg::new("completion").long("completion")
298+
.takes_value(true)
299+
.value_name("SHELL")
300+
.value_parser(clap::value_parser!(Shell))
301+
.help("Generate completion for a given shell")
302+
)
295303
.arg(
296304
Arg::new("paths")
297305
.value_name("PATHS")
@@ -603,7 +611,11 @@ fn parse_overrides_or_die(raw_overrides: &[String]) -> Vec<(LanguageOverride, Ve
603611
/// Parse CLI arguments passed to the binary.
604612
pub(crate) fn parse_args() -> Mode {
605613
let matches = app().get_matches();
606-
614+
if let Some(shell) = matches.get_one::<Shell>("completion") {
615+
generate(*shell, &mut app(), "difft", &mut std::io::stdout());
616+
// TODO: What should the exit code be? Should it be defined in src/exit_codes.rs
617+
std::process::exit(0);
618+
}
607619
let color_output = match matches.value_of("color").expect("color has a default") {
608620
"always" => ColorOutput::Always,
609621
"never" => ColorOutput::Never,

0 commit comments

Comments
 (0)