From 3e645287dc5b990160e6408c15a7823ae3bbc63e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 9 Oct 2024 23:26:29 +0300 Subject: [PATCH 1/2] gh-59330: Improve error message for dest= for positionals Also improve the documentation. Specify how dest and metavar are derived from add_argument() positional arguments. Co-authored-by: Simon Law --- Doc/library/argparse.rst | 16 ++++++++++++++++ Lib/argparse.py | 3 ++- Lib/test/test_argparse.py | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index e9a08984f77c3a..e9ffdd696042dc 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -635,6 +635,22 @@ be positional:: usage: PROG [-h] [-f FOO] bar PROG: error: the following arguments are required: bar +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:: + + >>> 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: diff --git a/Lib/argparse.py b/Lib/argparse.py index 21299b69ecd74c..b6c557e7391c94 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -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 diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 1bf812b36fc2c6..1d9cf031956d17 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -5338,7 +5338,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): From 36c5c200ac6917d553b13c4ab680be7689600e0b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 11 Oct 2024 11:38:21 +0300 Subject: [PATCH 2/2] Apply suggestions from review. --- Doc/library/argparse.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index e9ffdd696042dc..6c72ad941d151c 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -635,8 +635,11 @@ be positional:: usage: PROG [-h] [-f FOO] bar PROG: error: the following arguments are required: bar -You do not need to specify the dest_ and metavar_ parameters. The -dest_ parameter defaults to the argument name with underscores ``_`` +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::