diff --git a/README.md b/README.md index d3f638c5f..16affc679 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ npm install liquidjs **CLI** ```bash -npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Liquid"}' +npx liquidjs 'Hello, {{ name }}!' --context '{"name": "Liquid"}' +# or: npx liquidjs --template 'Hello, {{ name }}!' --context '{"name": "Liquid"}' ``` See the [setup guide][setup] for partials, layouts, caching, and other options. diff --git a/bin/liquid.js b/bin/liquid.js index 703f4a99f..964f8515b 100755 --- a/bin/liquid.js +++ b/bin/liquid.js @@ -14,7 +14,8 @@ async function render () { program .name('liquidjs') .description('Render a Liquid template') - .requiredOption('-t, --template ', 'liquid template to render (inline, @path, or @- for stdin)') + .argument('[template]', 'liquid template to render (inline, @path, or @- for stdin)') + .option('-t, --template ', 'liquid template to render (inline, @path, or @- for stdin)') .option('-c, --context ', 'input context in JSON format (inline, @path, or @- for stdin)') .option('-o, --output ', 'write rendered output to file (omit to write to stdout)') .option('--cache [size]', 'cache previously parsed template structures (default cache size: 1024)') @@ -45,12 +46,23 @@ async function render () { .parse() const options = program.opts() + const positionalTemplate = program.args[0] + const optionTemplate = options.template - if (Object.values(options).filter((value) => value === '@-').length > 1) { + if (positionalTemplate && optionTemplate && positionalTemplate !== optionTemplate) { + throw new Error(`Conflicting templates: positional argument and --template differ.`) + } + + const templateOption = positionalTemplate || optionTemplate + if (!templateOption) { + throw new Error(`A template is required. Pass a positional