mirror of
https://github.com/typst/typst
synced 2025-07-11 22:52: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 }
|
typst-timing = { workspace = true }
|
||||||
chrono = { workspace = true }
|
chrono = { workspace = true }
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
|
clap_complete = { workspace = true }
|
||||||
codespan-reporting = { workspace = true }
|
codespan-reporting = { workspace = true }
|
||||||
color-print = { workspace = true }
|
color-print = { workspace = true }
|
||||||
comemo = { workspace = true }
|
comemo = { workspace = true }
|
||||||
|
@ -7,6 +7,7 @@ use std::str::FromStr;
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use clap::builder::{TypedValueParser, ValueParser};
|
use clap::builder::{TypedValueParser, ValueParser};
|
||||||
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum, ValueHint};
|
use clap::{ArgAction, Args, ColorChoice, Parser, Subcommand, ValueEnum, ValueHint};
|
||||||
|
use clap_complete::Shell;
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
|
|
||||||
/// The character typically used to separate path components
|
/// The character typically used to separate path components
|
||||||
@ -81,6 +82,9 @@ pub enum Command {
|
|||||||
/// Self update the Typst CLI.
|
/// Self update the Typst CLI.
|
||||||
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
|
#[cfg_attr(not(feature = "self-update"), clap(hide = true))]
|
||||||
Update(UpdateCommand),
|
Update(UpdateCommand),
|
||||||
|
|
||||||
|
/// Generates shell completion scripts.
|
||||||
|
Completions(CompletionsCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compiles an input file into a supported output format.
|
/// Compiles an input file into a supported output format.
|
||||||
@ -198,6 +202,14 @@ pub struct UpdateCommand {
|
|||||||
pub backup_path: Option<PathBuf>,
|
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.
|
/// Arguments for compilation and watching.
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct CompileArgs {
|
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 args;
|
||||||
mod compile;
|
mod compile;
|
||||||
|
mod completions;
|
||||||
mod download;
|
mod download;
|
||||||
mod fonts;
|
mod fonts;
|
||||||
mod greet;
|
mod greet;
|
||||||
@ -71,6 +72,7 @@ fn dispatch() -> HintedStrResult<()> {
|
|||||||
Command::Query(command) => crate::query::query(command)?,
|
Command::Query(command) => crate::query::query(command)?,
|
||||||
Command::Fonts(command) => crate::fonts::fonts(command),
|
Command::Fonts(command) => crate::fonts::fonts(command),
|
||||||
Command::Update(command) => crate::update::update(command)?,
|
Command::Update(command) => crate::update::update(command)?,
|
||||||
|
Command::Completions(command) => crate::completions::completions(command),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user