Show unrecognized arguments error using subparser#2029
Merged
Conversation
command_attribute has a truthy default value, and there are no callers that set a falsy value. Removing for simplicity.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2029 +/- ##
==========================================
- Coverage 73.27% 73.25% -0.02%
==========================================
Files 86 86
Lines 10748 10755 +7
Branches 2095 2096 +1
==========================================
+ Hits 7876 7879 +3
- Misses 2498 2502 +4
Partials 374 374 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
victorlin
commented
Jul 15, 2026
joverlee521
approved these changes
Jul 16, 2026
joverlee521
left a comment
Contributor
There was a problem hiding this comment.
Nice! I always thought it was weird that the error didn't point to the subcommand.
Standard argparse behavior always uses the top-level parser for unrecognized arguments, rendering generic error messages. Call parse_known_args() ourselves to access the unrecognized arguments, and make the subparser accessible as an attribute on the on the parsed arguments namespace to use it for errors. An alternative approach is to subclass _SubParsersAction, but this requires manually injecting the custom action class into every nested parser (curate, export, titers, etc.). The current approach cleanly supports nested parsers.
8f4f9de to
fc95052
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of proposed changes
This PR contains 1 prep commit + 1 main commit. Message from main commit:
Standard argparse behavior always uses the top-level parser for unrecognized arguments, rendering generic error messages.
Call parse_known_args() ourselves to access the unrecognized arguments, and make the subparser accessible as an attribute on the on the parsed arguments namespace to use it for errors.
An alternative approach is to subclass _SubParsersAction, but this requires manually injecting the custom action class into every nested parser (curate, export, titers, etc.). The current approach cleanly supports nested parsers.
Related issue(s)
Closes #2028
Checklist
Check if you need to add teststested manually. I don't think this needs any command-specific functional test as argparse output may change over time.