Skip to content

feat(cli): add shell flag-value completion for --checks/--export/--proxy-type - #59

Merged
francomano merged 1 commit into
francomano:masterfrom
MsfPablo:feat/22-shell-completion
Sep 2, 2026
Merged

feat(cli): add shell flag-value completion for --checks/--export/--proxy-type#59
francomano merged 1 commit into
francomano:masterfrom
MsfPablo:feat/22-shell-completion

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

What

Closes #22 — shell completion support.

Background

Cobra already generates a completion command (bash/zsh/fish/powershell) that outputs valid scripts and completes subcommands + flag names. That covers two of the three acceptance criteria out of the box. The real gap is the third criterion ("completions work with all subcommands and flags"): flag values are not completed. diagnose --checks <Tab> fell back to file completion, and --export / --proxy-type likewise.

Changes

  • cmd/cli/commands/completion.go — registers flag-value completion functions:
    • --checks: default check IDs + their categories + the literal all, with comma-segment completion (only the final segment is narrowed, preserving the typed prefix).
    • --export: text / json / html / markdown.
    • --proxy-type: auto / http / https / socks4 / socks5.
  • cmd/cli/commands/completion_test.go — unit tests for each helper (prefix filtering, comma handling, exhaustive value sets, NoFileComp directive).
  • README.md — new "Shell Completion" section with per-shell install instructions.

Registration uses cobra.OnInitialize rather than a bare init(): Go runs package init()s in filename order, so completion.go runs before diagnose.go / plugins.go define the --checks / --export / --proxy-type flags, and RegisterFlagCompletionFunc rejects a missing flag. Deferring to Execute time runs after every init() has completed.

Verification

  • go build ./cmd/cli
  • go test ./cmd/cli/commands/...
  • End-to-end via the __complete hidden command:
    • __complete diagnose --checks ddns_leak, dns_resolve (:4 NoFileComp)
    • __complete diagnose --checks public_ip,dnspublic_ip,dns_leak, public_ip,dns_resolve
    • __complete diagnose --proxy-type ssocks4, socks5
    • __complete diagnose --export mmarkdown

go test ./... shows one failure in core/adapters (TestAdapterHTTPSProxy_HTTPRequest, an x509 cert-verification error in a local HTTPS-proxy fixture). It reproduces identically on master without these changes and is unrelated to cmd/cli/commands.

Note

The completion script is keyed to the root command name (proxyctl); the README section calls this out for anyone invoking the binary under a different name.

…oxy-type

Cobra already ships a completion command that completes subcommands and flag
names. It cannot, however, suggest values for flags whose accepted set is
domain-specific: --checks <Tab> fell back to file completion.

Register flag-value completion functions via cobra.OnInitialize (deferred to
Execute time so the flags exist when registration runs, since package init
order is alphabetical and completion.go runs before the flags are defined):

- --checks: default check IDs + their categories + the literal "all", with
  comma-segment completion so only the final segment is narrowed.
- --export: text/json/html/markdown.
- --proxy-type: auto/http/https/socks4/socks5.

Adds unit tests for each helper (prefix filtering, comma handling, exhaustive
value sets) and a README Shell Completion section with per-shell install
instructions.

Closes francomano#22
@francomano
francomano merged commit 8fb6cd2 into francomano:master Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add shell completion support (bash, zsh, fish)

2 participants