-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argparse fails with required subparsers, un-named dest, and empty argv #73484
Comments
In python 3.6 (and several versions back), using argparse with required subparsers will cause an unhelpful TypeError if the 'dest' parameter is not explicitly specified, and no arguments are provided. Test case: Observed result: If the line above is changed to: Then the following is printed to stderr: This issue goes back at least several years: Though it seems odd to not specify a dest in the add_subparsers line, the pattern is not completely useless. The below works fine without setting a 'dest' in add_subparsers, except when argv is empty: However, an empty argv produces the unexpected TypeError above. I'm not sure if argparse should provide a more useful exception in this case, or if there is a clean way to do the right thing without a dest specified. |
http://bugs.python.org/issue9253 argparse: optional subparsers Initially this bug/issue was a request to allow subparsers to be optional. But with the change in how required actions are handled, subparsers are now optional by default. As you learned from the SO question you now have to specify subparsers.required = True This is also discussed in my post (and following ones) http://bugs.python.org/issue9253#msg186387 The default 'dest' is SUPPRESS. The error you report occurs because the 'required' error mechanism cannot handle that value. The suggest fix is to assign Reviewing my suggested patches, it looks like I generate a 'dest' substitute from the subparser names. So the 'required' error would look like python: error: the following arguments are required: {cmd1, cmd2} I think this issue can be closed with a reference to 9253. Or maybe that issue is too old, long and confusing, and we need a new bug/issue. |
I was just hit by the very same issue and added the following test into
I checked bpo-9253 as referenced by paul j3 and like the Any chance this can be addressed one way or another? |
while waiting for a fix, would it be possible to document in the argparse documentation that the 'dest' parameter is required (at least temporarily) for add_subparsers()? (somewhere near file:///usr/share/doc/python/html/library/argparse.html#sub-commands) gratuitous diff: the pull request from 2017 would probably fix it. my diffs are here (from: Python 3.8.0 (default, Oct 23 2019, 18:51:26). (the pull request changes the utility '_get_action_name'; i wasn't sure of side-effects with other callers, so changed nearer the failure location.) *** 2017,2030 **** for action in self._actions:
if action not in seen_actions:
if action.required:
! required_actions.append(_get_action_name(action))
else:
# Convert action default now instead of doing it before
# parsing arguments to avoid calling convert functions |
crash means segfault, not unhandled exception. |
I'd like to second this idea, since it's very confusing without it:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: