From 916d7933aa4df4e61c6b6bcbc90529f91a5f1caa Mon Sep 17 00:00:00 2001 From: Vincent Le Goff Date: Sun, 18 Jun 2017 20:29:46 -0700 Subject: [PATCH] Fix minor errors in displaying the UnixCommand --- evennia/contrib/unixcommand.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/evennia/contrib/unixcommand.py b/evennia/contrib/unixcommand.py index 0bc4ad9fbe..dde36207da 100644 --- a/evennia/contrib/unixcommand.py +++ b/evennia/contrib/unixcommand.py @@ -149,7 +149,9 @@ class UnixCommand(Command): try: self.opts = self.parser.parse_args(shlex.split(self.args)) except ParseError as err: - self.msg(str(err)) + msg = str(err) + if msg: + self.msg(msg) raise InterruptCommand @@ -168,16 +170,17 @@ class EvenniaParser(argparse.ArgumentParser): prog = prog or command.key super(EvenniaParser, self).__init__( prog=prog, description=description, - formatter_class=argparse.RawDescriptionHelpFormatter, - conflict_handler='resolve', **kwargs) + conflict_handler='resolve', add_help=False, **kwargs) self.command = command self.post_help = epilog def n_exit(code=None, msg=None): - if msg: - raise ParseError(msg) + raise ParseError(msg) self.exit = n_exit + # Replace the -h/--help + self.add_argument("-h", "--hel", nargs=0, action=HelpAction, help="display heeeelp") + def format_usage(self): """Return the usage line.""" return raw(super(EvenniaParser, self).format_usage()) @@ -185,18 +188,24 @@ class EvenniaParser(argparse.ArgumentParser): def format_help(self): """Return the parser help, including its epilog.""" autohelp = raw(super(EvenniaParser, self).format_help()) - return autohelp + "\n\n" + self.post_help + return "\n" + autohelp + "\n" + self.post_help def print_usage(self, file=None): """Print the usage to the caller.""" if self.command: - self.command.msg(ParseError(self.format_usage())) - else: - raise ParseError(self.format_usage()) + self.command.msg(self.format_usage().strip()) def print_help(self, file=None): """Print the help to the caller.""" if self.command: - self.command.msg(ParseError(self.format_help())) - else: - raise ParseError(self.format_help()) + self.command.msg(self.format_help().strip()) + + +class HelpAction(argparse.Action): + + """Override the -h/--he.p.""" + + def __call__(self, parser, namespace, values, option_string=None): + if parser.command: + parser.command.msg(parser.format_help().strip()) + parser.exit(0, "")