summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src/args.rs
diff options
context:
space:
mode:
authorLaurenz <ritters_werth@outlook.com>2023-12-18 12:18:41 +0100
committerGitHub <noreply@github.com>2023-12-18 12:18:41 +0100
commit22ba6825db3b82e0b0f83ef6052f17289893e385 (patch)
treecf69e7ac0eeb49f13ffb0b81e36461e07cb766ba /crates/typst-cli/src/args.rs
parent356bdeba18153efd4209657182971e22bdaf4db2 (diff)
Key/Value data from CLI (#2894)
Diffstat (limited to 'crates/typst-cli/src/args.rs')
-rw-r--r--crates/typst-cli/src/args.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crates/typst-cli/src/args.rs b/crates/typst-cli/src/args.rs
index 075412cd..8f0261f1 100644
--- a/crates/typst-cli/src/args.rs
+++ b/crates/typst-cli/src/args.rs
@@ -1,6 +1,7 @@
use std::fmt::{self, Display, Formatter};
use std::path::PathBuf;
+use clap::builder::ValueParser;
use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use semver::Version;
@@ -116,6 +117,15 @@ pub struct SharedArgs {
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
pub root: Option<PathBuf>,
+ /// Add a string key-value pair visible through `sys.inputs`
+ #[clap(
+ long = "input",
+ value_name = "key=value",
+ action = ArgAction::Append,
+ value_parser = ValueParser::new(parse_input_pair),
+ )]
+ pub inputs: Vec<(String, String)>,
+
/// Adds additional directories to search for fonts
#[clap(
long = "font-path",
@@ -134,6 +144,22 @@ pub struct SharedArgs {
pub diagnostic_format: DiagnosticFormat,
}
+/// Parses key/value pairs split by the first equal sign.
+///
+/// This function will return an error if the argument contains no equals sign
+/// or contains the key (before the equals sign) is empty.
+fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
+ let (key, val) = raw
+ .split_once('=')
+ .ok_or("input must be a key and a value separated by an equal sign")?;
+ let key = key.trim().to_owned();
+ if key.is_empty() {
+ return Err("the key was missing or empty".to_owned());
+ }
+ let val = val.trim().to_owned();
+ Ok((key, val))
+}
+
/// Lists all discovered fonts in system and custom font paths
#[derive(Debug, Clone, Parser)]
pub struct FontsCommand {