Skip to content

Commit ee5550a

Browse files
only21milclaudeeffect-bot
authored
Fix CLI wizard repeating flags and arguments typed before --wizard (#10529)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Effect Bot <[email protected]>
1 parent 3a65e9e commit ee5550a

6 files changed

Lines changed: 111 additions & 22 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix CLI `--wizard` generating a command that fails to parse when flags or arguments were already typed for the selected command.

‎packages/effect/src/cli/Command.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,9 +2161,13 @@ export const runWith = <const Name extends string, Input, E, R, ContextInput>(
21612161
const { colors } = yield* Prompt.Theme
21622162
return yield* Effect.gen(function*() {
21632163
yield* Console.log(Wizard.renderIntroduction(command.name, config.version, command.description, colors))
2164+
// The wizard prompts for the selected command's inputs, so drop the typed ones.
2165+
const selectedTokens = new Set(Parser.getSelectedCommandTokens(remainder, command))
21642166
const prefix = [
21652167
command.name,
2166-
...args.filter((arg) => arg !== "--wizard" && !arg.startsWith("--wizard="))
2168+
...tokens.filter((token) =>
2169+
!selectedTokens.has(token) && !(token._tag === "LongOption" && token.name === "wizard")
2170+
).map(Lexer.formatToken)
21672171
]
21682172
const wizardResult = yield* Wizard.run(command, { commandPath, prefix })
21692173
yield* Console.log(Wizard.renderCompletion(wizardResult.displayArgs, colors))

‎packages/effect/src/cli/internal/lexer.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ export function lex(argv: ReadonlyArray<string>): LexResult {
4646
}
4747
}
4848

49+
/**
50+
* Formats a token as an argument. Short option clusters become one argument
51+
* per flag.
52+
*
53+
* @internal
54+
*/
55+
export const formatToken = (token: Token): string => {
56+
if (token._tag === "Value") return token.value
57+
if (token._tag === "LongOption") return token.raw
58+
// Numeric aliases need long-option syntax to avoid becoming negative numbers.
59+
const flag = negativeNumberRegex.test(token.raw) ? `--${token.flag}` : token.raw
60+
return token.value === undefined ? flag : `${flag}=${token.value}`
61+
}
62+
4963
const negativeNumberRegex = /^-(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/
5064

5165
const lexTokens = (args: ReadonlyArray<string>): ReadonlyArray<Token> => {

‎packages/effect/src/cli/internal/parser.ts‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,7 @@ export const parseArgs = (
5858
Effect.gen(function*() {
5959
const { tokens, trailingOperands: afterEndOfOptions } = lexResult
6060
const newCommandPath = [...commandPath, command.name]
61-
62-
const commandImpl = toImpl(command)
63-
const singles = commandImpl.config.flags.flatMap(Param.extractSingleParams)
64-
const flagParams = singles.filter(Param.isFlagParam)
65-
const flagRegistry = createFlagRegistry(flagParams)
66-
67-
const inheritedSingles = commandImpl.contextConfig.flags.flatMap(Param.extractSingleParams)
68-
const inheritedFlagParams = inheritedSingles.filter(Param.isFlagParam)
69-
const inheritedFlagRegistry = createFlagRegistry(inheritedFlagParams)
70-
const inheritedNames = new Set(inheritedFlagParams.map((param) => param.name))
71-
72-
const context: CommandContext = {
73-
command,
74-
commandPath: newCommandPath,
75-
flagRegistry,
76-
inheritedFlagRegistry,
77-
localFlagNames: flagParams.filter((param) => !inheritedNames.has(param.name)).map((param) => param.name)
78-
}
79-
80-
const result = scanCommandLevel(tokens, context)
61+
const result = scanCommandLevel(tokens, makeCommandContext(command, newCommandPath))
8162

8263
if (result._tag === "Leaf") {
8364
return {
@@ -104,6 +85,20 @@ export const parseArgs = (
10485
}
10586
})
10687

88+
/**
89+
* Returns the subset of `tokens` left for the selected command once its
90+
* ancestors have consumed subcommand names and inherited flags.
91+
*
92+
* @internal
93+
*/
94+
export const getSelectedCommandTokens = (
95+
tokens: ReadonlyArray<Token>,
96+
command: Command.Any
97+
): ReadonlyArray<Token> => {
98+
const result = scanCommandLevel(tokens, makeCommandContext(command, []))
99+
return result._tag === "Leaf" ? tokens : getSelectedCommandTokens(result.childTokens, result.sub)
100+
}
101+
107102
/* ========================================================================== */
108103
/* Types */
109104
/* ========================================================================== */
@@ -945,6 +940,19 @@ const processValue = (
945940
/* Command Level Scanning */
946941
/* ========================================================================== */
947942

943+
const makeCommandContext = (command: Command.Any, commandPath: ReadonlyArray<string>): CommandContext => {
944+
const flagParams = extractFlagParams(command)
945+
const inheritedFlagParams = extractContextFlagParams(command)
946+
const inheritedNames = new Set(inheritedFlagParams.map((param) => param.name))
947+
return {
948+
command,
949+
commandPath,
950+
flagRegistry: createFlagRegistry(flagParams),
951+
inheritedFlagRegistry: createFlagRegistry(inheritedFlagParams),
952+
localFlagNames: flagParams.filter((param) => !inheritedNames.has(param.name)).map((param) => param.name)
953+
}
954+
}
955+
948956
/**
949957
* Scans a single command level, processing all tokens.
950958
*

‎packages/effect/src/cli/internal/wizard.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const promptCommand: (
8484

8585
if (config.arguments.length > 0) {
8686
yield* Console.log(renderSection(sectionName, "ARGUMENTS", colors))
87+
// Keep positional answers from binding to preceding boolean flags.
88+
if (visibleSubcommands.length === 0) {
89+
commandLine.push(commandLineArg("--"))
90+
}
8791
for (const param of config.arguments) {
8892
commandLine.push(...yield* promptParam(param))
8993
}

‎packages/effect/test/cli/Command.test.ts‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe("Command", () => {
161161
yield* MockTerminal.inputKey("enter")
162162
yield* Fiber.join(fiber)
163163

164-
assert.deepStrictEqual(captured, [["greet", "--name", "Alice", "2"]])
164+
assert.deepStrictEqual(captured, [["greet", "--name", "Alice", "--", "2"]])
165165
}).pipe(Effect.provide(TestLayer)))
166166

167167
it.effect("should run wizard-generated arguments with --wizard", () =>
@@ -361,6 +361,60 @@ describe("Command", () => {
361361
assert.deepStrictEqual(captured, ["selected"])
362362
}).pipe(Effect.provide(TestLayer)))
363363

364+
it.effect("should replace typed flags and arguments with wizard answers", () =>
365+
Effect.gen(function*() {
366+
const captured: Array<readonly [string, string]> = []
367+
const command = Command.make("demo", {
368+
name: Flag.String("name"),
369+
value: Argument.String("value")
370+
}, ({ name, value }) => Effect.sync(() => captured.push([name, value])))
371+
372+
const fiber = yield* Command.runWith(command, { version: "1.0.0" })([
373+
"--name",
374+
"typed",
375+
"typed",
376+
"--wizard"
377+
]).pipe(Effect.forkChild)
378+
yield* MockTerminal.inputText("prompted")
379+
yield* MockTerminal.inputKey("enter")
380+
yield* MockTerminal.inputText("prompted")
381+
yield* MockTerminal.inputKey("enter")
382+
yield* MockTerminal.inputKey("enter")
383+
yield* Fiber.join(fiber)
384+
385+
assert.deepStrictEqual(captured, [["prompted", "prompted"]])
386+
}).pipe(Effect.provide(TestLayer)))
387+
388+
it.effect("should preserve inherited clustered flags before a boolean-looking wizard answer", () =>
389+
Effect.gen(function*() {
390+
const captured: Array<readonly [boolean, boolean, string]> = []
391+
const root = Command.make("root").pipe(
392+
Command.withSharedFlags({
393+
silent: Flag.Boolean("silent").pipe(Flag.withAlias("s")),
394+
ipv4: Flag.Boolean("ipv4").pipe(Flag.withAlias("4"))
395+
})
396+
)
397+
const child = Command.make("child", { value: Argument.String("value") }, ({ value }) =>
398+
Effect.gen(function*() {
399+
const { silent, ipv4 } = yield* root
400+
captured.push([silent, ipv4, value])
401+
}))
402+
const command = root.pipe(Command.withSubcommands([child]))
403+
404+
const fiber = yield* Command.runWith(command, { version: "1.0.0" })([
405+
"child",
406+
"-s4",
407+
"typed",
408+
"--wizard"
409+
]).pipe(Effect.forkChild)
410+
yield* MockTerminal.inputText("false")
411+
yield* MockTerminal.inputKey("enter")
412+
yield* MockTerminal.inputKey("enter")
413+
yield* Fiber.join(fiber)
414+
415+
assert.deepStrictEqual(captured, [[true, true, "false"]])
416+
}).pipe(Effect.provide(TestLayer)))
417+
364418
it.effect("should reject --completions without a shell", () =>
365419
Effect.gen(function*() {
366420
let invoked = false

0 commit comments

Comments
 (0)