From c4cf49f1d20ec5ce0c71d91eb15e74a0f99beb6b Mon Sep 17 00:00:00 2001 From: Kjell Tore Guttormsen Date: Mon, 4 May 2026 20:31:23 +0200 Subject: [PATCH] feat(tally): parse --regex/-r flag and add compileRegex helper Step 1 of plan.md (Spor B B3 pipeline run). Adds the new --regex / -r flag to parseArgs and a compileRegex(pattern) helper. The flag is parsed but main() does not yet branch on it (wired in step 2). All 10 baseline tests remain green. [skip-docs] --- plugins/ultraplan-local/examples/02-real-cli/tally.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/ultraplan-local/examples/02-real-cli/tally.mjs b/plugins/ultraplan-local/examples/02-real-cli/tally.mjs index 0f9d8da..f63719a 100755 --- a/plugins/ultraplan-local/examples/02-real-cli/tally.mjs +++ b/plugins/ultraplan-local/examples/02-real-cli/tally.mjs @@ -21,11 +21,12 @@ function fail(msg, code = 2) { function parseArgs(argv) { const positional = []; - const flags = { json: false, ignoreCase: false, lines: false }; + const flags = { json: false, ignoreCase: false, lines: false, regex: false }; for (const a of argv) { if (a === '--json') flags.json = true; else if (a === '-i' || a === '--ignore-case') flags.ignoreCase = true; else if (a === '--lines') flags.lines = true; + else if (a === '--regex' || a === '-r') flags.regex = true; else if (a === '-h' || a === '--help') { process.stdout.write(HELP); process.exit(0); } else if (a.startsWith('-')) fail(`unknown flag: ${a}`); else positional.push(a); @@ -34,6 +35,11 @@ function parseArgs(argv) { return { pattern: positional[0], file: positional[1], flags }; } +function compileRegex(pattern) { + try { return new RegExp(pattern, 'g'); } + catch (e) { fail(`invalid regex: ${e.message}`); } +} + function countOccurrences(text, pattern, ignoreCase) { if (pattern.length === 0) return 0; const haystack = ignoreCase ? text.toLowerCase() : text;