Skip to content

Commit fd75cb6

Browse files
lutterclaude
andcommitted
gnd: Add shell completions command
Add a `completions` subcommand that generates shell completion scripts for bash, elvish, fish, powershell, and zsh. Users can install completions by adding the output to their shell configuration. Example usage: gnd completions bash >> ~/.bashrc gnd completions zsh > ~/.zfunc/_gnd gnd completions fish > ~/.config/fish/completions/gnd.fish This uses the clap_complete crate which generates completions directly from the clap CLI definition, ensuring they stay in sync with the actual commands and options. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 117b76c commit fd75cb6

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-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.

gnd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ graph-node = { path = "../node" }
1616
# Direct dependencies from current dev.rs
1717
anyhow = { workspace = true }
1818
clap = { workspace = true }
19+
clap_complete = "4"
1920
env_logger = "0.11.8"
2021
git-testament = "0.2"
2122
lazy_static = "1.5.0"

gnd/src/main.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use std::io;
2+
13
use anyhow::Result;
2-
use clap::{Parser, Subcommand};
4+
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
5+
use clap_complete::{generate, Shell};
36
use git_testament::{git_testament, render_testament};
47
use graph::log::logger;
58
use lazy_static::lazy_static;
@@ -79,6 +82,27 @@ enum Commands {
7982

8083
/// Remove build artifacts and generated files
8184
Clean(CleanOpt),
85+
86+
/// Generate shell completions
87+
Completions(CompletionsOpt),
88+
}
89+
90+
/// Options for shell completion generation.
91+
#[derive(Parser, Debug)]
92+
pub struct CompletionsOpt {
93+
/// Shell to generate completions for
94+
#[clap(value_enum)]
95+
pub shell: CompletionShell,
96+
}
97+
98+
/// Supported shells for completion generation.
99+
#[derive(Copy, Clone, Debug, ValueEnum)]
100+
pub enum CompletionShell {
101+
Bash,
102+
Elvish,
103+
Fish,
104+
PowerShell,
105+
Zsh,
82106
}
83107

84108
fn shutdown_token() -> CancellationToken {
@@ -197,6 +221,16 @@ async fn main() -> Result<()> {
197221
std::process::exit(1);
198222
}
199223
}
224+
Commands::Completions(completions_opt) => {
225+
let shell = match completions_opt.shell {
226+
CompletionShell::Bash => Shell::Bash,
227+
CompletionShell::Elvish => Shell::Elvish,
228+
CompletionShell::Fish => Shell::Fish,
229+
CompletionShell::PowerShell => Shell::PowerShell,
230+
CompletionShell::Zsh => Shell::Zsh,
231+
};
232+
generate(shell, &mut Cli::command(), "gnd", &mut io::stdout());
233+
}
200234
}
201235

202236
Ok(())

0 commit comments

Comments
 (0)