Skip to content

Commit 906651b

Browse files
committed
fix(sim-cli): an unknown option lists the command's own flags
Commander suggests only a near spelling, so a guessed flag such as `tables rows batch-delete --row-ids` (the flag is --row) got a bare "unknown option" and the caller had to look the command up before retrying. The parse-error formatter now appends the command's flags; an id that opens with a dash keeps its `--` example instead.
1 parent 7abe86b commit 906651b

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎packages/sim-cli/src/embed-isolation.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,24 @@ describe('embedded CLI output ownership', () => {
8787
expect(invalid.stdout).toBe('')
8888
expect(invalid.stderr).toContain('missing required argument')
8989
})
90+
91+
it('answers a guessed flag with the command flags and an id that opens with a dash with the escape', async () => {
92+
const transport = async () => {
93+
throw new Error('a parse error must not reach the API')
94+
}
95+
const [guessed, dashedId] = await Promise.all([
96+
runEmbeddedCli(['tables', 'rows', 'batch-delete', 'tbl_1', '--row-ids', 'row_1', '--yes'], {
97+
...identity,
98+
transport,
99+
}),
100+
runEmbeddedCli(['audit-logs', 'get', '-X9abc'], { ...identity, transport }),
101+
])
102+
expect(guessed.exitCode).toBe(1)
103+
expect(guessed.stderr).toContain("unknown option '--row-ids'")
104+
expect(guessed.stderr).toContain('Options for sim tables rows batch-delete:')
105+
expect(guessed.stderr).toContain('--row <value...>')
106+
expect(dashedId.exitCode).toBe(1)
107+
expect(dashedId.stderr).toContain('Example: sim audit-logs get -- -X9abc')
108+
expect(dashedId.stderr).not.toContain('Options for')
109+
})
90110
})

‎packages/sim-cli/src/runtime/build.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ function looksLikeAnId(token: string): boolean {
9292
}
9393

9494
/**
95-
* Appends a worked example to the parse errors a positional argument causes.
95+
* Appends a worked example to the parse errors a positional argument causes,
96+
* and the command's own flags to an unknown option.
9697
*
9798
* Covers the argument being absent and the argument being swallowed as an
9899
* option because its id opens with a dash; the second needs the `--` escape,
99-
* which commander never mentions.
100+
* which commander never mentions. A guessed flag gets the real list: commander
101+
* suggests only a near spelling, so `--row-ids` for `--row` got a bare error
102+
* and a caller had to look the command up before retrying.
100103
*/
101-
function addArgumentExamples(command: Command): Command {
104+
function addParseErrorGuidance(command: Command): Command {
102105
const outputError = command.configureOutput().outputError
103106
if (!outputError) throw new Error('Commander output formatter is not configured')
104107

@@ -113,10 +116,15 @@ function addArgumentExamples(command: Command): Command {
113116
return
114117
}
115118

116-
if (command.registeredArguments.length === 0) return
117119
const token = UNKNOWN_OPTION_TOKEN.exec(message)?.[1]
118-
if (!token || !looksLikeAnId(token)) return
119-
write(`Example: ${commandPath(command)} -- ${token}\n`)
120+
if (!token) return
121+
if (looksLikeAnId(token)) {
122+
if (command.registeredArguments.length > 0)
123+
write(`Example: ${commandPath(command)} -- ${token}\n`)
124+
return
125+
}
126+
const flags = command.options.filter((option) => !option.hidden).map((option) => option.flags)
127+
write(`Options for ${commandPath(command)}: ${flags.join(', ')}\n`)
120128
},
121129
})
122130
return command
@@ -339,7 +347,7 @@ function configureOperation(
339347
}
340348

341349
function buildLeaf(operation: V2OperationName, spec: CommandSpec, leafName: string): Command {
342-
return addArgumentExamples(configureOperation(new Command(leafName), operation, spec))
350+
return addParseErrorGuidance(configureOperation(new Command(leafName), operation, spec))
343351
}
344352

345353
/**

0 commit comments

Comments
 (0)