Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-zsh-mixed-command-completions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix Zsh completions for CLI commands with both positional arguments and subcommands.
10 changes: 8 additions & 2 deletions packages/effect/src/unstable/cli/internal/completions/zsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ const generateFunction = (
lines.push(` ${spec}`)
}
}
for (const arg of descriptor.arguments) {
lines.push(` ${argSpec(arg)}`)
if (descriptor.arguments.length > 0) {
lines.push(` -`)
lines.push(` parent-arguments`)
for (const arg of descriptor.arguments) {
lines.push(` ${argSpec(arg)}`)
}
lines.push(` -`)
lines.push(` subcommands`)
}
lines.push(` '1:command:->command'`)
lines.push(` '*::arg:->args'`)
Expand Down
32 changes: 32 additions & 0 deletions packages/effect/test/unstable/cli/completions/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ const withPaths = Command.make("process", {
)
}).pipe(Command.withDescription("Process files"))

const withOptionalDirectoryAndSubcommands = Command.make("example", {
directory: Argument.directory("directory").pipe(
Argument.withDescription("Directory to start in"),
Argument.optional
)
}).pipe(
Command.withSubcommands([
Command.make("serve").pipe(Command.withDescription("Start the server"))
])
)

const nested3Levels = (() => {
const leaf = Command.make("action", {
dryRun: Flag.boolean("dry-run").pipe(Flag.withDescription("Dry run mode"))
Expand Down Expand Up @@ -341,6 +352,27 @@ describe("Zsh completions", () => {
assert.include(script, "(us-east eu-west ap-south)")
})

it("uses alternative argument sets for positional arguments and subcommands", () => {
const desc = fromCommand(withOptionalDirectoryAndSubcommands)
const script = Zsh.generate("example", desc)

assert.include(
script,
` -
parent-arguments
':Directory to start in:_directories'
-
subcommands
'1:command:->command'
'*::arg:->args'`
)
assert.notInclude(
script,
` ':Directory to start in:_directories'
'1:command:->command'`
)
})

it("starts with #compdef directive", () => {
const desc = fromCommand(simpleCmd)
const script = Zsh.generate("greet", desc)
Expand Down