Skip to content

Commit

Permalink
feat(minijnija-cli): add support for defining args as env
Browse files Browse the repository at this point in the history
Signed-off-by: Devin Buhl <devin@buhl.casa>

fix: keep super on func

Signed-off-by: Devin Buhl <devin@buhl.casa>

fix: forgot to update after testing

Signed-off-by: Devin Buhl <devin@buhl.casa>
  • Loading branch information
onedr0p committed Oct 14, 2024
1 parent 67035cb commit c95723c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
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
64 changes: 42 additions & 22 deletions minijinja-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,49 @@ 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."),
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]"),
arg!(-s --syntax <PAIR>... "Changes a syntax feature (feature=value)")
.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"]),
Expand All @@ -63,10 +77,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

0 comments on commit c95723c

Please sign in to comment.