From 4d68880f20a644b7bf6af48cfdd53f7e4cc5a974 Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 16:04:51 +0900 Subject: [PATCH 1/8] Delete a redundant cleanup --- Lib/argparse.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 240625ff01084e..58401976c70d81 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -490,7 +490,6 @@ def _format_actions_usage(self, actions, groups): text = _re.sub(r'(%s) ' % open, r'\1', text) text = _re.sub(r' (%s)' % close, r'\1', text) text = _re.sub(r'%s *%s' % (open, close), r'', text) - text = _re.sub(r'\(([^|]*)\)', r'\1', text) text = text.strip() # return the text From 70ef4fcf84ea3a4b78eb0eb24a7b32719dd25ed6 Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 16:23:00 +0900 Subject: [PATCH 2/8] Add parenthesis only when there are more than two required actions --- Lib/argparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 58401976c70d81..8865f5bc7f2edd 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -416,7 +416,7 @@ def _format_actions_usage(self, actions, groups): inserts[end] += ']' else: inserts[end] = ']' - else: + elif len(group._group_actions) > 1: if start in inserts: inserts[start] += ' (' else: From da07d8c9972f125953b44efa4e7c144dd9fc0417 Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 17:28:38 +0900 Subject: [PATCH 3/8] Count only exposed actions --- Lib/argparse.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 8865f5bc7f2edd..c3f02b0704237e 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -405,8 +405,15 @@ def _format_actions_usage(self, actions, groups): else: end = start + len(group._group_actions) if actions[start:end] == group._group_actions: + + suppressed_actions_count = 0 for action in group._group_actions: group_actions.add(action) + if action.help is SUPPRESS: + suppressed_actions_count += 1 + + exposed_actions_count = len(group._group_actions) - suppressed_actions_count + if not group.required: if start in inserts: inserts[start] += ' [' @@ -416,7 +423,7 @@ def _format_actions_usage(self, actions, groups): inserts[end] += ']' else: inserts[end] = ']' - elif len(group._group_actions) > 1: + elif exposed_actions_count > 1: if start in inserts: inserts[start] += ' (' else: From 4c6889c23de1251078dfd21807e2bbc6b9d4520d Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 18:36:34 +0900 Subject: [PATCH 4/8] Add testcase --- Lib/test/test_argparse.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index cabb2f837693ff..4b8c402cef288f 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -3764,6 +3764,28 @@ class TestHelpUsage(HelpTestCase): version = '' +class TestHelpUsageWithParenthesis(HelpTestCase): + parser_signature = Sig(prog='PROG') + argument_signatures = [ + Sig('positional', metavar='(example) positional'), + Sig('-p', '--optional', metavar='{1 (option A), 2 (option B)}'), + ] + + usage = '''\ + usage: PROG [-h] [-p {1 (option A), 2 (option B)}] (example) positional + ''' + help = usage + '''\ + + positional arguments: + (example) positional + + options: + -h, --help show this help message and exit + -p {1 (option A), 2 (option B)}, --optional {1 (option A), 2 (option B)} + ''' + version = '' + + class TestHelpOnlyUserGroups(HelpTestCase): """Test basic usage messages""" From 0dc91c0f094495cd8e88cabfc4260f8f9b0c1ee3 Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 09:52:48 +0000 Subject: [PATCH 5/8] Add NEWS.d --- .../next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst diff --git a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst new file mode 100644 index 00000000000000..a2b0d0acd60867 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst @@ -0,0 +1,2 @@ +Fix a bug that parentheses in metavar param of :func:`add_argument()` were +dropped. Patch by Yeojin Kim. From 7f828633d2fa469ad6d0e955640b21341cc76cf6 Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Tue, 28 Feb 2023 19:07:19 +0900 Subject: [PATCH 6/8] Fix typo --- Lib/test/test_argparse.py | 2 +- .../next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 4b8c402cef288f..861da2326d1214 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -3764,7 +3764,7 @@ class TestHelpUsage(HelpTestCase): version = '' -class TestHelpUsageWithParenthesis(HelpTestCase): +class TestHelpUsageWithParentheses(HelpTestCase): parser_signature = Sig(prog='PROG') argument_signatures = [ Sig('positional', metavar='(example) positional'), diff --git a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst index a2b0d0acd60867..823e7111f09e66 100644 --- a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst +++ b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst @@ -1,2 +1,2 @@ -Fix a bug that parentheses in metavar param of :func:`add_argument()` were +Fix a bug that parentheses in metavar argument of :func:`add_argument()` were dropped. Patch by Yeojin Kim. From 74c14ceda6e40e555282696afed22b9eefe0418b Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Wed, 1 Mar 2023 23:31:53 +0900 Subject: [PATCH 7/8] Refactor code --- Lib/argparse.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index c3f02b0704237e..a819d2650e85f0 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -403,7 +403,8 @@ def _format_actions_usage(self, actions, groups): except ValueError: continue else: - end = start + len(group._group_actions) + group_action_count = len(group._group_actions) + end = start + group_action_count if actions[start:end] == group._group_actions: suppressed_actions_count = 0 @@ -412,7 +413,7 @@ def _format_actions_usage(self, actions, groups): if action.help is SUPPRESS: suppressed_actions_count += 1 - exposed_actions_count = len(group._group_actions) - suppressed_actions_count + exposed_actions_count = group_action_count - suppressed_actions_count if not group.required: if start in inserts: From b05fb160f95b4f4df03c010bda734e7e94bd4d3c Mon Sep 17 00:00:00 2001 From: Yeojin Kim Date: Sat, 4 Mar 2023 19:38:02 +0900 Subject: [PATCH 8/8] Update Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst Co-authored-by: Jelle Zijlstra --- .../next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst index 823e7111f09e66..1efe72439b3a4a 100644 --- a/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst +++ b/Misc/NEWS.d/next/Library/2023-02-28-09-52-25.gh-issue-101979.or3hXV.rst @@ -1,2 +1,2 @@ -Fix a bug that parentheses in metavar argument of :func:`add_argument()` were +Fix a bug where parentheses in the ``metavar`` argument to :meth:`argparse.ArgumentParser.add_argument` were dropped. Patch by Yeojin Kim.