Skip to content

Commit 0453826

Browse files
authored
fix: report unknown command before flag-support validation (#1038)
* fix: report unknown command before flag-support validation When an unknown command is provided with flags, the parser was validating flag support before checking if the command exists, resulting in misleading "Flags are not supported for command X" errors instead of "Unknown command: X". This fix reorders the validation to check for unknown commands first, reporting more helpful error messages. Additionally, when the unknown command is 'tap', the error now includes a hint suggesting 'press or click' as alternatives. Closes #1036 * refactor: drop as-any casts and note the cli.ts sync requirement Review follow-ups: widen the command lists to readonly string[] instead of narrowing the value, and document that isCommandKnown must stay in sync with the cli.ts dispatch fall-through.
1 parent a79dad6 commit 0453826

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎src/__tests__/cli-help.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,19 @@ test('help rejects multiple positional commands and skips daemon dispatch', asyn
149149
assert.equal(result.calls.length, 0);
150150
assert.match(result.stderr, /Error \(INVALID_ARGS\): help accepts at most one command/);
151151
});
152+
153+
test('unknown command with flags reports unknown command before flag validation', async () => {
154+
const result = await runCliCapture(['tap', 'e3', '--session', 'foo']);
155+
assert.equal(result.code, 1);
156+
assert.equal(result.calls.length, 0);
157+
assert.match(result.stderr, /Error \(INVALID_ARGS\): Unknown command: tap/);
158+
assert.match(result.stderr, /Did you mean press or click/);
159+
});
160+
161+
test('unknown command without flags reports unknown command with alias suggestion', async () => {
162+
const result = await runCliCapture(['tap', 'e3']);
163+
assert.equal(result.code, 1);
164+
assert.equal(result.calls.length, 0);
165+
assert.match(result.stderr, /Error \(INVALID_ARGS\): Unknown command: tap/);
166+
assert.match(result.stderr, /Did you mean press or click/);
167+
});

‎src/cli/parser/args.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '../../utils/command-schema.ts';
1212
import { buildCommandUsageText, buildUsageText } from './cli-help.ts';
1313
import { isFlagSupportedForCommand } from '../../utils/cli-option-schema.ts';
14+
import { listCliCommandNames, INTERNAL_COMMANDS } from '../../command-catalog.ts';
1415

1516
type ParsedArgs = {
1617
command: string | null;
@@ -148,6 +149,18 @@ export function finalizeParsedArgs(
148149
options?.defaultFlags ?? {},
149150
);
150151
mergeDefinedFlags(flags, parsed.flags);
152+
153+
// Check if the command is known before validating flags
154+
// This ensures "Unknown command" errors take precedence over flag validation errors
155+
// However, skip this check if --help is provided, since cli.ts will handle it gracefully
156+
if (parsed.command && !isCommandKnown(parsed.command) && !flags.help) {
157+
const hint = getCommandAliasSuggestion(parsed.command);
158+
const message = hint
159+
? `Unknown command: ${parsed.command}. Did you mean ${hint}?`
160+
: `Unknown command: ${parsed.command}`;
161+
throw new AppError('INVALID_ARGS', message);
162+
}
163+
151164
const disallowed = parsed.providedFlags.filter(
152165
(entry) => !isFlagSupportedForCommand(entry.key, parsed.command),
153166
);
@@ -333,6 +346,25 @@ function normalizeParsedCommandAliases(parsed: ParsedArgs): ParsedArgs {
333346
return parsed;
334347
}
335348

349+
function isCommandKnown(command: string): boolean {
350+
// Must stay in sync with the authoritative dispatch fall-through in
351+
// cli.ts ("Unknown command"): any command runnable there must be listed in
352+
// the catalog (or below), or this early check rejects it at parse time.
353+
// 'help' is handled specially in cli.ts and is not in the command catalog
354+
if (command === 'help') return true;
355+
// Internal commands are handled specially in cli.ts
356+
if ((Object.values(INTERNAL_COMMANDS) as readonly string[]).includes(command)) return true;
357+
return (listCliCommandNames() as readonly string[]).includes(command);
358+
}
359+
360+
const COMMAND_ALIAS_SUGGESTIONS: Record<string, string> = {
361+
tap: 'press or click',
362+
};
363+
364+
function getCommandAliasSuggestion(command: string): string | undefined {
365+
return COMMAND_ALIAS_SUGGESTIONS[command];
366+
}
367+
336368
function formatUnsupportedFlagMessage(command: string | null, unsupported: string[]): string {
337369
if (!command) {
338370
return unsupported.length === 1

0 commit comments

Comments
 (0)