generate_doc: Use argparse for command syntax
This commit is contained in:
parent
05f3809d01
commit
b03b0a173c
@ -47,27 +47,10 @@ def _open_file(name, mode='w'):
|
|||||||
|
|
||||||
def _get_cmd_syntax(name, cmd):
|
def _get_cmd_syntax(name, cmd):
|
||||||
"""Get the command syntax for a command."""
|
"""Get the command syntax for a command."""
|
||||||
words = []
|
usage = cmd.parser.format_usage()
|
||||||
argspec = inspect.getfullargspec(cmd.handler)
|
if usage.startswith('usage: '):
|
||||||
if argspec.defaults is not None:
|
usage = usage[7:]
|
||||||
defaults = dict(zip(reversed(argspec.args),
|
return usage
|
||||||
reversed(list(argspec.defaults))))
|
|
||||||
else:
|
|
||||||
defaults = {}
|
|
||||||
words.append(name)
|
|
||||||
minargs, maxargs = cmd.nargs
|
|
||||||
i = 1
|
|
||||||
for arg in argspec.args:
|
|
||||||
if arg in ['self', 'count']:
|
|
||||||
continue
|
|
||||||
if minargs is not None and i <= minargs:
|
|
||||||
words.append('<{}>'.format(arg))
|
|
||||||
elif maxargs is None or i <= maxargs:
|
|
||||||
words.append('[<{}>]'.format(arg))
|
|
||||||
i += 1
|
|
||||||
if argspec.varargs is not None:
|
|
||||||
words.append('[<{name}> [...]]'.format(name=argspec.varargs))
|
|
||||||
return (' '.join(words), defaults)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_command_quickref(cmds):
|
def _get_command_quickref(cmds):
|
||||||
@ -105,7 +88,7 @@ def _get_command_doc(name, cmd):
|
|||||||
"""Generate the documentation for a command."""
|
"""Generate the documentation for a command."""
|
||||||
output = ['[[cmd-{}]]'.format(name)]
|
output = ['[[cmd-{}]]'.format(name)]
|
||||||
output += ['==== {}'.format(name)]
|
output += ['==== {}'.format(name)]
|
||||||
syntax, defaults = _get_cmd_syntax(name, cmd)
|
syntax = _get_cmd_syntax(name, cmd)
|
||||||
if syntax != name:
|
if syntax != name:
|
||||||
output.append('Syntax: +:{}+'.format(syntax))
|
output.append('Syntax: +:{}+'.format(syntax))
|
||||||
output.append("")
|
output.append("")
|
||||||
@ -119,12 +102,6 @@ def _get_command_doc(name, cmd):
|
|||||||
text = desc.splitlines()
|
text = desc.splitlines()
|
||||||
firstline = text[0].replace(', or None', '')
|
firstline = text[0].replace(', or None', '')
|
||||||
item = "* +{}+: {}".format(arg, firstline)
|
item = "* +{}+: {}".format(arg, firstline)
|
||||||
if arg in defaults:
|
|
||||||
val = defaults[arg]
|
|
||||||
if val is None:
|
|
||||||
item += " (optional)\n"
|
|
||||||
else:
|
|
||||||
item += " (default: +{}+)\n".format(defaults[arg])
|
|
||||||
item += '\n'.join(text[1:])
|
item += '\n'.join(text[1:])
|
||||||
output.append(item)
|
output.append(item)
|
||||||
output.append("")
|
output.append("")
|
||||||
|
Loading…
Reference in New Issue
Block a user