Repository navigation
Fix argparse completion ignoring flags given as --flag=value - #1864
mokshupadhyay wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
Thanks for the PR, @mokshupadhyay, and for the clear reproducer. I confirmed the bug on I found two cases worth addressing before merge:
|
|
@mokshupadhyay On second thought after chatting with the co-maintainer, omitting
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. |
|
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 |
|
@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. |
ArgparseCompleterignores 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=valuematched 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
mutexcommand of the test app (-f/--flagand-o/--other_flagare mutually exclusive), onmainand with this change (the "after" rows are the same as for--flag value):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) intests/test_argparse_completer.py; all four fail onmainand pass now. The full suite passes (3235 passed, 5 skipped), andruff check,ruff format --checkandmypy cmd2/argparse_completer.pyare clean. I did not runmake checkas a whole ormake docs-test, and I did not edit any Markdown file (AGENTS.md asks to ask first).