diff --git a/Cargo.lock b/Cargo.lock index bbcf3d2c51..582c39f47a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3955,6 +3955,7 @@ dependencies = [ "anyhow", "argfile", "clap", + "clap_complete_command", "colored 3.0.0", "countme", "crossbeam", diff --git a/crates/ty/Cargo.toml b/crates/ty/Cargo.toml index c185760d05..1c70dc1be0 100644 --- a/crates/ty/Cargo.toml +++ b/crates/ty/Cargo.toml @@ -22,6 +22,7 @@ ty_server = { workspace = true } anyhow = { workspace = true } argfile = { workspace = true } clap = { workspace = true, features = ["wrap_help", "string"] } +clap_complete_command = { workspace = true } colored = { workspace = true } countme = { workspace = true, features = ["enable"] } crossbeam = { workspace = true } diff --git a/crates/ty/src/args.rs b/crates/ty/src/args.rs index c9969d0f9c..8c67ca60e0 100644 --- a/crates/ty/src/args.rs +++ b/crates/ty/src/args.rs @@ -24,6 +24,10 @@ pub(crate) enum Command { /// Display ty's version Version, + + /// Generate shell completion + #[clap(hide = true)] + GenerateShellCompletion { shell: clap_complete_command::Shell }, } #[derive(Debug, Parser)] diff --git a/crates/ty/src/main.rs b/crates/ty/src/main.rs index 156ce39bd5..96ed01c97a 100644 --- a/crates/ty/src/main.rs +++ b/crates/ty/src/main.rs @@ -7,7 +7,7 @@ use std::sync::Mutex; use crate::args::{Args, CheckCommand, Command, TerminalColor}; use crate::logging::setup_tracing; use anyhow::{anyhow, Context}; -use clap::Parser; +use clap::{CommandFactory, Parser}; use colored::Colorize; use crossbeam::channel as crossbeam_channel; use rayon::ThreadPoolBuilder; @@ -68,6 +68,10 @@ fn run() -> anyhow::Result { Command::Server => run_server().map(|()| ExitStatus::Success), Command::Check(check_args) => run_check(check_args), Command::Version => version().map(|()| ExitStatus::Success), + Command::GenerateShellCompletion { shell } => { + shell.generate(&mut Args::command(), &mut stdout()); + Ok(ExitStatus::Success) + } } }