Skip to content

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
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +1855 to +1858
Copy link
Contributor

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.

Suggested change
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
main parser is called to handle the case when no subcommand is
main parser is called to handle the case when no sub-command is

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):
Expand All @@ -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
Expand All @@ -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
Expand Down