10000 gh-133653: Fix argparse.ArgumentParser with the formatter_class argument by serhiy-storchaka · Pull Request #133813 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133653: Fix argparse.ArgumentParser with the formatter_class argument #133813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove the prefix_chars parameter of HelpFormatter.
  • Loading branch information
serhiy-storchaka committed May 10, 2025
commit 049676d48b3e6766ca2f2d91eecb843679a4616c
29 changes: 7 additions & 22 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def __init__(
indent_increment=2,
max_help_position=24,
width=None,
prefix_chars='-',
color=False,
):
# default setting for width
Expand All @@ -177,7 +176,6 @@ def __init__(
width -= 2

self._set_color(color)
self._prefix_chars = prefix_chars
self._prog = prog
self._indent_increment = indent_increment
self._max_help_position = min(max_help_position,
Expand Down Expand Up @@ -417,14 +415,7 @@ def _format_actions_usage(self, actions, groups):
return ' '.join(self._get_actions_usage_parts(actions, groups))

def _is_long_option(self, string):
return len(string) >= 2 and string[1] in self._prefix_chars

def _is_short_option(self, string):
return (
not self._is_long_option(string)
and len(string) >= 1
and string[0] in self._prefix_chars
)
return len(string) > 2

def _get_actions_usage_parts(self, actions, groups):
# find group indices and identify actions in groups
Expand Down Expand Up @@ -473,25 +464,22 @@ def _get_actions_usage_parts(self, actions, groups):
# produce the first way to invoke the option in brackets
else:
option_string = action.option_strings[0]
if self._is_long_option(option_string):
option_color = t.summary_long_option
else:
option_color = t.summary_short_option

# if the Optional doesn't take a value, format is:
# -s or --long
if action.nargs == 0:
part = action.format_usage()
if self._is_long_option(part):
part = f"{t.summary_long_option}{part}{t.reset}"
elif self._is_short_option(part):
part = f"{t.summary_short_option}{part}{t.reset}"
part = f"{option_color}{part}{t.reset}"

# if the Optional takes a value, format is:
# -s ARGS or --long ARGS
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
if self._is_long_option(option_string):
option_color = t.summary_long_option
elif self._is_short_option(option_string):
option_color = t.summary_short_option
part = (
f"{option_color}{option_string} "
f"{t.summary_label}{args_string}{t.reset}"
Expand Down Expand Up @@ -608,10 +596,8 @@ def color_option_strings(strings):
for s in strings:
if self._is_long_option(s):
parts.append(f"{t.long_option}{s}{t.reset}")
elif self._is_short_option(s):
parts.append(f"{t.short_option}{s}{t.reset}")
else:
parts.append(s)
parts.append(f"{t.short_option}{s}{t.reset}")
return parts

# if the Optional doesn't take a value, format is:
Expand Down Expand Up @@ -2726,7 +2712,6 @@ def format_help(self):

def _get_formatter(self):
formatter = self.formatter_class(prog=self.prog)
formatter._prefix_chars = self.prefix_chars
formatter._set_color(self.color)
return formatter

Expand Down
Loading
0