diff --git a/packages/sim-cli/src/runtime/build.ts b/packages/sim-cli/src/runtime/build.ts index 8e904d7e2b1..fc303b56c2f 100644 --- a/packages/sim-cli/src/runtime/build.ts +++ b/packages/sim-cli/src/runtime/build.ts @@ -1,3 +1,4 @@ +import { didYouMean } from '@sim/workflow-code' import { Command, type Option } from 'commander' import { CLI_CONTRACT } from '../contract/commands' import type { CommandSpec, CommandVariantSpec } from '../contract/types' @@ -171,15 +172,16 @@ function missingMandatory(command: Command): Option[] { /** * An unknown flag typed while a required one is missing, in commander's unknown-option words. * Commander checks required flags before unknown ones, so its own near-spelling suggestion never - * reaches `runs get --workflow-id`. With one required flag missing, that is the one suggested. + * reaches `runs get --workflow-id`. The missing flag spelled closest to the typed one is + * suggested; one spelled nothing like it (`--verbose` beside a missing `--workflow`) is not. */ function unknownBesideMissing(command: Command): string | undefined { const token = unknownOptionToken(command) if (!token) return undefined - const missing = missingMandatory(command) - const suggestion = - missing.length === 1 && missing[0].long ? ` (Did you mean ${missing[0].long}?)` : '' - return `${token.split('=', 1)[0]} is not an option of ${commandPath(command)}.${suggestion}` + const typed = token.split('=', 1)[0] + const missing = missingMandatory(command).flatMap((option) => (option.long ? [option.long] : [])) + const suggestion = didYouMean(typed, missing) + return `${typed} is not an option of ${commandPath(command)}.${suggestion ? ` (${suggestion})` : ''}` } /** diff --git a/packages/sim-cli/src/runtime/file-references.test.ts b/packages/sim-cli/src/runtime/file-references.test.ts index 8b3c69ceb67..360b63d1047 100644 --- a/packages/sim-cli/src/runtime/file-references.test.ts +++ b/packages/sim-cli/src/runtime/file-references.test.ts @@ -274,6 +274,22 @@ describe('one --workflow flag across commands', () => { 'Example: sim workflows runs get --workflow ' ) }) + + it.each([ + [ + ['workflows', 'runs', 'get', 'run_a', '--verbose'], + '--verbose is not an option of sim workflows runs get.', + ], + [ + ['tables', 'dispatches', 'create', 'tbl_a', '--limit', '5', '--yes'], + '--yes is not an option of sim tables dispatches create.', + ], + ])('suggests no required flag spelled nothing like the unknown one (%j)', async (argv, named) => { + const result = await runEmbeddedCli(argv, identity) + expect(result.exitCode).toBe(1) + expect(result.stderr).toContain(named) + expect(result.stderr).not.toContain('Did you mean') + }) }) describe('every id list reads repeated, comma-joined and JSON-array values alike', () => {