Skip to content
Merged
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
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions minijinja-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ serde_json5 = { version = "0.1.0", optional = true }
serde_qs = { version = "0.12.0", optional = true }
serde_yaml = { version = "0.9.25", optional = true }
toml = { version = "0.7.6", optional = true }

[build-dependencies]
clap = { version = "4.3.21", default-features = false, features = [
"std",
"cargo",
"help",
"usage",
"error-context",
] }
clap_complete = "4"
clap_complete_fig = "4"
clap_complete_nushell = "4"
clap_mangen = "0.2"
40 changes: 40 additions & 0 deletions minijinja-cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fs::create_dir_all;

use clap::ValueEnum;
use clap_complete::Shell;
use clap_complete_fig::Fig;
use clap_complete_nushell::Nushell;

pub mod cli {
include!("src/cli.rs");
}

fn main() -> std::io::Result<()> {
println!("cargo:rerun-if-changed=build.rs");

let out_dir = match std::env::var_os("ASSET_OUT_DIR") {
Some(dir) => std::path::PathBuf::from(dir),
None => return Ok(()),
};

let mut cli = cli::make_command();

let man = clap_mangen::Man::new(cli.clone());
let mut man_buffer: Vec<u8> = Default::default();
man.render(&mut man_buffer)?;

let man_out_dir = out_dir.as_path().join("man");
create_dir_all(&man_out_dir)?;
std::fs::write(man_out_dir.join("minijinja-cli.1"), man_buffer)?;

let completions_out_dir = out_dir.as_path().join("completions");
create_dir_all(&completions_out_dir)?;

for shell in Shell::value_variants() {
clap_complete::generate_to(*shell, &mut cli, "minijinja-cli", &completions_out_dir)?;
}
clap_complete::generate_to(Nushell, &mut cli, "minijinja-cli", &completions_out_dir)?;
clap_complete::generate_to(Fig, &mut cli, "minijinja-cli", &completions_out_dir)?;

Ok(())
}
46 changes: 46 additions & 0 deletions minijinja-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::path::PathBuf;

use clap::{arg, command, value_parser, ArgAction, Command};

pub(super) fn make_command() -> Command {
command!()
.args([
arg!(-f --format <FORMAT> "the format of the input data")
.value_parser([
"auto",
"json",
#[cfg(feature = "querystring")]
"querystring",
#[cfg(feature = "yaml")]
"yaml",
#[cfg(feature = "toml")]
"toml",
#[cfg(feature = "cbor")]
"cbor",
])
.default_value("auto"),
arg!(-a --autoescape <MODE> "reconfigures autoescape behavior")
.value_parser(["auto", "html", "json", "none"])
.default_value("auto"),
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 newline"),
arg!(--env "Pass environment variables as ENV to the template"),
arg!(-E --expr <EXPR> "Evaluates an expression instead"),
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"]),
#[cfg(feature = "repl")]
arg!(--repl "starts the repl with the given data")
.conflicts_with_all(["expr", "template"]),
arg!(template: [TEMPLATE] "path to the input template").default_value("-"),
arg!(data: [DATA] "path to the data file").value_parser(value_parser!(PathBuf)),
])
.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")
}
49 changes: 4 additions & 45 deletions minijinja-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Mutex;
use std::{fs, io};

use anyhow::{bail, Context, Error};
use clap::{arg, command, value_parser, ArgAction, ArgMatches, Command};
use clap::ArgMatches;
use minijinja::machinery::{get_compiled_template, parse, tokenize, Instructions};
use minijinja::{
context, AutoEscape, Environment, Error as MError, ErrorKind, UndefinedBehavior, Value,
Expand All @@ -14,6 +14,8 @@ use minijinja::{
#[cfg(feature = "repl")]
mod repl;

mod cli;

const STDIN: &str = "-";

#[cfg(not(feature = "json5"))]
Expand Down Expand Up @@ -179,51 +181,8 @@ fn create_env(
env
}

fn make_command() -> Command {
command!()
.args([
arg!(-f --format <FORMAT> "the format of the input data")
.value_parser([
"auto",
"json",
#[cfg(feature = "querystring")]
"querystring",
#[cfg(feature = "yaml")]
"yaml",
#[cfg(feature = "toml")]
"toml",
#[cfg(feature = "cbor")]
"cbor",
])
.default_value("auto"),
arg!(-a --autoescape <MODE> "reconfigures autoescape behavior")
.value_parser(["auto", "html", "json", "none"])
.default_value("auto"),
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 newline"),
arg!(--env "Pass environment variables as ENV to the template"),
arg!(-E --expr <EXPR> "Evaluates an expression instead"),
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"]),
#[cfg(feature = "repl")]
arg!(--repl "starts the repl with the given data")
.conflicts_with_all(["expr", "template"]),
arg!(template: [TEMPLATE] "path to the input template").default_value("-"),
arg!(data: [DATA] "path to the data file").value_parser(value_parser!(PathBuf)),
])
.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")
}

fn execute() -> Result<i32, Error> {
let matches = make_command().get_matches();
let matches = cli::make_command().get_matches();

let format = matches.get_one::<String>("format").unwrap();
let (base, stdin_used) = if let Some(data) = matches.get_one::<PathBuf>("data") {
Expand Down