MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / error

Method error

cmd2/argparse_utils.py:852–872  ·  view source on GitHub ↗

Override that applies custom formatting to the error message.

(self, message: str)

Source from the content-addressed store, hash-verified

850 raise ValueError(f"Subcommand '{subcommand}' does not exist for '{target_parser.prog}'") from None
851
852 def error(self, message: str) -> NoReturn:
853 """Override that applies custom formatting to the error message."""
854 lines = message.split("\n")
855 formatted_message = ""
856 for linum, line in enumerate(lines):
857 if linum == 0:
858 formatted_message = "Error: " + line
859 else:
860 formatted_message += "\n " + line
861
862 with self.output_to(sys.stderr):
863 self.print_usage(sys.stderr)
864
865 # Use console to add style since it will respect ALLOW_STYLE's value.
866 # Now _get_formatter() will return a formatter bound to stderr.
867 console = self._get_formatter().console
868 with console.capture() as capture:
869 console.print(formatted_message, style=Cmd2Style.ERROR)
870 formatted_message = f"{capture.get()}"
871
872 self.exit(2, f"{formatted_message}\n")
873
874 def _get_formatter(self, **_kwargs: Any) -> Cmd2HelpFormatter:
875 """Override with customizations for Cmd2HelpFormatter."""

Callers 1

Calls 5

output_toMethod · 0.95
print_usageMethod · 0.95
_get_formatterMethod · 0.95
printMethod · 0.80
getMethod · 0.45

Tested by 1