Skip to content

Commit e73a992

Browse files
committed
fix(cli): name only options commander parsed as unknown, never operands after --
1 parent ea832bc commit e73a992

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,21 @@ function optionList(command: Command): string {
145145
: `${commandPath(command)} takes no options.`
146146
}
147147

148+
/** What each guided command's own option parse left unknown, recorded by {@link addParseErrorGuidance}. */
149+
const UNKNOWN_ARGUMENTS = new WeakMap<Command, readonly string[]>()
150+
151+
function isDashLed(arg: string): boolean {
152+
return arg.length > 1 && arg.startsWith('-')
153+
}
154+
148155
/**
149-
* The first unknown option commander saw. Commander files it, and every
150-
* operand after it, at the end of `args`; an operand never opens with a dash
151-
* unless it follows `--`, and then no option is unknown.
156+
* The first unknown option commander saw. Its parse files that option and everything after it as
157+
* unknown, while an operand escaped with `--` (`files rename f_1 -- --name`) stays an operand:
158+
* `args` holds both, so only the parse's own unknown list tells them apart.
152159
*/
153160
function unknownOptionToken(command: Command): string | undefined {
154-
return command.args.find((arg) => arg.length > 1 && arg.startsWith('-'))
161+
const first = UNKNOWN_ARGUMENTS.get(command)?.[0]
162+
return first !== undefined && isDashLed(first) ? first : undefined
155163
}
156164

157165
function missingMandatory(command: Command): Option[] {
@@ -179,12 +187,12 @@ function unknownBesideMissing(command: Command): string | undefined {
179187
* was wanted (`files rename <id> new.pdf`, `knowledge search <kbId>`), when
180188
* that reading is the only one. An unknown option puts its own value among the
181189
* operands, and with two flags missing the value could belong to either, so
182-
* both cases name nothing (as does a dash-led operand escaped with `--`, which
183-
* reads the same as an unknown option here).
190+
* both cases name nothing, as does a dash-led operand escaped with `--`, which
191+
* may be the flag itself (`files rename <id> -- --name`).
184192
*/
185193
function misplacedFlagValue(command: Command): { option: Option; value: string } | undefined {
186194
const missing = missingMandatory(command)
187-
if (missing.length !== 1 || unknownOptionToken(command) !== undefined) return undefined
195+
if (missing.length !== 1 || command.args.some(isDashLed)) return undefined
188196
if (command.registeredArguments.some((argument) => argument.variadic)) return undefined
189197
const extra = command.args.slice(command.registeredArguments.length)
190198
return extra.length === 1 ? { option: missing[0], value: extra[0] } : undefined
@@ -370,6 +378,12 @@ function parseErrorGuidance(command: Command, code: string | undefined): string[
370378
* one would answer for all of them.
371379
*/
372380
function addParseErrorGuidance(command: Command): void {
381+
const parseOptions = command.parseOptions.bind(command)
382+
command.parseOptions = (argv) => {
383+
const parsed = parseOptions(argv)
384+
UNKNOWN_ARGUMENTS.set(command, parsed.unknown)
385+
return parsed
386+
}
373387
const error = command.error.bind(command)
374388
command.error = (message, options) => {
375389
const guidance = parseErrorGuidance(command, options?.code)

‎packages/sim-cli/src/runtime/file-references.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ describe('one --workflow flag across commands', () => {
229229
}
230230
})
231231

232+
it.each([
233+
[
234+
['files', 'rename', 'file_1', '--', '--name'],
235+
'Example: sim files rename <fileId> --name <value>',
236+
],
237+
[
238+
['workflows', 'runs', 'get', '--', '-Ab3x'],
239+
'Example: sim workflows runs get <runId> --workflow',
240+
],
241+
])('names no unknown option for an operand escaped with -- (%j)', async (argv, example) => {
242+
const result = await runEmbeddedCli(argv, identity)
243+
expect(result.exitCode).toBe(1)
244+
expect(result.stderr).not.toContain('is not an option of')
245+
expect(result.stderr).not.toContain('Did you mean')
246+
expect(result.stderr).toContain(example)
247+
})
248+
232249
it('suggests the required flag an unknown one was typed for, which commander never reaches', async () => {
233250
const result = await runEmbeddedCli(
234251
['workflows', 'runs', 'get', 'run_a', '--workflow-id', WORKFLOW_ID],

0 commit comments

Comments
 (0)