Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(minijnija-cli): add support for using env for options #598

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions minijinja-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ contrib = ["minijinja-contrib"]
anyhow = "1.0.74"
ciborium = { version = "0.2.1", optional = true }
clap = { version = "4.3.21", default-features = false, features = [
"env",
"std",
"cargo",
"help",
Expand Down
5 changes: 4 additions & 1 deletion minijinja-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Crates.io](https://img.shields.io/crates/d/minijinja-cli.svg)](https://crates.io/crates/minijinja-cli)
[![rustc 1.61.0](https://img.shields.io/badge/rust-1.61%2B-orange.svg)](https://img.shields.io/badge/rust-1.61%2B-orange.svg)

`minijinja-cli` is a command line executable that uses
`minijinja-cli` is a command line executable that uses
[MiniJinja](https://github.com/mitsuhiko/minijinja) to render Jinja2 templates
directly from the command line to stdout.

Expand Down Expand Up @@ -52,6 +52,9 @@ can be set to stdin at once.

## Options

Note: The below options can also be set via environment varibles using the
`MINIJINJA_<OPTION>` syntax, for example `MINIJINJA_FORMAT=auto`.

- `-f`, `--format` `<FORMAT>`:
this defines the input format of the data file. The default is `auto` which
turns on auto detection based on the file extension. For the supported formats
Expand Down
67 changes: 46 additions & 21 deletions minijinja-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,57 @@ pub(super) fn make_command() -> Command {
#[cfg(feature = "cbor")]
"cbor",
])
.default_value("auto"),
.default_value("auto")
.env("MINIJINJA_FORMAT"),
arg!(-a --autoescape <MODE> "reconfigures autoescape behavior")
.value_parser(["auto", "html", "json", "none"])
.default_value("auto"),
.default_value("auto")
.env("MINIJINJA_AUTOESCAPE"),
arg!(-D --define <EXPR> "defines an input variable (key=value)")
.action(ArgAction::Append),
arg!(--strict "disallow undefined variables in templates"),
arg!(--"no-include" "Disallow includes and extending"),
arg!(--"no-newline" "Do not output a trailing newline"),
arg!(--"trim-blocks" "Enable the trim_blocks flag"),
arg!(--"lstrip-blocks" "Enable the lstrip_blocks flag"),
.action(ArgAction::Append)
.env("MINIJINJA_DEFINE"),
arg!(--strict "disallow undefined variables in templates")
.env("MINIJINJA_STRICT"),
arg!(--"no-include" "Disallow includes and extending")
.env("MINIJINJA_NO_INCLUDE"),
arg!(--"no-newline" "Do not output a trailing newline")
.env("MINIJINJA_NO_NEWLINE"),
arg!(--"trim-blocks" "Enable the trim_blocks flag")
.env("MINIJINJA_TRIM_BLOCKS"),
arg!(--"lstrip-blocks" "Enable the lstrip_blocks flag")
.env("MINIJINJA_LSTRIP_BLOCKS"),
#[cfg(feature = "contrib")]
arg!(--"py-compat" "Enables improved Python compatibility. Enabling \
this adds methods such as dict.keys and some others."),
this adds methods such as dict.keys and some others.")
.env("MINIJINJA_PY_COMPAT"),
arg!(-s --syntax <PAIR>... "Changes a syntax feature (feature=value) \
[possible features: block-start, block-end, variable-start, variable-end, \
comment-start, comment-end, line-statement-prefix, \
line-statement-comment]"),
line-statement-comment]")
.env("MINIJINJA_SYNTAX"),
arg!(--"safe-path" <PATH>... "Only allow includes from this path. Can be used multiple times.")
.conflicts_with("no-include")
.value_parser(value_parser!(PathBuf)),
arg!(--env "Pass environment variables as ENV to the template"),
arg!(-E --expr <EXPR> "Evaluates an expression instead"),
.value_parser(value_parser!(PathBuf))
.env("MINIJINJA_SAFE_PATH"),
arg!(--env "Pass environment variables as ENV to the template")
.env("MINIJINJA_ENV"),
arg!(-E --expr <EXPR> "Evaluates an expression instead")
.env("MINIJINJA_EXPR"),
arg!(--"expr-out" <MODE> "Sets the expression output mode")
.value_parser(["print", "json", "json-pretty", "status"])
.default_value("print")
.requires("expr"),
arg!(--fuel <AMOUNT> "configures the maximum fuel").value_parser(value_parser!(u64)),
arg!(--dump <KIND> "dump internals of a template").value_parser(["instructions", "ast", "tokens"]),
.requires("expr")
.env("MINIJINJA_EXPR_OUT"),
arg!(--fuel <AMOUNT> "configures the maximum fuel")
.value_parser(value_parser!(u64))
.env("MINIJINJA_FUEL"),
arg!(--dump <KIND> "dump internals of a template")
.value_parser(["instructions", "ast", "tokens"])
.env("MINIJINJA_DUMP"),
#[cfg(feature = "repl")]
arg!(--repl "starts the repl with the given data")
.conflicts_with_all(["expr", "template"]),
.conflicts_with_all(["expr", "template"])
.env("MINIJINJA_REPL"),
#[cfg(feature = "completions")]
arg!(--"generate-completion" <SHELL> "generate a completion script for the given shell")
.value_parser([
Expand All @@ -63,10 +82,16 @@ pub(super) fn make_command() -> Command {
]),
arg!(-o --output <FILENAME> "path to the output file")
.default_value("-")
.value_parser(value_parser!(PathBuf)),
arg!(--select <SELECTOR> "select a path of the input data"),
arg!(template: [TEMPLATE] "path to the input template").default_value("-"),
arg!(data: [DATA] "path to the data file").value_parser(value_parser!(PathBuf)),
.value_parser(value_parser!(PathBuf))
.env("MINIJINJA_OUTPUT"),
arg!(--select <SELECTOR> "select a path of the input data")
.env("MINIJINJA_SELECT"),
arg!(template: [TEMPLATE] "path to the input template")
.default_value("-")
.env("MINIJINJA_TEMPLATE"),
arg!(data: [DATA] "path to the data file")
.value_parser(value_parser!(PathBuf))
.env("MINIJINJA_DATA"),
])
.about("minijinja-cli is a command line tool to render or evaluate jinja2 templates.")
.after_help("For more information see https://github.com/mitsuhiko/minijinja/tree/main/minijinja-cli/README.md")
Expand Down
20 changes: 20 additions & 0 deletions minijinja-cli/tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ fn test_no_newline() {
"###);
}

#[test]
fn test_no_newline_env() {
let input = file_with_contents(r#"{"foo": "bar"}"#);
let tmpl = file_with_contents(r#"Hello {{ foo }}!"#);

assert_cmd_snapshot!(
cli()
.env("MINIJINJA_FORMAT", "json")
.env("MINIJINJA_NO_NEWLINE", "true")
.arg(tmpl.path())
.arg(input.path()),
@r###"
success: true
exit_code: 0
----- stdout -----
Hello bar!
----- stderr -----
"###);
}

#[test]
fn test_json() {
let input = file_with_contents_and_ext(r#"{"foo": "bar"}"#, ".json");
Expand Down