Skip to content

Commit 35d9c66

Browse files
committed
Add shell completions using clap_complete
1 parent 4ab6494 commit 35d9c66

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

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
@@ -39,6 +39,7 @@ pkg-fmt = "zip"
3939
[dependencies]
4040
regex = "1.10.4"
4141
clap = { version = "4.0.0", features = ["cargo", "env", "wrap_help", "string"] }
42+
clap_complete = "4.5.67"
4243
typed-arena = "2.0.2"
4344
rustc-hash = "2.0.0"
4445
strsim = "0.11.0"

src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::fmt::Display;
66
use std::path::{Path, PathBuf};
77

88
use clap::{crate_authors, crate_description, value_parser, Arg, ArgAction, Command};
9+
use clap_complete::{generate, Shell};
910
use crossterm::tty::IsTty;
1011
use owo_colors::OwoColorize as _;
1112

@@ -360,6 +361,12 @@ Higher values will allow difftastic to perform a structural diff in more cases.
360361
.value_parser(clap::value_parser!(usize))
361362
.required(false),
362363
)
364+
.arg(
365+
Arg::new("completion").long("completion")
366+
.value_name("SHELL")
367+
.value_parser(clap::value_parser!(Shell))
368+
.help("Generate completion for a given shell")
369+
)
363370
.arg(
364371
Arg::new("paths")
365372
.value_name("PATHS")
@@ -714,6 +721,12 @@ fn parse_binary_overrides_or_die(glob_strs: &[String]) -> Vec<glob::Pattern> {
714721
pub(crate) fn parse_args() -> Mode {
715722
let matches = app().get_matches();
716723

724+
if let Some(shell) = matches.get_one::<Shell>("completion") {
725+
generate(*shell, &mut app(), "difft", &mut std::io::stdout());
726+
// TODO: What should the exit code be? Should it be defined in src/exit_codes.rs
727+
std::process::exit(0);
728+
}
729+
717730
let color_output = match matches
718731
.get_one::<String>("color")
719732
.map(|s| s.as_str())

0 commit comments

Comments
 (0)