Skip to content

Commit 292d2f4

Browse files
authored
feat: add --input to compile (#2328)
## What - add `--input key=value` to `tinymist compile` and pass values into `sys.inputs` - reuse the existing `parse_input_pair` helper ## Why Align tinymist CLI with Typst's `--input` behavior so `sys.inputs` works in compile tasks. ## How - extend `DocNewArgs` to accept inputs and store them in `ProjectInput.inputs` - export `parse_input_pair` from `tinymist_world::args` and reuse it ## Testing - `cargo check -p tinymist-cli`
1 parent 782272f commit 292d2f4

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

crates/tinymist-project/src/args.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::{path::Path, sync::OnceLock};
22

3-
use clap::ValueHint;
3+
use clap::{ArgAction, ValueHint, builder::ValueParser};
44
use tinymist_std::{bail, error::prelude::Result};
55

6-
use tinymist_world::args::PdfExportArgs;
7-
use tinymist_world::args::PngExportArgs;
86
pub use tinymist_world::args::{CompileFontArgs, CompilePackageArgs};
7+
use tinymist_world::args::{PdfExportArgs, PngExportArgs, parse_input_pair};
98

109
use crate::PROJECT_ROUTE_USER_ACTION_PRIORITY;
1110
use crate::model::*;
@@ -31,6 +30,14 @@ pub struct DocNewArgs {
3130
/// (PWD).
3231
#[clap(long = "root", env = "TYPST_ROOT", value_name = "DIR")]
3332
pub root: Option<String>,
33+
/// Add a string key-value pair visible through `sys.inputs`.
34+
#[clap(
35+
long = "input",
36+
value_name = "key=value",
37+
action = ArgAction::Append,
38+
value_parser = ValueParser::new(parse_input_pair),
39+
)]
40+
pub inputs: Vec<(String, String)>,
3441
/// Specify the font related arguments.
3542
#[clap(flatten)]
3643
pub font: CompileFontArgs,
@@ -74,8 +81,7 @@ impl DocNewArgs {
7481
lock_dir: Some(ctx.1.to_path_buf()),
7582
root,
7683
main,
77-
// todo: inputs
78-
inputs: vec![],
84+
inputs: self.inputs.clone(),
7985
font_paths,
8086
system_fonts: !self.font.ignore_system_fonts,
8187
package_path,

crates/tinymist-world/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl CompileOnceArgs {
216216
///
217217
/// This function will return an error if the argument contains no equals sign
218218
/// or contains the key (before the equals sign) is empty.
219-
fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
219+
pub fn parse_input_pair(raw: &str) -> Result<(String, String), String> {
220220
let (key, val) = raw
221221
.split_once('=')
222222
.ok_or("input must be a key and a value separated by an equal sign")?;

tests/e2e/e2e/cli.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn test_help_lsp() {
9696
#[test]
9797
fn test_help_compile_alias() {
9898
apply_common_filters!();
99-
insta_cmd::assert_cmd_snapshot!(cli().arg("c").arg("--help"), @r"
99+
insta_cmd::assert_cmd_snapshot!(cli().arg("c").arg("--help"), @"
100100
success: true
101101
exit_code: 0
102102
----- stdout -----
@@ -127,6 +127,9 @@ fn test_help_compile_alias() {
127127
128128
[env: TYPST_ROOT=REDACTED]
129129
130+
--input <key=value>
131+
Add a string key-value pair visible through `sys.inputs`
132+
130133
--font-path <DIR>
131134
Add additional directories that are recursively searched for fonts.
132135
@@ -231,7 +234,7 @@ fn test_help_compile_alias() {
231234
#[test]
232235
fn test_help_compile() {
233236
apply_common_filters!();
234-
insta_cmd::assert_cmd_snapshot!(cli().arg("compile").arg("--help"), @r"
237+
insta_cmd::assert_cmd_snapshot!(cli().arg("compile").arg("--help"), @"
235238
success: true
236239
exit_code: 0
237240
----- stdout -----
@@ -262,6 +265,9 @@ fn test_help_compile() {
262265
263266
[env: TYPST_ROOT=REDACTED]
264267
268+
--input <key=value>
269+
Add a string key-value pair visible through `sys.inputs`
270+
265271
--font-path <DIR>
266272
Add additional directories that are recursively searched for fonts.
267273

0 commit comments

Comments
 (0)