[commands] Add Command.signature
This replaces HelpFormatter.get_command_signature for the most part.
This commit is contained in:
parent
54fdafb792
commit
3087600c8d
@ -544,6 +544,45 @@ class Command:
|
|||||||
return self.help.split('\n', 1)[0]
|
return self.help.split('\n', 1)[0]
|
||||||
return ''
|
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
|
@asyncio.coroutine
|
||||||
def can_run(self, ctx):
|
def can_run(self, ctx):
|
||||||
"""|coro|
|
"""|coro|
|
||||||
|
@ -190,39 +190,9 @@ class HelpFormatter:
|
|||||||
|
|
||||||
def get_command_signature(self):
|
def get_command_signature(self):
|
||||||
"""Retrieves the signature portion of the help page."""
|
"""Retrieves the signature portion of the help page."""
|
||||||
result = []
|
|
||||||
prefix = self.clean_prefix
|
prefix = self.clean_prefix
|
||||||
cmd = self.command
|
cmd = self.command
|
||||||
parent = cmd.full_parent_name
|
return prefix + cmd.signature
|
||||||
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)
|
|
||||||
|
|
||||||
def get_ending_note(self):
|
def get_ending_note(self):
|
||||||
command_name = self.context.invoked_with
|
command_name = self.context.invoked_with
|
||||||
|
Loading…
x
Reference in New Issue
Block a user