@@ -580,19 +580,6 @@ def _complete_for_arg(self, arg_action: argparse.Action,
580580
581581 return self ._format_completions (arg_action , results )
582582
583- @staticmethod
584- def _format_message_prefix (arg_action : argparse .Action ) -> str :
585- """Format the arg prefix text that appears before messages printed to the user"""
586- # Check if this is a flag
587- if arg_action .option_strings :
588- flags = ', ' .join (arg_action .option_strings )
589- param = ' ' + str (arg_action .dest ).upper ()
590- return '{}{}' .format (flags , param )
591-
592- # Otherwise this is a positional
593- else :
594- return '{}' .format (str (arg_action .dest ).upper ())
595-
596583 @staticmethod
597584 def _print_message (msg : str ) -> None :
598585 """Print a message instead of tab completions and redraw the prompt and input line"""
@@ -606,47 +593,37 @@ def _print_arg_hint(self, arg_action: argparse.Action) -> None:
606593 """
607594 # Check if hinting is disabled
608595 suppress_hint = getattr (arg_action , ATTR_SUPPRESS_TAB_HINT , False )
609- if suppress_hint or arg_action .help == argparse .SUPPRESS or arg_action . dest == argparse . SUPPRESS :
596+ if suppress_hint or arg_action .help == argparse .SUPPRESS :
610597 return
611598
612- prefix = self ._format_message_prefix (arg_action )
613- prefix = ' {0: <{width}} ' .format (prefix , width = 20 )
614- pref_len = len (prefix )
615-
616- help_text = '' if arg_action .help is None else arg_action .help
617- help_lines = help_text .splitlines ()
618-
619- if len (help_lines ) == 1 :
620- self ._print_message ('\n Hint:\n {}{}\n ' .format (prefix , help_lines [0 ]))
621- else :
622- out_str = '\n {}' .format (prefix )
623- out_str += '\n {0: <{width}}' .format ('' , width = pref_len ).join (help_lines )
624- self ._print_message ('\n Hint:' + out_str + '\n ' )
599+ # Use the parser's help formatter to print just this action's help text
600+ formatter = self ._parser ._get_formatter ()
601+ formatter .start_section ("Hint" )
602+ formatter .add_argument (arg_action )
603+ formatter .end_section ()
604+ out_str = formatter .format_help ()
605+ self ._print_message ('\n ' + out_str )
625606
626607 def _print_unfinished_flag_error (self , flag_arg_state : _ArgumentState ) -> None :
627608 """
628609 Print an error during tab completion when the user has not finished the current flag
629610 :param flag_arg_state: information about the unfinished flag action
630611 """
631- prefix = self ._format_message_prefix (flag_arg_state .action )
632-
633- out_str = "\n Error:\n "
634- out_str += ' {0: <{width}} ' .format (prefix , width = 20 )
635- out_str += generate_range_error (flag_arg_state .min , flag_arg_state .max )
636-
637- out_str += ' ({} entered)' .format (flag_arg_state .count )
638- self ._print_message (style_error ('{}\n ' .format (out_str )))
612+ error = "\n Error: argument {}: {} ({} entered)\n " .\
613+ format (argparse ._get_action_name (flag_arg_state .action ),
614+ generate_range_error (flag_arg_state .min , flag_arg_state .max ),
615+ flag_arg_state .count )
616+ self ._print_message (style_error ('{}' .format (error )))
639617
640618 def _print_completion_error (self , arg_action : argparse .Action , completion_error : CompletionError ) -> None :
641619 """
642620 Print a CompletionError to the user
643621 :param arg_action: action being tab completed
644622 :param completion_error: error that occurred
645623 """
646- prefix = self ._format_message_prefix (arg_action )
647-
648- out_str = "\n Error:\n "
649- out_str += ' {0: <{width}} ' .format (prefix , width = 20 )
650- out_str += str (completion_error )
651-
652- self ._print_message (style_error ('{}\n ' .format (out_str )))
624+ formatter = self ._parser ._get_formatter ()
625+ formatter .start_section ("Error tab completing {}" .format (argparse ._get_action_name (arg_action )))
626+ formatter .add_text (str (completion_error ))
627+ formatter .end_section ()
628+ error = style_error (formatter .format_help ())
629+ self ._print_message ('\n ' + error )
0 commit comments