Skip to content

Commit f2c97a3

Browse files
committed
feat: support positional args as template variables (_1, _2, etc.)
Positional CLI arguments can now be referenced in the body using {{ _1 }}, {{ _2 }}, etc. template syntax. Example: md translate.claude.md "bread" "taco" With body: Translate to french: {{ _1 }} Translate to spanish: {{ _2 }}
1 parent d75404b commit f2c97a3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/cli-runner.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ export class CliRunner {
377377
}
378378
}
379379

380+
// Inject positional CLI args as template variables (_1, _2, etc.)
381+
// First, separate flags from positional args in remaining
382+
const positionalCliArgs: string[] = [];
383+
const flagArgs: string[] = [];
384+
for (let i = 0; i < remaining.length; i++) {
385+
const arg = remaining[i];
386+
if (arg.startsWith("-")) {
387+
// It's a flag - include it and its value if present
388+
flagArgs.push(arg);
389+
if (i + 1 < remaining.length && !remaining[i + 1].startsWith("-")) {
390+
flagArgs.push(remaining[++i]);
391+
}
392+
} else {
393+
// It's a positional arg
394+
positionalCliArgs.push(arg);
395+
}
396+
}
397+
// Inject positional args as _1, _2, etc. template variables
398+
// Uses underscore prefix to match other template var conventions
399+
for (let i = 0; i < positionalCliArgs.length; i++) {
400+
templateVars[`_${i + 1}`] = positionalCliArgs[i];
401+
}
402+
// Update remaining to only contain flag args (positionals consumed for templates)
403+
remaining = flagArgs;
404+
380405
// Expand imports
381406
let expandedBody = rawBody;
382407
const fileDir = dirname(resolve(localFilePath));

0 commit comments

Comments
 (0)