Skip to content
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

gh-59330: Improve error message for dest= for positionals #125215

Merged
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,25 @@ be positional::
usage: PROG [-h] [-f FOO] bar
PROG: error: the following arguments are required: bar

By default, argparse automatically handles the internal naming and
display names of arguments, simplifying the process without requiring
additional configuration.
As such, you do not need to specify the dest_ and metavar_ parameters.
The dest_ parameter defaults to the argument name with underscores ``_``
replacing hyphens ``-`` . The metavar_ parameter defaults to the
upper-cased name. For example::
Comment on lines +639 to +645
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, argparse automatically handles the internal naming and
display names of arguments, simplifying the process without requiring
additional configuration.
As such, you do not need to specify the dest_ and metavar_ parameters.
The dest_ parameter defaults to the argument name with underscores ``_``
replacing hyphens ``-`` . The metavar_ parameter defaults to the
upper-cased name. For example::
By default, argparse automatically handles the internal naming and
display names of arguments, simplifying the process without requiring
additional configuration. As such, you do not need to specify the dest_
and metavar_ parameters. The dest_ parameter defaults to the argument
name with underscores ``_`` replacing hyphens ``-`` . The metavar_
parameter defaults to the upper-cased name. For example::

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removing the newline as I think that this can be one short paragraph.

Copy link
Member Author

@serhiy-storchaka serhiy-storchaka Oct 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not affect the result. For Sphinx this is the same.

I prefer to start new sentence from a new line. This will help to keep the text more readable in future without reformatting the whole paragraph. Adding or removing will only affect one sentence.

I don't know if this is already in the style guide, but I try to follow this rule.


>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo-bar')
>>> parser.parse_args(['--foo-bar', 'FOO-BAR']
Namespace(foo_bar='FOO-BAR')
>>> parser.print_help()
usage: [-h] [--foo-bar FOO-BAR]

optional arguments:
-h, --help show this help message and exit
--foo-bar FOO-BAR


.. _action:

Expand Down
3 changes: 2 additions & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,8 @@ def add_argument(self, *args, **kwargs):
chars = self.prefix_chars
if not args or len(args) == 1 and args[0][0] not in chars:
if args and 'dest' in kwargs:
raise ValueError('dest supplied twice for positional argument')
raise ValueError('dest supplied twice for positional argument,'
' did you mean metavar?')
kwargs = self._get_positional_kwargs(*args, **kwargs)

# otherwise, we're adding an optional argument
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5380,7 +5380,8 @@ def test_multiple_dest(self):
parser.add_argument(dest='foo')
with self.assertRaises(ValueError) as cm:
parser.add_argument('bar', dest='baz')
self.assertIn('dest supplied twice for positional argument',
self.assertIn('dest supplied twice for positional argument,'
' did you mean metavar?',
str(cm.exception))

def test_no_argument_actions(self):
Expand Down
Loading