fix: harden consult argument parsing

This commit is contained in:
Affaan Mustafa
2026-04-30 07:09:47 -04:00
committed by Affaan Mustafa
parent 9a3f72712b
commit 08d6c82989
2 changed files with 21 additions and 4 deletions

View File

@@ -158,15 +158,18 @@ function parseArgs(argv) {
help: false, help: false,
}; };
if (args.includes('--help') || args.includes('-h')) {
parsed.help = true;
return parsed;
}
for (let index = 0; index < args.length; index += 1) { for (let index = 0; index < args.length; index += 1) {
const arg = args[index]; const arg = args[index];
if (arg === '--help' || arg === '-h') { if (arg === '--json') {
parsed.help = true;
} else if (arg === '--json') {
parsed.json = true; parsed.json = true;
} else if (arg === '--target') { } else if (arg === '--target') {
if (!args[index + 1]) { if (!args[index + 1] || args[index + 1].startsWith('-')) {
throw new Error('Missing value for --target'); throw new Error('Missing value for --target');
} }
parsed.target = args[index + 1]; parsed.target = args[index + 1];

View File

@@ -48,6 +48,13 @@ function runTests() {
assert.match(result.stdout, /node scripts\/consult\.js "security reviews"/); assert.match(result.stdout, /node scripts\/consult\.js "security reviews"/);
})) passed++; else failed++; })) passed++; else failed++;
if (test('shows help even when other flags would be invalid', () => {
const result = run(['--help', '--target', 'not-a-target']);
assert.strictEqual(result.status, 0, result.stderr);
assert.match(result.stdout, /Consult ECC install components/);
})) passed++; else failed++;
if (test('recommends security components and profile for a natural language query', () => { if (test('recommends security components and profile for a natural language query', () => {
const result = run(['security', 'reviews', '--json']); const result = run(['security', 'reviews', '--json']);
@@ -108,6 +115,13 @@ function runTests() {
assert.match(result.stderr, /Unknown install target/); assert.match(result.stderr, /Unknown install target/);
})) passed++; else failed++; })) passed++; else failed++;
if (test('rejects flag-like target values as missing target names', () => {
const result = run(['security', '--target', '--json']);
assert.strictEqual(result.status, 1);
assert.match(result.stderr, /Missing value for --target/);
})) passed++; else failed++;
console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`); console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0); process.exit(failed > 0 ? 1 : 0);
} }