Add shell completion (#251)

* add commandline argument to generate shell completion

* update changelog

* fixup! add commandline argument to generate shell completion

* Update option name and description

---------

Co-authored-by: David Peter <mail@david-peter.de>
This commit is contained in:
Friedrich 'friedɀ' SchwedlerandDavid Peter authored and GitHub committed 2025-08-31 21:39:17 +02:00
1 parent 909dd7d947
commit 585d06293a
4 files changed
+25 -1

No files matched your search

+1
View File
@@ -1,6 +1,7 @@
# unreleased
- Fix memory allocation bug when terminal width is less than 10, see #244 (@selfup)
- Add command line argument to generate shell completion, see #155 (@friedz)
## Features
Generated
+10
View File
@@ -128,6 +128,15 @@ dependencies = [
"terminal_size",
]
[[package]]
name = "clap_complete"
version = "4.5.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5abde44486daf70c5be8b8f8f1b66c49f86236edf6fa2abadb4d961c4c6229a"
dependencies = [
"clap",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
@@ -222,6 +231,7 @@ dependencies = [
"anyhow",
"assert_cmd",
"clap",
"clap_complete",
"const_format",
"libc",
"owo-colors",
+1
View File
@@ -19,6 +19,7 @@ owo-colors = "4"
supports-color = "3"
thiserror = "1.0"
terminal_size = "0.4"
clap_complete = "4"
[dependencies.clap]
version = "4"
+13 -1
View File
@@ -4,7 +4,8 @@ use std::num::{NonZeroI64, NonZeroU64};
use std::path::PathBuf;
use clap::builder::ArgPredicate;
use clap::{ArgAction, Parser, ValueEnum};
use clap::{ArgAction, CommandFactory, Parser, ValueEnum};
use clap_complete::aot::{generate, Shell};
use anyhow::{anyhow, bail, Context, Result};
@@ -192,6 +193,10 @@ struct Opt {
/// Print a table showing how different types of bytes are colored.
#[arg(long)]
print_color_table: bool,
/// Show shell completion for a certain shell
#[arg(long, value_name("SHELL"))]
completion: Option<Shell>,
}
#[derive(Clone, Debug, Default, ValueEnum)]
@@ -248,6 +253,13 @@ fn run() -> Result<()> {
return print_color_table().map_err(|e| anyhow!(e));
}
if let Some(sh) = opt.completion {
let mut cmd = Opt::command();
let name = cmd.get_name().to_string();
generate(sh, &mut cmd, name, &mut io::stdout());
return Ok(());
}
let stdin = io::stdin();
let mut reader = match opt.file {