src2asciidoc: Split _get_command_doc.

This commit is contained in:
Florian Bruhin 2015-04-06 19:48:36 +02:00
parent 2d258ec53f
commit a480b297ca

View File

@ -157,47 +157,82 @@ def _get_command_doc(name, cmd):
output.append("") output.append("")
output.append(parser.long_desc) output.append(parser.long_desc)
if cmd.pos_args: output += list(_get_command_doc_args(cmd, parser))
output += list(_get_command_doc_count(cmd, parser))
output += list(_get_command_doc_notes(cmd))
output.append("") output.append("")
output.append("==== positional arguments") output.append("")
return '\n'.join(output)
def _get_command_doc_args(cmd, parser):
"""Get docs for the arguments of a command.
Args:
cmd: The Command to get the docs for.
parser: The DocstringParser to use.
Yield:
Strings which should be added to the docs.
"""
if cmd.pos_args:
yield ""
yield "==== positional arguments"
for arg, name in cmd.pos_args: for arg, name in cmd.pos_args:
try: try:
output.append("* +'{}'+: {}".format(name, yield "* +'{}'+: {}".format(name, parser.arg_descs[arg])
parser.arg_descs[arg]))
except KeyError as e: except KeyError as e:
raise KeyError("No description for arg {} of command " raise KeyError("No description for arg {} of command "
"'{}'!".format(e, cmd.name)) "'{}'!".format(e, cmd.name))
if cmd.opt_args: if cmd.opt_args:
output.append("") yield ""
output.append("==== optional arguments") yield "==== optional arguments"
for arg, (long_flag, short_flag) in cmd.opt_args.items(): for arg, (long_flag, short_flag) in cmd.opt_args.items():
try: try:
output.append('* +*{}*+, +*{}*+: {}'.format( yield '* +*{}*+, +*{}*+: {}'.format(short_flag, long_flag,
short_flag, long_flag, parser.arg_descs[arg])) parser.arg_descs[arg])
except KeyError: except KeyError:
raise KeyError("No description for arg {} of command " raise KeyError("No description for arg {} of command "
"'{}'!".format(e, cmd.name)) "'{}'!".format(e, cmd.name))
def _get_command_doc_count(cmd, parser):
"""Get docs for the count of a command.
Args:
cmd: The Command to get the docs for.
parser: The DocstringParser to use.
Yield:
Strings which should be added to the docs.
"""
if cmd.special_params['count'] is not None: if cmd.special_params['count'] is not None:
output.append("") yield ""
output.append("==== count") yield "==== count"
output.append(parser.arg_descs[cmd.special_params['count']]) yield parser.arg_descs[cmd.special_params['count']]
def _get_command_doc_notes(cmd):
"""Get docs for the notes of a command.
Args:
cmd: The Command to get the docs for.
parser: The DocstringParser to use.
Yield:
Strings which should be added to the docs.
"""
if cmd.maxsplit is not None or cmd.no_cmd_split: if cmd.maxsplit is not None or cmd.no_cmd_split:
output.append("") yield ""
output.append("==== note") yield "==== note"
if cmd.maxsplit is not None: if cmd.maxsplit is not None:
output.append("* This command does not split arguments after the " yield ("* This command does not split arguments after the last "
"last argument and handles quotes literally.") "argument and handles quotes literally.")
if cmd.no_cmd_split is not None: if cmd.no_cmd_split is not None:
output.append("* With this command, +;;+ is interpreted " yield ("* With this command, +;;+ is interpreted literally "
"literally instead of splitting off a second " "instead of splitting off a second command.")
"command.")
output.append("")
output.append("")
return '\n'.join(output)
def _get_action_metavar(action, nargs=1): def _get_action_metavar(action, nargs=1):