Skip to content

fix(cli): add positional argument completion for zsh leaf commands - #69509

Closed
armorbreak001 wants to merge 1 commit into
openclaw:mainfrom
armorbreak001:fix/69293-zsh-completion-positional-args
Closed

armorbreak001 wants to merge 1 commit into
openclaw:mainfrom
armorbreak001:fix/69293-zsh-completion-positional-args

Conversation

@armorbreak001

Copy link
Copy Markdown

Summary

Zsh completion for leaf commands (e.g. openclaw wiki ingest) was missing positional argument specs, so <Tab> did not trigger file-path completion for commands that accept file arguments.

Root cause: generateZshSubcommands only emitted _arguments with option flags (generateZshArgs) for leaf commands — it never included "N:description:_files" lines for registered .argument() definitions.

Changes

  • Added generateZshPositionalArgs(cmd) helper that reads argument metadata from Commander.js internal _args / registeredArguments (with fallback between the two for version compatibility) and emits zsh positional argument completion specs ("1:markdown file:_files" style).
  • Wired the new helper into the leaf command template so positional args are now included in _arguments calls.

Fix

Closes #69293

Notes

  • Uses _files action for all positional arguments since the vast majority of openclaw leaf-command args are file paths (ingest, export, import, etc.). Commands with non-file positional args (if any) would need a more sophisticated mapper in a follow-up.
  • Defensive: returns empty string when no args are registered, so existing behavior is preserved for commands without positional arguments.

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: XS labels Apr 21, 2026
@greptile-apps

greptile-apps Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds generateZshPositionalArgs to wire positional argument specs into zsh leaf-command completion functions, fixing the missing file-path tab-completion for commands like openclaw wiki ingest. The approach is correct in spirit, but three bugs in the implementation prevent it from working reliably.

  • arg.description?.() treats the Commander.js Argument.description string as a callable — it will throw TypeError at runtime for any argument that has a description, breaking completion generation entirely.
  • The if (arg.variadic) branch returns the same n:desc:_files spec as the non-variadic path instead of *:desc:_files, so variadic args never complete beyond the first value.
  • When a leaf command has no positional args, the template still emits an unconditional trailing \ continuation that passes } as a word to _arguments, producing a broken completion function for the majority of existing leaf commands.

Confidence Score: 2/5

Not safe to merge — three P1 bugs cause runtime errors and broken zsh completions for existing commands.

All three issues are present defects in the changed code: a TypeError throw during script generation (description-as-function), wrong zsh spec for variadic args, and a syntax-corrupting trailing backslash that affects all leaf commands that have options but no positional args. Together they mean the fix either crashes or silently produces broken completions for the majority of commands.

src/cli/completion-cli.ts — specifically lines 231, 237–240, and 280–285.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 231

Comment:
**`description` is a string, not a function**

`Argument.description` in Commander.js is a plain string property, not a getter method. Calling `arg.description?.()` will throw `TypeError: arg.description is not a function` at runtime whenever a positional argument actually has a description string set (the `?.` short-circuits only for `null`/`undefined`, not for a non-null string value). The fallback to `arg.name` therefore never fires for described args — instead the whole completion generation crashes.

Fix: use the value directly:
```suggestion
      const desc = (arg.description ?? arg.name)
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 237-240

Comment:
**Variadic branch produces identical output to non-variadic**

Both arms of the `if (arg.variadic)` check return the same string (`${n}:${desc}:_files`), so the branch is dead code. In zsh `_arguments`, a fixed positional slot uses `n:msg:action` while variadic (repeatable) slots use `*:msg:action`. Using a fixed index for a variadic arg means only the first file is ever completed; subsequent values get no completion.

```suggestion
      if (arg.variadic) {
        return `*:${desc}:_files`;
      }
      return `${n}:${desc}:_files`;
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 280-285

Comment:
**Trailing `\` produces invalid zsh syntax for commands with no positional args**

When `generateZshPositionalArgs(cmd)` returns `""` (the command has no positional arguments), the template still emits a backslash continuation from the previous line, leaving an empty continuation line followed by `}`. Zsh treats `}` as a word argument to `_arguments`, corrupting the completion function for every leaf command that has options but no positional arguments — the majority of commands.

Only emit the extra `\` and positional-args fragment when the helper returns a non-empty string:

```typescript
const positional = generateZshPositionalArgs(cmd);
segments.push(`
${funcName}() {
  _arguments -C \\
    ${generateZshArgs(cmd)}${positional ? ` \\\n    ${positional}` : ""}
}
`);
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(cli): add positional argument comple..." | Re-trigger Greptile

Comment thread src/cli/completion-cli.ts
return args
.map((arg, i) => {
const n = i + 1;
const desc = (arg.description?.() ?? arg.name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 description is a string, not a function

Argument.description in Commander.js is a plain string property, not a getter method. Calling arg.description?.() will throw TypeError: arg.description is not a function at runtime whenever a positional argument actually has a description string set (the ?. short-circuits only for null/undefined, not for a non-null string value). The fallback to arg.name therefore never fires for described args — instead the whole completion generation crashes.

Fix: use the value directly:

Suggested change
const desc = (arg.description?.() ?? arg.name)
const desc = (arg.description ?? arg.name)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 231

Comment:
**`description` is a string, not a function**

`Argument.description` in Commander.js is a plain string property, not a getter method. Calling `arg.description?.()` will throw `TypeError: arg.description is not a function` at runtime whenever a positional argument actually has a description string set (the `?.` short-circuits only for `null`/`undefined`, not for a non-null string value). The fallback to `arg.name` therefore never fires for described args — instead the whole completion generation crashes.

Fix: use the value directly:
```suggestion
      const desc = (arg.description ?? arg.name)
```

How can I resolve this? If you propose a fix, please make it concise.

Comment thread src/cli/completion-cli.ts
Comment on lines +237 to +240
if (arg.variadic) {
return `${n}:${desc}:_files`;
}
return `${n}:${desc}:_files`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Variadic branch produces identical output to non-variadic

Both arms of the if (arg.variadic) check return the same string (${n}:${desc}:_files), so the branch is dead code. In zsh _arguments, a fixed positional slot uses n:msg:action while variadic (repeatable) slots use *:msg:action. Using a fixed index for a variadic arg means only the first file is ever completed; subsequent values get no completion.

Suggested change
if (arg.variadic) {
return `${n}:${desc}:_files`;
}
return `${n}:${desc}:_files`;
if (arg.variadic) {
return `*:${desc}:_files`;
}
return `${n}:${desc}:_files`;
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 237-240

Comment:
**Variadic branch produces identical output to non-variadic**

Both arms of the `if (arg.variadic)` check return the same string (`${n}:${desc}:_files`), so the branch is dead code. In zsh `_arguments`, a fixed positional slot uses `n:msg:action` while variadic (repeatable) slots use `*:msg:action`. Using a fixed index for a variadic arg means only the first file is ever completed; subsequent values get no completion.

```suggestion
      if (arg.variadic) {
        return `*:${desc}:_files`;
      }
      return `${n}:${desc}:_files`;
```

How can I resolve this? If you propose a fix, please make it concise.

Comment thread src/cli/completion-cli.ts
Comment on lines 280 to 285
segments.push(`
${funcName}() {
_arguments -C \\
${generateZshArgs(cmd)}
${generateZshArgs(cmd)} \\
${generateZshPositionalArgs(cmd)}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Trailing \ produces invalid zsh syntax for commands with no positional args

When generateZshPositionalArgs(cmd) returns "" (the command has no positional arguments), the template still emits a backslash continuation from the previous line, leaving an empty continuation line followed by }. Zsh treats } as a word argument to _arguments, corrupting the completion function for every leaf command that has options but no positional arguments — the majority of commands.

Only emit the extra \ and positional-args fragment when the helper returns a non-empty string:

const positional = generateZshPositionalArgs(cmd);
segments.push(`
${funcName}() {
  _arguments -C \\
    ${generateZshArgs(cmd)}${positional ? ` \\\n    ${positional}` : ""}
}
`);
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cli/completion-cli.ts
Line: 280-285

Comment:
**Trailing `\` produces invalid zsh syntax for commands with no positional args**

When `generateZshPositionalArgs(cmd)` returns `""` (the command has no positional arguments), the template still emits a backslash continuation from the previous line, leaving an empty continuation line followed by `}`. Zsh treats `}` as a word argument to `_arguments`, corrupting the completion function for every leaf command that has options but no positional arguments — the majority of commands.

Only emit the extra `\` and positional-args fragment when the helper returns a non-empty string:

```typescript
const positional = generateZshPositionalArgs(cmd);
segments.push(`
${funcName}() {
  _arguments -C \\
    ${generateZshArgs(cmd)}${positional ? ` \\\n    ${positional}` : ""}
}
`);
```

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c016fa94d7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/cli/completion-cli.ts
if (arg.variadic) {
return `${n}:${desc}:_files`;
}
return `${n}:${desc}:_files`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Quote positional specs before passing them to _arguments

The positional completion entries are emitted as raw words (${n}:${desc}:_files) rather than quoted _arguments specs, so any argument description containing spaces gets split into multiple shell tokens and _arguments misparses the definition. This affects many CLI arguments with multi-word descriptions (for example, "Model id or alias"), causing zsh completion to fail or behave unpredictably for those leaf commands. Emit each positional spec as a single quoted argument (or escape spaces) so zsh receives one complete spec per position.

Useful? React with 👍 / 👎.

@prtags prtags Bot mentioned this pull request Apr 23, 2026
7 tasks done
@prtags

prtags Bot commented Apr 23, 2026

Copy link
Copy Markdown

Related work from PRtags group pretty-bear-eteg

Title: Open PR duplicate: [Bug]: zsh completion for openclaw wiki ingest does not complete file paths

Number Title
#69305 CLI: complete positional args in zsh completion
#69352 fix: complete zsh positional wiki ingest paths
#69509* fix(cli): add positional argument completion for zsh leaf commands

* This PR

@clawsweeper

clawsweeper Bot commented Apr 26, 2026 •

Copy link
Copy Markdown
Contributor

Closing this as duplicate or superseded after Codex automated review.

Close #69509 as a duplicate of open PR #69305. Current main still lacks the zsh positional-argument completion fix, so the underlying #69293 bug remains real, but #69305 already tracks the same work with broader handling and regression tests. #69509 is a one-file duplicate with unresolved correctness findings.

Best possible solution:

Close #69509 as duplicate cleanup and keep the remaining work consolidated on #69305 and the linked #69293 bug. The eventual fix should emit quoted zsh positional specs from Commander argument metadata, handle optional and variadic arguments correctly, choose file/directory actions conservatively, and include regression tests in src/cli/completion-cli.test.ts.

What I checked:

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex Review notes: model gpt-5.5, reasoning high; reviewed against ee2ab9a64492.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: zsh completion for openclaw wiki ingest does not complete file paths

1 participant