Add completions subcommand (#6568)

This commit is contained in:
Jassiel Ovando 2025-07-09 08:41:40 -04:00 committed by GitHub
parent 1dc4c248d1
commit 1bbb58c43f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 0 deletions

View File

@ -29,6 +29,7 @@ typst-svg = { workspace = true }
typst-timing = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
clap_complete = { workspace = true }
codespan-reporting = { workspace = true }
color-print = { workspace = true }
comemo = { workspace = true }

View File

@ -7,6 +7,7 @@ use std::str::FromStr;
use chrono::{DateTime, Utc};
use clap::builder::{TypedValueParser, ValueParser};
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::Shell;
use semver::Version;
/// The character typically used to separate path components
@ -81,6 +82,9 @@ pub enum Command {
/// Self update the Typst CLI.
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
Update(UpdateCommand),
/// Generates shell completion scripts.
Completions(CompletionsCommand),
}
/// Compiles an input file into a supported output format.
@ -198,6 +202,14 @@ pub struct UpdateCommand {
pub backup_path: Option<PathBuf>,
}
/// Generates shell completion scripts.
#[derive(Debug, Clone, Parser)]
pub struct CompletionsCommand {
/// The shell to generate completions for.
#[arg(value_enum)]
pub shell: Shell,
}
/// Arguments for compilation and watching.
#[derive(Debug, Clone, Args)]
pub struct CompileArgs {

View File

@ -0,0 +1,13 @@
use std::io::stdout;
use clap::CommandFactory;
use clap_complete::generate;
use crate::args::{CliArguments, CompletionsCommand};
/// Execute the completions command.
pub fn completions(command: &CompletionsCommand) {
let mut cmd = CliArguments::command();
let bin_name = cmd.get_name().to_string();
generate(command.shell, &mut cmd, bin_name, &mut stdout());
}

View File

@ -1,5 +1,6 @@
mod args;
mod compile;
mod completions;
mod download;
mod fonts;
mod greet;
@ -71,6 +72,7 @@ fn dispatch() -> HintedStrResult<()> {
Command::Query(command) => crate::query::query(command)?,
Command::Fonts(command) => crate::fonts::fonts(command),
Command::Update(command) => crate::update::update(command)?,
Command::Completions(command) => crate::completions::completions(command),
}
Ok(())