mirror of
https://github.com/typst/typst
synced 2025-07-11 14:42:53 +08:00
Add completions subcommand (#6568)
This commit is contained in:
parent
1dc4c248d1
commit
1bbb58c43f
@ -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 }
|
||||
|
@ -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 {
|
||||
|
13
crates/typst-cli/src/completions.rs
Normal file
13
crates/typst-cli/src/completions.rs
Normal 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());
|
||||
}
|
@ -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(())
|
||||
|
Loading…
x
Reference in New Issue
Block a user