From 3c5f6a5b8ee6b802f3ff5439829cf2b612382ab4 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Sun, 2 Apr 2023 23:42:58 +0300 Subject: [PATCH 1/2] Fix ArgumentParser inconsistent with parse_known_args --- Lib/argparse.py | 5 ++--- Lib/test/test_argparse.py | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index a819d2650e85f0..cde7cc5009251c 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2018,9 +2018,8 @@ def consume_optional(start_index): action = optionals_map[option_string] explicit_arg = new_explicit_arg else: - msg = _('ignored explicit argument %r') - raise ArgumentError(action, msg % explicit_arg) - + extras.append(option_string) + explicit_arg = new_explicit_arg # if the action expect exactly one argument, we've # successfully matched the option; exit the loop elif arg_count == 1: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 861da2326d1214..2856be3d03418f 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2136,6 +2136,13 @@ def test_parse_known_args(self): (NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']), ) + def test_parse_known_args_with_single_dash_option(self): + parser = argparse.ArgumentParser() + parser.add_argument('-k', '--known', action='store_true') + self.assertEqual(parser.parse_known_args(['-k', '-u']), (NS(known=True), ['-u'])) + self.assertEqual(parser.parse_known_args(['-ku']), (NS(known=True), ['-u'])) + self.assertEqual(parser.parse_known_args(['-uk']), (NS(known=False), ['-uk'])) + def test_dest(self): parser = ErrorRaisingArgumentParser() parser.add_argument('--foo', action='store_true') From 04d7adc0ac217f4331feeb1d3fed5aea5c27fab0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 2 Apr 2023 21:20:39 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-04-02-21-20-35.gh-issue-60346.7mjgua.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-04-02-21-20-35.gh-issue-60346.7mjgua.rst diff --git a/Misc/NEWS.d/next/Library/2023-04-02-21-20-35.gh-issue-60346.7mjgua.rst b/Misc/NEWS.d/next/Library/2023-04-02-21-20-35.gh-issue-60346.7mjgua.rst new file mode 100644 index 00000000000000..c15bd6ed11d17f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-04-02-21-20-35.gh-issue-60346.7mjgua.rst @@ -0,0 +1 @@ +Fix ArgumentParser inconsistent with parse_known_args.