@@ -176,13 +176,13 @@ def __init__(
176
176
width = shutil .get_terminal_size ().columns
177
177
width -= 2
178
178
179
- from _colorize import ANSIColors , NoColors , can_colorize ,decolor
179
+ from _colorize import can_colorize ,decolor , get_theme
180
180
181
181
if color and can_colorize ():
182
- self ._ansi = ANSIColors ()
182
+ self ._theme = get_theme ( force_color = True ). argparse
183
183
self ._decolor = decolor
184
184
else :
185
- self ._ansi = NoColors
185
+ self ._theme = get_theme ( force_no_color = True ). argparse
186
186
self ._decolor = lambda text :text
187
187
188
188
self ._prefix_chars = prefix_chars
@@ -237,14 +237,12 @@ def format_help(self):
237
237
238
238
# add the heading if the section was non-empty
239
239
if self .heading is not SUPPRESS and self .heading is not None :
240
- bold_blue = self .formatter ._ansi .BOLD_BLUE
241
- reset = self .formatter ._ansi .RESET
242
-
243
240
current_indent = self .formatter ._current_indent
244
241
heading_text = _ ('%(heading)s:' )% dict (heading = self .heading )
242
+ t = self .formatter ._theme
245
243
heading = (
246
244
f'{ " " * current_indent } '
247
- f'{ bold_blue } { heading_text } { reset } \n '
245
+ f'{ t . heading } { heading_text } { t . reset } \n '
248
246
)
249
247
else :
250
248
heading = ''
@@ -314,26 +312,23 @@ def _join_parts(self, part_strings):
314
312
if part and part is not SUPPRESS ])
315
313
316
314
def _format_usage (self ,usage ,actions ,groups ,prefix ):
317
- bold_blue = self ._ansi .BOLD_BLUE
318
- bold_magenta = self ._ansi .BOLD_MAGENTA
319
- magenta = self ._ansi .MAGENTA
320
- reset = self ._ansi .RESET
315
+ t = self ._theme
321
316
322
317
if prefix is None :
323
318
prefix = _ ('usage: ' )
324
319
325
320
# if usage is specified, use that
326
321
if usage is not None :
327
322
usage = (
328
- magenta
323
+ t . prog_extra
329
324
+ usage
330
- % {"prog" :f"{ bold_magenta } { self ._prog } { reset } { magenta } " }
331
- + reset
325
+ % {"prog" :f"{ t . prog } { self ._prog } { t . reset } { t . prog_extra } " }
326
+ + t . reset
332
327
)
333
328
334
329
# if no optionals or positionals are available, usage is just prog
335
330
elif usage is None and not actions :
336
- usage = f"{ bold_magenta } { self ._prog } { reset } "
331
+ usage = f"{ t . prog } { self ._prog } { t . reset } "
337
332
338
333
# if optionals and positionals are available, calculate usage
339
334
elif usage is None :
@@ -411,10 +406,10 @@ def get_lines(parts, indent, prefix=None):
411
406
usage = '\n ' .join (lines )
412
407
413
408
usage = usage .removeprefix (prog )
414
- usage = f"{ bold_magenta } { prog } { reset } { usage } "
409
+ usage = f"{ t . prog } { prog } { t . reset } { usage } "
415
410
416
411
# prefix with 'usage:'
417
- return f'{ bold_blue } { prefix } { reset } { usage } \n \n '
412
+ return f'{ t . usage } { prefix } { t . reset } { usage } \n \n '
418
413
419
414
def _format_actions_usage (self ,actions ,groups ):
420
415
return ' ' .join (self ._get_actions_usage_parts (actions ,groups ))
@@ -452,10 +447,7 @@ def _get_actions_usage_parts(self, actions, groups):
452
447
453
448
# collect all actions format strings
454
449
parts = []
455
- cyan = self ._ansi .CYAN
456
- green = self ._ansi .GREEN
457
- yellow = self ._ansi .YELLOW
458
- reset = self ._ansi .RESET
450
+ t = self ._theme
459
451
for action in actions :
460
452
461
453
# suppressed arguments are marked with None
@@ -465,7 +457,11 @@ def _get_actions_usage_parts(self, actions, groups):
465
457
# produce all arg strings
466
458
elif not action .option_strings :
467
459
default = self ._get_default_metavar_for_positional (action )
468
- part = green + self ._format_args (action ,default )+ reset
460
+ part = (
461
+ t .summary_short_option
462
+ + self ._format_args (action ,default )
463
+ + t .reset
464
+ )
469
465
470
466
# if it's in a group, strip the outer []
471
467
if action in group_actions :
@@ -481,20 +477,23 @@ def _get_actions_usage_parts(self, actions, groups):
481
477
if action .nargs == 0 :
482
478
part = action .format_usage ()
483
479
if self ._is_long_option (part ):
484
- part = f"{ cyan } { part } { reset } "
480
+ part = f"{ t . summary_long_option } { part } { t . reset } "
485
481
elif self ._is_short_option (part ):
486
- part = f"{ green } { part } { reset } "
482
+ part = f"{ t . summary_short_option } { part } { t . reset } "
487
483
488
484
# if the Optional takes a value, format is:
489
485
# -s ARGS or --long ARGS
490
486
else :
491
487
default = self ._get_default_metavar_for_optional (action )
492
488
args_string = self ._format_args (action ,default )
493
489
if self ._is_long_option (option_string ):
494
- option_string = f" { cyan } { option_string } "
490
+ option_color = t . summary_long_option
495
491
elif self ._is_short_option (option_string ):
496
- option_string = f"{ green } { option_string } "
497
- part = f"{ option_string } { yellow } { args_string } { reset } "
492
+ option_color = t .summary_short_option
493
+ part = (
494
+ f"{ option_color } { option_string } "
495
+ f"{ t .summary_label } { args_string } { t .reset } "
496
+ )
498
497
499
498
# make it look optional if it's not required or in a group
500
499
if not action .required and action not in group_actions :
@@ -590,17 +589,14 @@ def _format_action(self, action):
590
589
return self ._join_parts (parts )
591
590
592
591
def _format_action_invocation (self ,action ):
593
- bold_green = self ._ansi .BOLD_GREEN
594
- bold_cyan = self ._ansi .BOLD_CYAN
595
- bold_yellow = self ._ansi .BOLD_YELLOW
596
- reset = self ._ansi .RESET
592
+ t = self ._theme
597
593
598
594
if not action .option_strings :
599
595
default = self ._get_default_metavar_for_positional (action )
600
596
return (
601
- bold_green
597
+ t . action
602
598
+ ' ' .join (self ._metavar_formatter (action ,default )(1 ))
603
- + reset
599
+ + t . reset
604
600
)
605
601
606
602
else :
@@ -609,9 +605,9 @@ def color_option_strings(strings):
609
605
parts = []
610
606
for s in strings :
611
607
if self ._is_long_option (s ):
612
- parts .append (f"{ bold_cyan } { s } { reset } " )
608
+ parts .append (f"{ t . long_option } { s } { t . reset } " )
613
609
elif self ._is_short_option (s ):
614
- parts .append (f"{ bold_green } { s } { reset } " )
610
+ parts .append (f"{ t . short_option } { s } { t . reset } " )
615
611
else :
616
612
parts .append (s )
617
613
return parts
@@ -628,7 +624,7 @@ def color_option_strings(strings):
628
624
default = self ._get_default_metavar_for_optional (action )
629
625
option_strings = color_option_strings (action .option_strings )
630
626
args_string = (
631
- f"{ bold_yellow } { self ._format_args (action ,default )} { reset } "
627
+ f"{ t . label } { self ._format_args (action ,default )} { t . reset } "
632
628
)
633
629
return ', ' .join (option_strings )+ ' ' + args_string
634
630