@@ -167,7 +167,6 @@ def __init__(
167167 indent_increment = 2 ,
168168 max_help_position = 24 ,
169169 width = None ,
170- prefix_chars = '-' ,
171170 color = False ,
172171 ):
173172 # default setting for width
@@ -176,16 +175,7 @@ def __init__(
176175 width = shutil .get_terminal_size ().columns
177176 width -= 2
178177
179- from _colorize import can_colorize , decolor , get_theme
180-
181- if color and can_colorize ():
182- self ._theme = get_theme (force_color = True ).argparse
183- self ._decolor = decolor
184- else :
185- self ._theme = get_theme (force_no_color = True ).argparse
186- self ._decolor = lambda text : text
187-
188- self ._prefix_chars = prefix_chars
178+ self ._set_color (color )
189179 self ._prog = prog
190180 self ._indent_increment = indent_increment
191181 self ._max_help_position = min (max_help_position ,
@@ -202,6 +192,16 @@ def __init__(
202192 self ._whitespace_matcher = _re .compile (r'\s+' , _re .ASCII )
203193 self ._long_break_matcher = _re .compile (r'\n\n\n+' )
204194
195+ def _set_color (self , color ):
196+ from _colorize import can_colorize , decolor , get_theme
197+
198+ if color and can_colorize ():
199+ self ._theme = get_theme (force_color = True ).argparse
200+ self ._decolor = decolor
201+ else :
202+ self ._theme = get_theme (force_no_color = True ).argparse
203+ self ._decolor = lambda text : text
204+
205205 # ===============================
206206 # Section and indentation methods
207207 # ===============================
@@ -415,14 +415,7 @@ def _format_actions_usage(self, actions, groups):
415415 return ' ' .join (self ._get_actions_usage_parts (actions , groups ))
416416
417417 def _is_long_option (self , string ):
418- return len (string ) >= 2 and string [1 ] in self ._prefix_chars
419-
420- def _is_short_option (self , string ):
421- return (
422- not self ._is_long_option (string )
423- and len (string ) >= 1
424- and string [0 ] in self ._prefix_chars
425- )
418+ return len (string ) > 2
426419
427420 def _get_actions_usage_parts (self , actions , groups ):
428421 # find group indices and identify actions in groups
@@ -471,25 +464,22 @@ def _get_actions_usage_parts(self, actions, groups):
471464 # produce the first way to invoke the option in brackets
472465 else :
473466 option_string = action .option_strings [0 ]
467+ if self ._is_long_option (option_string ):
468+ option_color = t .summary_long_option
469+ else :
470+ option_color = t .summary_short_option
474471
475472 # if the Optional doesn't take a value, format is:
476473 # -s or --long
477474 if action .nargs == 0 :
478475 part = action .format_usage ()
479- if self ._is_long_option (part ):
480- part = f"{ t .summary_long_option } { part } { t .reset } "
481- elif self ._is_short_option (part ):
482- part = f"{ t .summary_short_option } { part } { t .reset } "
476+ part = f"{ option_color } { part } { t .reset } "
483477
484478 # if the Optional takes a value, format is:
485479 # -s ARGS or --long ARGS
486480 else :
487481 default = self ._get_default_metavar_for_optional (action )
488482 args_string = self ._format_args (action , default )
489- if self ._is_long_option (option_string ):
490- option_color = t .summary_long_option
491- elif self ._is_short_option (option_string ):
492- option_color = t .summary_short_option
493483 part = (
494484 f"{ option_color } { option_string } "
495485 f"{ t .summary_label } { args_string } { t .reset } "
@@ -606,10 +596,8 @@ def color_option_strings(strings):
606596 for s in strings :
607597 if self ._is_long_option (s ):
608598 parts .append (f"{ t .long_option } { s } { t .reset } " )
609- elif self ._is_short_option (s ):
610- parts .append (f"{ t .short_option } { s } { t .reset } " )
611599 else :
612- parts .append (s )
600+ parts .append (f" { t . short_option } { s } { t . reset } " )
613601 return parts
614602
615603 # if the Optional doesn't take a value, format is:
@@ -2723,16 +2711,9 @@ def format_help(self):
27232711 return formatter .format_help ()
27242712
27252713 def _get_formatter (self ):
2726- if isinstance (self .formatter_class , type ) and issubclass (
2727- self .formatter_class , HelpFormatter
2728- ):
2729- return self .formatter_class (
2730- prog = self .prog ,
2731- prefix_chars = self .prefix_chars ,
2732- color = self .color ,
2733- )
2734- else :
2735- return self .formatter_class (prog = self .prog )
2714+ formatter = self .formatter_class (prog = self .prog )
2715+ formatter ._set_color (self .color )
2716+ return formatter
27362717
27372718 # =====================
27382719 # Help-printing methods
0 commit comments