From 3087600c8da40c34e560e37e0540f4c888134402 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 21 Mar 2017 00:45:52 -0400 Subject: [PATCH] [commands] Add Command.signature This replaces HelpFormatter.get_command_signature for the most part. --- discord/ext/commands/core.py | 39 +++++++++++++++++++++++++++++++ discord/ext/commands/formatter.py | 32 +------------------------ 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index 71d5ff4be..bca52ad36 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -544,6 +544,45 @@ class Command: return self.help.split('\n', 1)[0] return '' + @property + def signature(self): + """Returns a POSIX-like signature useful for help command output.""" + result = [] + parent = self.full_parent_name + if len(self.aliases) > 0: + aliases = '|'.join(self.aliases) + fmt = '[%s|%s]' % (self.name, aliases) + if parent: + fmt = parent + fmt + result.append(fmt) + else: + name = self.name if not parent else parent + ' ' + self.name + result.append(name) + + if self.usage: + result.append(self.usage) + return ' '.join(result) + + params = self.clean_params + if not params: + return ' '.join(result) + + for name, param in params.items(): + if param.default is not param.empty: + # We don't want None or '' to trigger the [name=value] case and instead it should + # do [name] since [name=None] or [name=] are not exactly useful for the user. + should_print = param.default if isinstance(param.default, str) else param.default is not None + if should_print: + result.append('[%s=%s]' % (name, param.default)) + else: + result.append('[%s]' % name) + elif param.kind == param.VAR_POSITIONAL: + result.append('[%s...]' % name) + else: + result.append('<%s>' % name) + + return ' '.join(result) + @asyncio.coroutine def can_run(self, ctx): """|coro| diff --git a/discord/ext/commands/formatter.py b/discord/ext/commands/formatter.py index 5c9fff2f4..523d616ae 100644 --- a/discord/ext/commands/formatter.py +++ b/discord/ext/commands/formatter.py @@ -190,39 +190,9 @@ class HelpFormatter: def get_command_signature(self): """Retrieves the signature portion of the help page.""" - result = [] prefix = self.clean_prefix cmd = self.command - parent = cmd.full_parent_name - if len(cmd.aliases) > 0: - aliases = '|'.join(cmd.aliases) - fmt = '{0}[{1.name}|{2}]' - if parent: - fmt = '{0}{3} [{1.name}|{2}]' - result.append(fmt.format(prefix, cmd, aliases, parent)) - else: - name = prefix + cmd.name if not parent else prefix + parent + ' ' + cmd.name - result.append(name) - - params = cmd.clean_params - if cmd.usage: - result.append(cmd.usage) - elif len(params) > 0: - for name, param in params.items(): - if param.default is not param.empty: - # We don't want None or '' to trigger the [name=value] case and instead it should - # do [name] since [name=None] or [name=] are not exactly useful for the user. - should_print = param.default if isinstance(param.default, str) else param.default is not None - if should_print: - result.append('[{}={}]'.format(name, param.default)) - else: - result.append('[{}]'.format(name)) - elif param.kind == param.VAR_POSITIONAL: - result.append('[{}...]'.format(name)) - else: - result.append('<{}>'.format(name)) - - return ' '.join(result) + return prefix + cmd.signature def get_ending_note(self): command_name = self.context.invoked_with