Skip to content

Commit 78bf3e7

Browse files
committed
Add shell completions command to CLI
Introduces a new 'completions' subcommand to generate shell completion scripts for bash, zsh, fish, powershell, and elvish. Updates dependencies to include 'clap_complete' and integrates completion generation into the CLI.
1 parent 7ef2b72 commit 78bf3e7

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

langcodec-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ documentation = "https://docs.rs/langcodec-cli"
1414
[dependencies]
1515
langcodec = "0.3.4"
1616
clap = { version = "4", features = ["derive"] }
17+
clap_complete = "4"
1718
unicode-width = "0.2.1"
1819
crossterm = "0.29.0"
1920
atty = "0.2.14"

langcodec-cli/src/main.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use crate::merge::{ConflictStrategy, run_merge_command};
1111
use crate::transformers::custom_format_to_resource;
1212
use crate::validation::{ValidationContext, validate_context, validate_custom_format_file};
1313
use crate::view::print_view;
14-
use clap::{Parser, Subcommand};
14+
use clap::{CommandFactory, Parser, Subcommand};
15+
use clap_complete::{Shell, generate};
1516

1617
use langcodec::{Codec, convert_auto, formats::FormatType};
1718
use std::fs::File;
@@ -108,6 +109,19 @@ enum Commands {
108109
#[arg(short, long)]
109110
output: Option<String>,
110111
},
112+
113+
/// Generate shell completion script and print to stdout.
114+
///
115+
/// Examples:
116+
/// - langcodec completions bash > /etc/bash_completion.d/langcodec
117+
/// - langcodec completions zsh > "${fpath[1]}/_langcodec"
118+
/// - langcodec completions fish > ~/.config/fish/completions/langcodec.fish
119+
/// - langcodec completions powershell > langcodec.ps1
120+
Completions {
121+
/// Shell to generate completions for (bash, zsh, fish, powershell, elvish)
122+
#[arg(value_enum)]
123+
shell: Shell,
124+
},
111125
}
112126

113127
fn main() {
@@ -234,6 +248,11 @@ fn main() {
234248

235249
run_debug_command(input, lang, output);
236250
}
251+
Commands::Completions { shell } => {
252+
let mut cmd = Args::command();
253+
cmd = cmd.bin_name("langcodec");
254+
generate(shell, &mut cmd, "langcodec", &mut std::io::stdout());
255+
}
237256
}
238257
}
239258

0 commit comments

Comments
 (0)