-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-99735: Handle no arguments when using sub-commands in argparse #99736
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1852,10 +1852,13 @@ Sub-commands | |||||
>>> parser.parse_args(['co', 'bar']) | ||||||
Namespace(foo='bar') | ||||||
|
||||||
One particularly effective way of handling sub-commands is to combine the use | ||||||
of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so | ||||||
that each subparser knows which Python function it should execute. For | ||||||
example:: | ||||||
One particularly effective way of handling sub-commands is to | ||||||
combine the use of the :meth:`add_subparsers` method with calls to | ||||||
:meth:`set_defaults` so that each subparser knows which Python | ||||||
function it should execute. The :meth:`set_defaults` method of the | ||||||
main parser is called to handle the case when no subcommand is | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tiny nit, since 'sub-command' was used above in the same paragraph
Suggested change
|
||||||
included in the command line. The main parser :meth:`print_help` | ||||||
method is called to display the help in this case. For example:: | ||||||
|
||||||
>>> # sub-command functions | ||||||
>>> def foo(args): | ||||||
|
@@ -1866,6 +1869,7 @@ Sub-commands | |||||
... | ||||||
>>> # create the top-level parser | ||||||
>>> parser = argparse.ArgumentParser() | ||||||
>>> parser.set_defaults(func=lambda args: parser.print_help()) | ||||||
>>> subparsers = parser.add_subparsers() | ||||||
>>> | ||||||
>>> # create the parser for the "foo" command | ||||||
|
@@ -1888,6 +1892,17 @@ Sub-commands | |||||
>>> args = parser.parse_args('bar XYZYX'.split()) | ||||||
>>> args.func(args) | ||||||
((XYZYX)) | ||||||
>>> | ||||||
>>> # show the help when called without arguments | ||||||
>>> args = parser.parse_args() | ||||||
>>> args.func(args) | ||||||
usage: app.py [-h] {foo,bar} ... | ||||||
|
||||||
positional arguments: | ||||||
{foo,bar} | ||||||
|
||||||
options: | ||||||
-h, --help show this help message and exit | ||||||
|
||||||
This way, you can let :meth:`parse_args` do the job of calling the | ||||||
appropriate function after argument parsing is complete. Associating | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend keeping the original structure so just the new changes can be more easily reviewed.