GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-26510: Allow argparse add_subparsers to take `required` kwarg. by asottile · Pull Request #3027 · python/cpython

bpo-26510: Allow argparse add_subparsers to take required kwarg. #3027

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.

Sign up for GitHub

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

Merged
merwok merged 6 commits into python:master from asottile:issue26510-argparse_subparsers_required
Sep 20, 2017

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-26510: Allow argparse add_subparsers to take required kwarg. #3027

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
46282df
Allow argparse add_subparsers to take `required` kwarg.
asottile Aug 8, 2017
dede844
Subparsers are required by default.
asottile Aug 8, 2017
bb9b7e7
Add news and update docs.
asottile Sep 19, 2017
28bfe64
Minor tweaks
merwok Sep 20, 2017
b3cbad6
rewrite news
merwok Sep 20, 2017
fe5feb0
Make explicit that there is a new param
merwok Sep 20, 2017
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 5 additions & 2 deletions 7 Doc/library/argparse.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1539,8 +1539,8 @@ Sub-commands

.. method:: ArgumentParser.add_subparsers([title], [description], [prog], \
[parser_class], [action], \
[option_string], [dest], [help], \
[metavar])
[option_string], [dest], [required] \
[help], [metavar])

Many programs split up their functionality into a number of sub-commands,
for example, the ``svn`` program can invoke sub-commands like ``svn
Expand Down Expand Up @@ -1576,6 +1576,9 @@ Sub-commands
* dest_ - name of the attribute under which sub-command name will be
stored; by default ``None`` and no value is stored

* required_ - Whether or not a subcommand must be provided, by default
``True``.

* help_ - help for sub-parser group in help output, by default ``None``

* metavar_ - string presenting available sub-commands in help; by default it
Expand Down
2 changes: 2 additions & 0 deletions 2 Lib/argparse.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ def __init__(self,
prog,
parser_class,
dest=SUPPRESS,
required=True,
help=None,
metavar=None):
Comment thread Outdated

@merwok merwok Sep 15, 2017

Copy link
Copy Markdown
Member

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.

For maximum backward compatibility, the new parameter should be added to the end.

(I don’t personnally write calls like something('abc', SUPPRESS, True, None, None) but they are supported, and should not be broken when possible.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile Sep 15, 2017

Copy link
Copy Markdown
Contributor Author

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.

add_subparsers itself takes no keyword arguments so I don't think there's a concern of the private api here.

>>> parser.add_subparsers('foo', True, False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add_subparsers() takes 1 positional argument but 4 were given

(the 1 positional argument here being self)

The ordering I chose was intentional to match the signatures in the rest of the file.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Sep 15, 2017

Copy link
Copy Markdown
Member

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.

Perfect!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile Sep 17, 2017

Copy link
Copy Markdown
Contributor Author

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.

@merwok do I monty-python it up now? I'm unclear on the process for dismissing a review :P

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Sep 18, 2017

Copy link
Copy Markdown
Member

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.

The review holding this up is «please update docs and news» :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@asottile asottile Sep 18, 2017

Copy link
Copy Markdown
Contributor Author

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.

whoops totally missed that! I'll get right on it :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

merwok reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Expand All @@ -1079,6 +1080,7 @@ def __init__(self,
dest=dest,
nargs=PARSER,
choices=self._name_parser_map,
required=required,
help=help,
metavar=metavar)

Expand Down
37 changes: 36 additions & 1 deletion 37 Lib/test/test_argparse.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
'bar', type=float, help='bar help')

# check that only one subparsers argument can be added
subparsers_kwargs = {}
subparsers_kwargs = {'required': False}
if aliases:
subparsers_kwargs['metavar'] = 'COMMAND'
subparsers_kwargs['title'] = 'commands'
Expand Down Expand Up @@ -1907,6 +1907,41 @@ def test_dest(self):
self.assertEqual(NS(foo=False, bar='1', baz='2'),
parser.parse_args('1 2'.split()))

def _test_required_subparsers(self, parser):
# Should parse the sub command
ret = parser.parse_args(['run'])
self.assertEqual(ret.command, 'run')

# Error when the command is missing
self.assertArgumentParserError(parser.parse_args, ())

def test_required_subparsers_via_attribute(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command')
subparsers.required = True
subparsers.add_parser('run')
self._test_required_subparsers(parser)

def test_required_subparsers_via_kwarg(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command', required=True)
subparsers.add_parser('run')
self._test_required_subparsers(parser)

def test_required_subparsers_default(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('run')
self._test_required_subparsers(parser)

def test_optional_subparsers(self):
parser = ErrorRaisingArgumentParser()
subparsers = parser.add_subparsers(dest='command', required=False)
subparsers.add_parser('run')
# No error here
ret = parser.parse_args(())
self.assertIsNone(ret.command)

def test_help(self):
self.assertEqual(self.parser.format_usage(),
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/Library/2017-09-19-13-29-29.bpo-26510.oncW6V.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
argparse subparsers are now required by default. This matches behaviour in Python 2.
For optional subparsers, use the new parameter ``add_subparsers(required=False)``.
Patch by Anthony Sottile.
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.