From 732cfbb0607a558004a58f738ffb55c8cf663403 Mon Sep 17 00:00:00 2001 From: 180909 <734461790@qq.com> Date: Tue, 17 Oct 2023 14:06:55 +0800 Subject: [PATCH] gho-109475: argument get the value is `--` --- Lib/argparse.py | 3 +++ Lib/test/test_argparse.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Lib/argparse.py b/Lib/argparse.py index a32884db80d1ea..2059ec880c6a2d 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2536,6 +2536,9 @@ def _get_values(self, action, arg_strings): elif action.nargs == SUPPRESS: value = SUPPRESS + elif len(arg_strings) == 0: + value = '--' + # all other types of nargs produce a list else: value = [self._get_value(action, v) for v in arg_strings] diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 7c1f5d36999a3d..295fda5dfba210 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -5405,6 +5405,12 @@ def test_zero_or_more_optional(self): args = parser.parse_args([]) self.assertEqual(NS(x=[]), args) + def test_double_dash(self): + parser = argparse.ArgumentParser() + parser.add_argument('--foo') + args = parser.parse_args(['--foo=--']) + self.assertEqual(NS(foo='--'), args) + # =========================== # parse_intermixed_args tests