Skip to content

Commit bae2c62

Browse files
committed
fix(cli): refuse a flag's two spellings as they are parsed, before any hook runs
Both spellings at once were refused only when the generated action ran, after catalog's preAction had already resolved a workflow name (a listWorkflows request). The parse-time copy now refuses the pair in either order with commander's error, and one helper words the refusal for both checks.
1 parent 04eadc5 commit bae2c62

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { DEFAULT_PAGE_SIZE, defaultListLimit, OTHER_SPELLING_SOURCE } from './op
1818
import { renameWarningOptions, warnRenamedFlag } from './renamed'
1919
import {
2020
type BuiltRequest,
21+
bothSpellingsRefusal,
2122
buildRequest,
2223
cursorSlot,
2324
flagNameFor,
@@ -422,10 +423,7 @@ function foldOtherSpellings(
422423
const supplied = flags[camel(name)]
423424
if (supplied === undefined) continue
424425
if (taken) {
425-
throw new SimApiError(
426-
`--${name} is ${renamed ? 'the former name' : 'another spelling'} of --${current}; pass one, not both`,
427-
0
428-
)
426+
throw new SimApiError(bothSpellingsRefusal(name, renamed, current), 0)
429427
}
430428
if (renamed) warnRenamedFlag(host, name, current)
431429
if (!copied) flags[camel(current)] = supplied

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ describe('one --workflow flag across commands', () => {
214214
expect(result.requests).toEqual([])
215215
})
216216

217+
it.each([
218+
[['--workflow-id', 'Nightly sync', '--workflow', 'Nightly sync']],
219+
[['--workflow', 'Nightly sync', '--workflow-id', 'Nightly sync']],
220+
])(
221+
'refuses both spellings on workflows source catalog before resolving a name (%j)',
222+
async (flags) => {
223+
const result = await invoke(['workflows', 'source', 'catalog', ...flags])
224+
expect(result.exitCode).toBe(1)
225+
expect(result.stderr).toContain(
226+
'--workflow-id is the former name of --workflow; pass one, not both'
227+
)
228+
expect(result.requests).toEqual([])
229+
}
230+
)
231+
217232
it('still requires a workflow on workflow-mcp-servers tools create, by either spelling', async () => {
218233
const result = await invoke(['workflow-mcp-servers', 'tools', 'create', 'srv_a'])
219234
expect(result.exitCode).toBe(1)

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type Command, Option } from 'commander'
22
import type { CommandSpec } from '../contract/types'
33
import type { V2OperationName } from '../generated/v2-api'
44
import {
5+
bothSpellingsRefusal,
56
cursorSlot,
67
type FieldSpec,
78
flagNameFor,
@@ -258,7 +259,6 @@ function addFieldOption(
258259
: ''
259260
}${literalNull ? literalNullHint(documented, name) : ''}`
260261

261-
const hiddenNames = otherSpellings(flag).map(({ name }) => name)
262262
const option = new Option(`${short}--${name} ${placeholder}`, describe)
263263
if (choices && !takesList) option.choices([...choices])
264264
if (descriptor.default !== undefined && field !== 'limit') {
@@ -271,30 +271,41 @@ function addFieldOption(
271271
if (descriptor.required && !flag.trailing) option.makeOptionMandatory()
272272
command.addOption(option)
273273

274-
for (const previous of hiddenNames) {
274+
for (const { name: previous, renamed } of otherSpellings(flag)) {
275275
const hidden = new Option(`--${previous} ${placeholder}`).hideHelp()
276276
if (choices && !takesList) hidden.choices([...choices])
277277
command.addOption(hidden)
278-
copyOntoCurrentSpelling(command, hidden, option)
278+
copyOntoCurrentSpelling(command, hidden, option, renamed)
279279
}
280280
}
281281

282282
/**
283283
* Copies a value typed under another spelling onto the documented flag as it is parsed, so the
284284
* documented flag is the one commander's mandatory check, help, the command inventory and any
285-
* action read. The copy is marked `implied`, so `foldOtherSpellings` tells it from a value typed
286-
* under the documented flag: it still warns about a former name and refuses both spellings at once.
285+
* action read. The copy is marked `implied`, so `foldOtherSpellings` can still warn about a former
286+
* name. Both spellings at once are refused here, as they are parsed in either order, so no hook or
287+
* action (a workflow name resolved to its id) runs on a command line that names the flag twice.
287288
*/
288-
function copyOntoCurrentSpelling(command: Command, other: Option, current: Option): void {
289+
function copyOntoCurrentSpelling(
290+
command: Command,
291+
other: Option,
292+
current: Option,
293+
renamed: boolean
294+
): void {
295+
const refuseBoth = () =>
296+
command.error(bothSpellingsRefusal(other.name(), renamed, current.name()))
289297
command.on(`option:${other.name()}`, () => {
290298
const target = current.attributeName()
291-
if (command.getOptionValueSource(target) === 'cli') return
299+
if (command.getOptionValueSource(target) === 'cli') refuseBoth()
292300
command.setOptionValueWithSource(
293301
target,
294302
command.getOptionValue(other.attributeName()),
295303
OTHER_SPELLING_SOURCE
296304
)
297305
})
306+
command.on(`option:${current.name()}`, () => {
307+
if (command.getOptionValueSource(other.attributeName()) === 'cli') refuseBoth()
308+
})
298309
}
299310

300311
/** Adds request-field and safety options for one generated operation. */

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ export function otherSpellings(flag: FlagSpec): { name: string; renamed: boolean
167167
]
168168
}
169169

170+
/** The refusal when a flag is given under its documented name and another spelling at once. */
171+
export function bothSpellingsRefusal(other: string, renamed: boolean, current: string): string {
172+
return `--${other} is ${renamed ? 'the former name' : 'another spelling'} of --${current}; pass one, not both`
173+
}
174+
170175
/** The list field whose values may also follow the command as arguments, if any. */
171176
export function trailingListField(commandSpec: CommandSpec): string | undefined {
172177
return Object.entries(commandSpec.flags ?? {}).find(([, flag]) => flag.trailing)?.[0]

0 commit comments

Comments
 (0)