Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Argparse(ThemeSection):
label: str = ANSIColors.BOLD_YELLOW
action: str = ANSIColors.BOLD_GREEN
default: str = ANSIColors.GREY
default_value: str = ANSIColors.YELLOW
interpolated_value: str = ANSIColors.YELLOW
reset: str = ANSIColors.RESET
error: str = ANSIColors.BOLD_MAGENTA
warning: str = ANSIColors.BOLD_YELLOW
Expand Down
8 changes: 6 additions & 2 deletions Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ def _expand_help(self, action):
params[name] = value.__name__
if params.get('choices') is not None:
params['choices'] = ', '.join(map(str, params['choices']))
# Before interpolating, wrap the values with color codes
t = self._theme
for name, value in params.items():
params[name] = f"{t.interpolated_value}{value}{t.reset}"
return help_string % params

def _iter_indented_subactions(self, action):
Expand Down Expand Up @@ -752,8 +756,8 @@ def _get_help_string(self, action):
default_str = _(" (default: %(default)s)")
prefix, suffix = default_str.split("%(default)s")
help += (
f" {t.default}{prefix.lstrip()}"
f"{t.default_value}%(default)s"
f" {t.default}{prefix.lstrip()}{t.reset}"
f"%(default)s"
f"{t.default}{suffix}{t.reset}"
)
return help
Expand Down
28 changes: 18 additions & 10 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7263,6 +7263,13 @@ def test_argparse_color(self):
choices=("Aaaaa", "Bbbbb", "Ccccc", "Ddddd"),
help="pick one",
)
parser.add_argument(
"--optional8",
default="A",
metavar="X",
choices=("A", "B", "C"),
help="among %(choices)s, default is %(default)s",
)

parser.add_argument("+f")
parser.add_argument("++bar")
Expand All @@ -7289,7 +7296,7 @@ def test_argparse_color(self):
label_b = self.theme.label
pos_b = self.theme.action
default = self.theme.default
default_value = self.theme.default_value
interp = self.theme.interpolated_value
reset = self.theme.reset

# Act
Expand All @@ -7302,8 +7309,8 @@ def test_argparse_color(self):
f"""\
{heading}usage: {reset}{prog}PROG{reset} [{short}-h{reset}] [{short}-v{reset} | {short}-q{reset}] [{short}-o{reset}] [{long}--optional2 {label}OPTIONAL2{reset}] [{long}--optional3 {label}{{X,Y,Z}}{reset}]
[{long}--optional4 {label}{{X,Y,Z}}{reset}] [{long}--optional5 {label}{{X,Y,Z}}{reset}] [{long}--optional6 {label}{{X,Y,Z}}{reset}]
[{short}-p {label}{{Aaaaa,Bbbbb,Ccccc,Ddddd}}{reset}] [{short}+f {label}F{reset}] [{long}++bar {label}BAR{reset}] [{long}-+baz {label}BAZ{reset}]
[{short}-c {label}COUNT{reset}]
[{short}-p {label}{{Aaaaa,Bbbbb,Ccccc,Ddddd}}{reset}] [{long}--optional8 {label}X{reset}] [{short}+f {label}F{reset}] [{long}++bar {label}BAR{reset}]
[{long}-+baz {label}BAZ{reset}] [{short}-c {label}COUNT{reset}]
{pos}x{reset} {pos}y{reset} {pos}this_indeed_is_a_very_long_action_name{reset} {pos}{{sub1,sub2}} ...{reset}

Colorful help
Expand All @@ -7316,17 +7323,18 @@ def test_argparse_color(self):

{heading}options:{reset}
{short_b}-h{reset}, {long_b}--help{reset} show this help message and exit
{short_b}-v{reset}, {long_b}--verbose{reset} more spam {default}(default: {default_value}False{default}){reset}
{short_b}-q{reset}, {long_b}--quiet{reset} less spam {default}(default: {default_value}False{default}){reset}
{short_b}-v{reset}, {long_b}--verbose{reset} more spam {default}(default: {reset}{interp}False{reset}{default}){reset}
{short_b}-q{reset}, {long_b}--quiet{reset} less spam {default}(default: {reset}{interp}False{reset}{default}){reset}
{short_b}-o{reset}, {long_b}--optional1{reset}
{long_b}--optional2{reset} {label_b}OPTIONAL2{reset}
pick one {default}(default: {default_value}None{default}){reset}
pick one {default}(default: {reset}{interp}None{reset}{default}){reset}
{long_b}--optional3{reset} {label_b}{{X,Y,Z}}{reset}
{long_b}--optional4{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {default_value}None{default}){reset}
{long_b}--optional5{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {default_value}None{default}){reset}
{long_b}--optional6{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {default_value}None{default}){reset}
{long_b}--optional4{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {reset}{interp}None{reset}{default}){reset}
{long_b}--optional5{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {reset}{interp}None{reset}{default}){reset}
{long_b}--optional6{reset} {label_b}{{X,Y,Z}}{reset} pick one {default}(default: {reset}{interp}None{reset}{default}){reset}
{short_b}-p{reset}, {long_b}--optional7{reset} {label_b}{{Aaaaa,Bbbbb,Ccccc,Ddddd}}{reset}
pick one {default}(default: {default_value}None{default}){reset}
pick one {default}(default: {reset}{interp}None{reset}{default}){reset}
{long_b}--optional8{reset} {label_b}X{reset} among {interp}A, B, C{reset}, default is {interp}A{reset}
{short_b}+f{reset} {label_b}F{reset}
{long_b}++bar{reset} {label_b}BAR{reset}
{long_b}-+baz{reset} {label_b}BAZ{reset}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add colour to all interpolated values in :mod:`argparse` help, like ``%(default)s`` or ``%(choices)s``. Patch by Alex Prengère.
Loading