Skip to content

Commit dcdf93d

Browse files
fix: Updated the Argparse module to be usable with python 3.12.3. #2
Version 3.10.1
1 parent 5e13bcd commit dcdf93d

3 files changed

Lines changed: 49 additions & 14 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies = [
2626
"webcolors",
2727
"docstring-parser"
2828
]
29-
version = "2.10.0"
29+
version = "2.10.1"
3030

3131
[tool.setuptools.packages.find]
3232
where = ["src"]

src/log21/Argparse.py

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,28 @@ def _format_actions_usage(self, actions: list, groups):
280280
group_actions = set()
281281
inserts = {}
282282
for group in groups:
283+
if not group._group_actions:
284+
raise ValueError(f'empty group {group}')
285+
283286
try:
284287
start = actions.index(group._group_actions[0])
285288
except ValueError:
286289
continue
287290
else:
288-
end = start + len(group._group_actions)
291+
group_action_count = len(group._group_actions)
292+
end = start + group_action_count
289293
if actions[start:end] == group._group_actions:
294+
295+
suppressed_actions_count = 0
290296
for action in group._group_actions:
291297
group_actions.add(action)
298+
if action.help is _argparse.SUPPRESS:
299+
suppressed_actions_count += 1
300+
301+
exposed_actions_count = group_action_count - suppressed_actions_count
302+
if not exposed_actions_count:
303+
continue
304+
292305
if not group.required:
293306
if start in inserts:
294307
inserts[start] += ' ['
@@ -298,7 +311,7 @@ def _format_actions_usage(self, actions: list, groups):
298311
inserts[end] += ']'
299312
else:
300313
inserts[end] = ']'
301-
else:
314+
elif exposed_actions_count > 1:
302315
if start in inserts:
303316
inserts[start] += ' ('
304317
else:
@@ -941,7 +954,22 @@ def consume_optional(start_index):
941954

942955
# get the optional identified at this index
943956
option_tuple = option_string_indices[start_index]
944-
action, option_string, explicit_arg = option_tuple
957+
if len(option_tuple) == 3:
958+
action, option_string, explicit_arg = option_tuple
959+
sep = None
960+
elif len(option_tuple) == 4:
961+
action, option_string, sep, explicit_arg = option_tuple
962+
else:
963+
# Tell the user that there seem to have been a change in argparse module
964+
# and if they see this error they should immediately report it in an
965+
# issue at GitHub.com/MPCodeWriter21/log21 with their Python version
966+
raise ValueError(
967+
'Unknown option tuple length, please report this issue at: '
968+
'https://GitHub.com/MPCodeWriter21/log21\n'
969+
f'Python version: {_sys.version}'
970+
f'Option tuple: {option_tuple}'
971+
f'log21 version: {_log21.__version__}'
972+
)
945973

946974
# identify additional optionals in the same arg string
947975
# (e.g. -xyz is the same as -x -y -z if no args are required)
@@ -964,20 +992,27 @@ def consume_optional(start_index):
964992
# of the tail of the option string
965993
chars = self.prefix_chars
966994
if arg_count == 0 and option_string[1] not in chars:
995+
if sep or explicit_arg[0] in chars:
996+
msg = _gettext('ignored explicit argument %r')
997+
raise _argparse.ArgumentError(action, msg % explicit_arg)
967998
action_tuples.append((action, [], option_string))
968999
char = option_string[0]
9691000
option_string = char + explicit_arg[0]
970-
new_explicit_arg = explicit_arg[1:] or None
9711001
optionals_map = self._option_string_actions
9721002
if option_string in optionals_map:
9731003
action = optionals_map[option_string]
974-
explicit_arg = new_explicit_arg
1004+
explicit_arg = explicit_arg[1:]
1005+
if not explicit_arg:
1006+
sep = explicit_arg = None
1007+
elif explicit_arg[0] == '=':
1008+
sep = '='
1009+
explicit_arg = explicit_arg[1:]
1010+
else:
1011+
sep = ''
9751012
else:
976-
msg = _gettext(
977-
f'ignored explicit argument {explicit_arg!r}'
978-
)
979-
raise _argparse.ArgumentError(action, msg)
980-
1013+
extras.append(char + explicit_arg)
1014+
stop = start_index + 1
1015+
break
9811016
# if the action expect exactly one argument, we've
9821017
# successfully matched the option; exit the loop
9831018
elif arg_count == 1:
@@ -989,8 +1024,8 @@ def consume_optional(start_index):
9891024
# error if a double-dash option did not use the
9901025
# explicit argument
9911026
else:
992-
msg = _gettext(f'ignored explicit argument {explicit_arg!r}')
993-
raise _argparse.ArgumentError(action, msg)
1027+
msg = _gettext('ignored explicit argument %r')
1028+
raise _argparse.ArgumentError(action, msg % explicit_arg)
9941029

9951030
# if there is no explicit argument, try to match the
9961031
# optional's string arguments with the following strings

src/log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .StreamHandler import StreamHandler, ColorizingStreamHandler
2525

2626
__author__ = 'CodeWriter21 (Mehrad Pooryoussof)'
27-
__version__ = '2.10.0'
27+
__version__ = '2.10.1'
2828
__github__ = 'Https://GitHub.com/MPCodeWriter21/log21'
2929
__all__ = [
3030
'ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter',

0 commit comments

Comments
 (0)