Skip to content

fix(cli): replace broken shell completion with full flag+alias support#454

Merged
mchmarny merged 3 commits intomainfrom
fix/cli-shell-completion
Mar 21, 2026
Merged

fix(cli): replace broken shell completion with full flag+alias support#454
mchmarny merged 3 commits intomainfrom
fix/cli-shell-completion

Conversation

@nvidiajeff
Copy link
Copy Markdown
Contributor

Summary

  • Fix command execution during TAB completion: aicr snapshot --<TAB> was executing the actual command (deploying agents) instead of showing flag completions. Root cause: urfave/cli v3 disables completion mode when -- precedes --generate-shell-completion. sanitizeCompletionArgs works around this by replacing bare -- with - before cmd.Run.
  • Fix partial flag completion: aicr verify --form<TAB> produced no output because --form fails urfave/cli's flag parser and never appears in cmd.Args(). completeWithAllFlags now reads os.Args directly to see the partial flag.
  • Show flag aliases in completions: Both --accelerator and --gpu now appear in TAB completions. The previous commandLister function (and urfave/cli's DefaultCompleteWithFlags) only showed primary flag names.
  • Remove redundant EnableShellCompletion: true from 7 subcommands — only meaningful on the root command in urfave/cli v3.

Test plan

  • go test -v -run TestCompletion ./pkg/cli/ — all 5 completion test suites pass
  • go test -race ./pkg/cli/ — no regressions
  • make build then manual verification:
    • aicr --generate-shell-completion → lists subcommands ✅
    • aicr bundle - --generate-shell-completion → lists all flags with aliases ✅
    • aicr snapshot -- --generate-shell-completion → lists flags (not command execution) ✅
    • aicr verify --form --generate-shell-completion → shows --format

@nvidiajeff nvidiajeff requested a review from a team as a code owner March 21, 2026 04:13
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 21, 2026

Coverage Report ✅

Metric Value
Coverage 73.9%
Threshold 70%
Status Pass
Coverage Badge
![Coverage](https://img.shields.io/badge/coverage-73.9%25-green)

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/NVIDIA/aicr/pkg/cli 33.85% (+4.31%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/NVIDIA/aicr/pkg/cli/bundle.go 0.72% (ø) 139 1 138
github.com/NVIDIA/aicr/pkg/cli/bundle_verify.go 35.09% (ø) 57 20 37
github.com/NVIDIA/aicr/pkg/cli/query.go 44.16% (+1.30%) 77 34 (+1) 43 (-1) 👍
github.com/NVIDIA/aicr/pkg/cli/recipe.go 76.54% (ø) 81 62 19
github.com/NVIDIA/aicr/pkg/cli/root.go 69.05% (+24.86%) 84 (+41) 58 (+39) 26 (+2) 🌟
github.com/NVIDIA/aicr/pkg/cli/snapshot.go 5.00% (ø) 40 2 38
github.com/NVIDIA/aicr/pkg/cli/trust.go 22.22% (ø) 9 2 7
github.com/NVIDIA/aicr/pkg/cli/validate.go 21.05% (ø) 152 32 120

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Shell completion was broken for all subcommands. Typing `aicr bundle --<TAB>`
executed the command instead of showing flag completions because a custom
`commandLister` on the root command overrode urfave/cli's built-in completion
and only listed top-level subcommand names — never flags.

Three issues are fixed:

1. Remove `commandLister` and replace it with `completeWithAllFlags`, a custom
   ShellComplete function that lists all flag names including aliases (e.g.
   both `--accelerator` and `--gpu`). The function is applied recursively to
   all subcommands via `setShellComplete` so urfave/cli's `setupDefaults`
   cannot replace it with `DefaultCompleteWithFlags` (which omits aliases).

2. Work around a urfave/cli v3 bug where `--` immediately before
   `--generate-shell-completion` disables completion mode entirely
   (`checkShellCompleteFlag` treats `--` as a POSIX flag terminator). This
   caused `aicr snapshot --<TAB>` to execute the snapshot action — deploying
   an agent to the cluster. `sanitizeCompletionArgs` replaces the bare `--`
   with `-` before passing args to `cmd.Run`, keeping completion mode active.

3. Read `os.Args` directly (via `completionLastArg`) instead of `cmd.Args()`
   to determine what the user was typing. Partial flags like `--form` fail
   urfave/cli's flag parser and never appear in parsed positional args, so
   `cmd.Args()` is empty. `os.Args` always has the original command line.

Also removes `EnableShellCompletion: true` from 7 subcommands — the field is
only meaningful on the root command in urfave/cli v3.
@mchmarny mchmarny assigned nvidiajeff and unassigned mchmarny Mar 21, 2026
@mchmarny mchmarny added this to the M2 - KubeCon EU milestone Mar 21, 2026
@mchmarny mchmarny added the bug Something isn't working label Mar 21, 2026
@mchmarny mchmarny merged commit cdc9bf4 into main Mar 21, 2026
17 checks passed
@mchmarny mchmarny deleted the fix/cli-shell-completion branch March 21, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli bug Something isn't working size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants