Skip to content

Fix argparse completion ignoring flags given as --flag=value - #1864

Closed
mokshupadhyay wants to merge 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/completer-flag-equals-value
Closed

mokshupadhyay wants to merge 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/completer-flag-equals-value

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

ArgparseCompleter ignores a flag that is written with its value attached, --flag=value. While walking the tokens it only compared a whole token with the known flag names, so --flag=value matched nothing: the flag was never marked as used, mutually exclusive flags were not excluded, and the hint shown for the next argument was the one for a position the flag's value had already filled. The space form (--flag value) worked.

Real results with the mutex command of the test app (-f/--flag and -o/--other_flag are mutually exclusive), on main and with this change (the "after" rows are the same as for --flag value):

input                              main                                          after
mutex --flag=val --               suggests --flag, --help, --other_flag          suggests --help
mutex --flag=val <TAB>            hint for the optional positional               hint for last_arg
mutex --flag=val --oth<TAB>       --other_flag                                    no matches
mutex --flag=val --other <TAB>    hint for --other_flag                           "-o/--other_flag: not allowed with argument -f/--flag"

The fix splits a token that is not a known flag name at the first = (the way argparse does), looks the flag up by the part before the = (also for an abbreviated flag such as --fl=val) and treats the part after it as the flag's argument, which finishes the flag. I did not handle a short flag with an attached value (-fval); it behaves as before.

Test: test_complete_flag_with_equals_value (four cases) in tests/test_argparse_completer.py; all four fail on main and pass now. The full suite passes (3235 passed, 5 skipped), and ruff check, ruff format --check and mypy cmd2/argparse_completer.py are clean. I did not run make check as a whole or make docs-test, and I did not edit any Markdown file (AGENTS.md asks to ask first).

ArgparseCompleter only recognized a flag when the whole token matched a flag
name, so --flag=value was skipped. It was never marked as used, mutually
exclusive group members were not excluded, and the next positional's hint
was wrong. Split the token at '=' the way argparse does and treat the
attached value as the flag's argument.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (041e2a8) to head (fa227e3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1864   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6783    +6     
=======================================
+ Hits         6761     6767    +6     
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt

Copy link
Copy Markdown
Member

Thanks for the PR, @mokshupadhyay, and for the clear reproducer. I confirmed the bug on main. After mutex --flag=val, completion still offered --other_flag even though the two flags are mutually exclusive. All four new regression cases failed against the base implementation and passed on this branch. The full suite passed locally as well (3,237 passed, 3 skipped).

I found two cases worth addressing before merge:

  1. An attached value containing a space still loses the flag. In the existing test app, type hint "--flag=two words" --fl and press Tab. Completion offers --flag again. With hint --flag "two words" --fl, the suggestion is correctly empty. Both spellings pass the same flag value to the command. The quote placement in the first example matters: cmd2 passes the whole quoted token to argparse. In ArgparseCompleter.complete, the whole token reaches _looks_like_flag before the new split, so the space sends it down the positional path. Could you resolve the option name before applying the space check and add this pair as a regression test?
  2. The action’s arity needs checking before marking it used. In the nargs test command, --set_value requires two values. After nargs --set_value=set --, Tab suggests more flags, while argparse rejects the attached form with expected 2 arguments even if another value follows. The converse happens with flag --normal_flag=x: the zero-argument action is marked used even though argparse rejects the explicit value. Could you check whether the action accepts exactly one value in the attached form before updating mutex/used-flag state, and return a completion error for these invalid inputs?

@tleonhardt

tleonhardt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

@mokshupadhyay On second thought after chatting with the co-maintainer, omitting = support was an intentional design decision for code simplicity. I doubt anyone is typing = while tab completing since that would be incredibly slow.

ArgparseCompleter was never meant to be a 1:1 argparse emulator. It was written to support how users typically type in a cmd2 prompt. cmd2 explicitly uses shlex-based parsing. I don't think we need to be further-tokenizing a list of parsed tokens.

Do you have a solid need to support this use case? If not, it might be best for us to just document that is intentionally not supported.

@mokshupadhyay

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review and for the context. No, I don't have a real need for this: I didn't hit it in a project, I found it by reading ArgparseCompleter and trying odd inputs against the test app, so I can't claim anyone types --flag=value while tab completing. Given that leaving it out was a design decision, and that supporting it needs more care than I first thought (your two cases: a quoted value with a space, and checking the action's arity before marking the flag used), I think it's best not to add the complexity. Please feel free to close this PR. If you would like it documented as not supported I'm happy to write that note instead, and I'll ask before touching any Markdown as AGENTS.md says. I won't push more code here unless you tell me to.

@tleonhardt

Copy link
Copy Markdown
Member

@mokshupadhyay Thanks for the context about how you encountered this.

I'll go ahead and close this without merging.

I'll look and find the right place to add something to the documentation.

@tleonhardt tleonhardt closed this Dec 3, 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.

2 participants