Skip to content

Commit 10ffc07

Browse files
committed
Simplifying tab completion message printing
1 parent f07808a commit 10ffc07

File tree

2 files changed

+20
-61
lines changed

2 files changed

+20
-61
lines changed

cmd2/argparse_completer.py

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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('\nHint:\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('\nHint:' + 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 = "\nError:\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 = "\nError: 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 = "\nError:\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)

tests/test_argparse_completer.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def test_unfinished_flag_error(ac_app, command_and_args, text, is_error, capsys)
654654
complete_tester(text, line, begidx, endidx, ac_app)
655655

656656
out, err = capsys.readouterr()
657-
assert is_error == all(x in out for x in ["Error:\n", "expected"])
657+
assert is_error == all(x in out for x in ["Error: argument", "expected"])
658658

659659

660660
def test_completion_items_default_header(ac_app):
@@ -709,24 +709,6 @@ def test_autocomp_hint(ac_app, command_and_args, text, has_hint, capsys):
709709
assert has_hint == ("Hint:\n" in out)
710710

711711

712-
def test_autocomp_hint_multiple_lines(ac_app, capsys):
713-
text = ''
714-
line = 'hint {}'.format(text)
715-
endidx = len(line)
716-
begidx = endidx - len(text)
717-
718-
first_match = complete_tester(text, line, begidx, endidx, ac_app)
719-
out, err = capsys.readouterr()
720-
721-
assert first_match is None
722-
assert out == '''
723-
Hint:
724-
HINT_POS here is a hint
725-
with new lines
726-
727-
'''
728-
729-
730712
def test_autocomp_hint_no_help_text(ac_app, capsys):
731713
text = ''
732714
line = 'hint foo {}'.format(text)

0 commit comments

Comments
 (0)