summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJassiel Ovando <jassielovando@protonmail.com>2025-07-09 08:41:40 -0400
committerGitHub <noreply@github.com>2025-07-09 12:41:40 +0000
commit1bbb58c43f6f5d94be4b2609728039a9922c75f8 (patch)
tree77740e36cd889b6a1dfa14ce84be2ac781b02e2f
parent1dc4c248d1022dc9f3b6e3e899857404f6c680a1 (diff)
Add completions subcommand (#6568)
-rw-r--r--crates/typst-cli/Cargo.toml1
-rw-r--r--crates/typst-cli/src/args.rs12
-rw-r--r--crates/typst-cli/src/completions.rs13
-rw-r--r--crates/typst-cli/src/main.rs2
4 files changed, 28 insertions, 0 deletions
diff --git a/crates/typst-cli/Cargo.toml b/crates/typst-cli/Cargo.toml
index 7e9b93f9..792cabae 100644
--- a/crates/typst-cli/Cargo.toml
+++ b/crates/typst-cli/Cargo.toml
@@ -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 }
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs
index c3fd541a..7459be0f 100644
--- a/crates/typst-cli/src/args.rs
+++ b/crates/typst-cli/src/args.rs
@@ -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 {
diff --git a/crates/typst-cli/src/completions.rs b/crates/typst-cli/src/completions.rs
new file mode 100644
index 00000000..51e7db10
--- /dev/null
+++ b/crates/typst-cli/src/completions.rs
@@ -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());
+}
diff --git a/crates/typst-cli/src/main.rs b/crates/typst-cli/src/main.rs
index 14f8a665..6a3b337d 100644
--- a/crates/typst-cli/src/main.rs
+++ b/crates/typst-cli/src/main.rs
@@ -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(())