templx
v0.0.3
Published
Lightweight CLI to render Handlebars templates with options derived from CLI flags.
Readme
templx
Lightweight CLI to render Handlebars templates with options derived from CLI flags.
Usage
npx templx format <template.hbs> [flags] [-]- Command: only
formatis supported. - Template: path to a Handlebars file.
- Flags: become template options (see mapping below).
-(single hyphen) as a positional means: read JSON options from stdin.
Flags ➜ options mapping
- Flags become keys on the options object by flag name, without dashes.
- Parsing collects values as arrays, then a single value is coerced to a string.
--name Alice➜{ name: "Alice" }--name=Aliceis equivalent.
- Repeating a flag accumulates values:
--color red --color blue➜{ color: ["red", "blue"] }
- Pending option consumption: if a flag has no inline value, the next positional token becomes its value—even if it is
-. - Option terminator
--stops parsing further tokens.
Notes:
- Single-occurrence flags become strings; repeated flags remain arrays. Write templates normally, e.g.
{{name}}. - Stdin JSON values are left as-is; only CLI flags are coerced.
- Handlebars runs in strict mode: missing variables cause an error.
Stdin JSON (-)
Provide a single - positional to read a JSON object from stdin. The JSON object is intelligently merged with CLI flags. Stdin values are not coerced; only flags are.
# stdin only
echo '{"name":"Bob","colors":["green"]}' \
| npx templx format template.hbs -
# stdin + flags merge
echo '{"name":"Bob","colors":["green"],"meta":{"a":1}}' \
| npx templx format template.hbs - --name Carol --colors yellow --meta.b 2Merge rules:
- At most one
-is accepted as a free positional. - If a flag is waiting for a value,
-is consumed as that value (no stdin mode in that case). - Stdin must be a top-level JSON object.
- Intelligent merge between stdin (left) and CLI (right):
- Objects: deep-merged
- Arrays: concatenated (stdin first, then CLI)
- Mixed array/scalar: scalar promoted to array and concatenated
- Scalars: CLI overrides stdin
Examples
Template (template.hbs):
Hello {{name}}!
Colors: {{#each colors}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}.Run:
npx templx format template.hbs --name Alice --colors red --colors blue
# => Hello Alice!
# => Colors: red, blue.With stdin merge:
echo '{"name":"Bob","colors":["green"],"meta":{"a":1}}' \
| npx templx format hi.hbs - --name Carol --colors yellow --meta.b 2
# => Hello Carol!
# => Colors: green, yellow.