diff options
| author | spore <spore2050@gmail.com> | 2024-02-05 21:05:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-05 13:05:26 +0000 |
| commit | 70b354e887cf735db070f766e4812357dddb8a40 (patch) | |
| tree | c7a62d63db4518a6e7bd51ddc58df0a24c3c589f /crates/typst-cli/src/args.rs | |
| parent | 302b87032121125bcfdb0490ad865a585ee0e3aa (diff) | |
Support reading input from stdin (#3339)
Diffstat (limited to 'crates/typst-cli/src/args.rs')
| -rw-r--r-- | crates/typst-cli/src/args.rs | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs index c14f0277..973eea8b 100644 --- a/crates/typst-cli/src/args.rs +++ b/crates/typst-cli/src/args.rs @@ -65,6 +65,7 @@ pub struct CompileCommand { pub common: SharedArgs, /// Path to output file (PDF, PNG, or SVG) + #[clap(required_if_eq("input", "-"))] pub output: Option<PathBuf>, /// The format of the output file, inferred from the extension by default @@ -121,8 +122,9 @@ pub enum SerializationFormat { /// Common arguments of compile, watch, and query. #[derive(Debug, Clone, Args)] pub struct SharedArgs { - /// Path to input Typst file - pub input: PathBuf, + /// Path to input Typst file, use `-` to read input from stdin + #[clap(value_parser = input_value_parser)] + pub input: Input, /// Configures the project root (for absolute paths) #[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")] @@ -155,6 +157,26 @@ pub struct SharedArgs { pub diagnostic_format: DiagnosticFormat, } +/// An input that is either stdin or a real path. +#[derive(Debug, Clone)] +pub enum Input { + /// Stdin, represented by `-`. + Stdin, + /// A non-empty path. + Path(PathBuf), +} + +/// The clap value parser used by `SharedArgs.input` +fn input_value_parser(value: &str) -> Result<Input, clap::error::Error> { + if value.is_empty() { + Err(clap::Error::new(clap::error::ErrorKind::InvalidValue)) + } else if value == "-" { + Ok(Input::Stdin) + } else { + Ok(Input::Path(value.into())) + } +} + /// Parses key/value pairs split by the first equal sign. /// /// This function will return an error if the argument contains no equals sign |
