Skip to content

Do not offer value completions for boolean flags (fixes #116) - #136

Open
apoorvdarshan wants to merge 1 commit into
click-contrib:masterfrom
apoorvdarshan:fix/flag-value-completion-116
Open

Do not offer value completions for boolean flags (fixes #116)#136
apoorvdarshan wants to merge 1 commit into
click-contrib:masterfrom
apoorvdarshan:fix/flag-value-completion-116

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Summary

Fixes #116.

A click option declared with is_flag=True (a store_true-style flag) does not
consume a value. However, ClickCompleter matched such flags against
BoolParamType and offered true/false value completions once the flag had
been typed. Selecting one of those completions inserted the value into the
positional-argument slot, so the command then errored with an unexpected extra
argument. Flags declared with a secondary option (--foo/--no-foo) were affected
in the same way.

Fix

Value completions are produced in ClickCompleter._get_completion_from_params.
The fix gates that logic on the parameter not being a flag: if
param.is_flag is true (which also covers --foo/--no-foo secondary opts, since
click sets is_flag=True for those too), we return no value completions. This is
localized to the completer and leaves genuine boolean value options
(type=click.BOOL, no is_flag) untouched — they still complete true/false.

Tests

Added two regression tests in
tests/test_completion/test_common_tests/test_option_completion.py:

  • test_flag_option_offers_no_value — a command with a STRING argument plus an
    is_flag=True option; after the flag is typed, no true/false completions
    are offered (matching the minimal reproduction in the issue).
  • test_boolean_flag_with_secondary_opts_offers_no_value — a --shout/--no-shout
    flag offers no value completions for either opt.

Both tests fail on the pristine code and pass with the fix. The full suite
(pytest) passes (67 passed, 5 skipped) and flake8 click_repl tests is clean.
test_boolean_option (a real type=click.BOOL value option) still passes,
confirming non-flag boolean options are unaffected.

Disclosure: prepared with AI assistance; reviewed and verified locally.

A click option declared with is_flag=True (including --foo/--no-foo style
flags with secondary opts) does not consume a value. The completer was
matching such flags against BoolParamType and offering true/false
completions; selecting one inserted the value into the positional-argument
slot, causing the command to error.

Gate value completions in _get_completion_from_params on the param not
being a flag, and add completer regression tests.

Fixes click-contrib#116.
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.

Flags are treated as regular boolean values

1 participant