Skip to content

complete per-command flags in the shell completions#97

Open
Kirananandan wants to merge 1 commit into
qorexdevs:mainfrom
Kirananandan:completion-per-command-flags
Open

complete per-command flags in the shell completions#97
Kirananandan wants to merge 1 commit into
qorexdevs:mainfrom
Kirananandan:completion-per-command-flags

Conversation

@Kirananandan

@Kirananandan Kirananandan commented Jul 3, 2026

Copy link
Copy Markdown

Closes #96.

Past the subcommand, the completion scripts only ever offered the five shared flags (-q --quiet --json --dry-run --help), so command-specific flags like restore --clean, diff --stat/--name-only, find --ids, save -n, or gc --keep never showed up on tab.

What changed

  • _flags_for_commands() — a helper next to _subcommands() that walks each subparser's _actions off the argparse parser and maps every public subcommand to its option strings, so the map stays in sync as flags are added.
  • Four small per-shell serializers (_bash_flagmap, _zsh_flagmap, _fish_flagmap, _pwsh_flagmap) render that map into each script's native form (bash/zsh associative array, fish complete lines, PowerShell hashtable), injected via a new __FLAGMAP__ placeholder.
  • The dash branch of each of the four shell blocks now offers ${command}'s own flags instead of the shared five. Refs and file fallback are unchanged.
  • Fish additionally keeps top-level flag completion (--version, --help) before a subcommand is chosen — the old unguarded global complete line offered those, and the per-command rewrite would otherwise have dropped them. Derived from the root parser via _root_flags().

Scope stayed in cli.py as the issue suggested, plus a docs note and a changelog entry.

Testing

  • Added per-shell tests asserting each command's real flags land in the map and don't leak across commands (e.g. --keep doesn't appear under restore), plus a fish root-flag test.
  • Full suite green (298 passed).
  • Verified the generated bash and PowerShell scripts functionally on real shells: restore --<TAB>--clean --only-missing ..., gc --<TAB>--keep --keep-named --keep-within, and the hyphenated check-ignore --<TAB>--exit-code all complete correctly. The PowerShell script also parses clean via the PS parser.

Past the subcommand the completion scripts only ever offered the five shared
flags, so command-specific flags like `restore --clean`, `diff --stat`,
`find --ids`, `save -n` or `gc --keep` never showed up on tab.

Build a per-command flag map by walking each subparser`s _actions off the
argparse parser (the same way `_subcommands()` walks the choices), then inject
it into all four completion templates. When the current word starts with `-`
each shell now offers that command`s own flags instead of the shared five.
Fish also keeps top-level flag completion (`--version`, `--help`) before a
subcommand is chosen, derived from the root parser so it stays in sync.

Closes qorexdevs#96
@Kirananandan Kirananandan force-pushed the completion-per-command-flags branch from 5f7c273 to 8145517 Compare July 3, 2026 11:39
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.

complete per-command flags in the shell completions

1 participant