Repository navigation
Handle no arguments when using sub-commands in argparse #99735
Copy link
Copy link
Closed
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Description
Activity
- changed the title
[-]Handle no arguments when using sub-commands[/-][+]Handle no arguments when using sub-commands in argparse[/+]on Nov 23, 2022 Originally
subparserswere required, so this issue did not come up. Because of a patching oversight, they are now optional, and we had to add therequiredparameter. I think this example would be better handled with asubparsers = parser.add_subparsers(required=True)Then we wouldn't have to define a
funcfor the no-subparser case. Your patch works, but I think it adds too much to the text and example, detracting from the essence of whatset_defaultsadds.Whether we use
set_defaultsor not, the Namespace will have different attributes depending on what subparser (or none) the user invokes. The post parsing code has to handle that issue in one way or the other.- added 4 commits that reference this issue
on Feb 19, 2023 - added a commit that references this issue
on Feb 20, 2023 Can we close this issue?
- addedpendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
on Feb 21, 2023 Yes we can! The backports had a hiccup, but they're all done now
Reacted by Erlend E. Aasland- removedpendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
on Feb 21, 2023 - added a commit that references this issue
on Feb 22, 2023
Metadata
Metadata
Assignees
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir
Projects
- StatusShow more project fieldsDone
Documentation
The example that shows the use of sub-commands and set_defaults to dispatch a function for each sub-command, fails when there are no command line arguments:
AttributeError: 'Namespace' object has no attribute 'func'A pull request is provided that adds one line to handle that case and an explanation in the documentation.
Linked PRs