Skip to content

Commit

Permalink
add shell completion generation (#100)
Browse files Browse the repository at this point in the history
* add shell completion generation

* remove extra printline

* use long form only for completion arg

Co-authored-by: Trent <[email protected]>

---------

Co-authored-by: Trent <[email protected]>
  • Loading branch information
NickyMeuleman and tbillington committed Oct 17, 2023
1 parent d3fcf9b commit 69c153b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kondo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ edition = "2021"

[dependencies]
clap = { version = "4", features = ["derive"] }
clap_complete = "4"

[dependencies.kondo-lib]
path = "../kondo-lib"
Expand Down
18 changes: 17 additions & 1 deletion kondo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::{
sync::mpsc::{Receiver, Sender, SyncSender},
};

use clap::Parser;
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, Generator, Shell};

use kondo_lib::{
dir_size, path_canonicalise, pretty_size, print_elapsed, scan, Project, ScanOptions,
Expand Down Expand Up @@ -49,6 +50,10 @@ struct Opt {
#[arg(short, long, value_parser = parse_age_filter, default_value = "0d")]
older: u64,

/// Generates completions for the specified shell
#[arg(long = "completions", value_enum)]
generator: Option<Shell>,

/// If there is no input, defaults to yes
#[arg(short, long)]
default: bool,
Expand Down Expand Up @@ -285,9 +290,20 @@ fn interactive_prompt(
}
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut stdout());
}

fn main() -> Result<(), Box<dyn Error>> {
let mut opt = Opt::parse();

if let Some(generator) = opt.generator {
let mut cmd = Opt::command();
eprintln!("Generating completion file for {generator:?}...");
print_completions(generator, &mut cmd);
return Ok(());
}

if opt.quiet > 0 && !opt.all {
eprintln!("Quiet mode can only be used with --all.");
std::process::exit(1);
Expand Down

0 comments on commit 69c153b

Please sign in to comment.