Parsing the docstring for help¶
Google (default) format¶
The command-tree by default can parse the classes and function docstring for search help for commands and arguments. The default comment format defined by the Google. For more info, see https://google.github.io/styleguide/pyguide.html#Comments.
help.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from command_tree import CommandTree
tree = CommandTree()
@tree.root()
class Root(object):
@tree.leaf()
@tree.argument()
def command1(self, arg1):
"""Help for command1
Args:
arg1: help for arg1
"""
return int(arg1) / 2
@tree.leaf()
@tree.argument()
def command2(self, arg1):
"""Help for command2
Args:
arg1: help for arg1
"""
return int(arg1) * 2
print(tree.execute())
|
python examples/help.py -h
usage: help.py [-h] subcommand ...
positional arguments:
subcommand
command1 Help for command1
command2 Help for command2
optional arguments:
-h, --help show this help message and exit
python examples/help.py command1 -h
usage: help.py command1 [-h] arg1
positional arguments:
arg1 help for arg1
optional arguments:
-h, --help show this help message and exit
Custom format¶
But if you want to use an other comment format, you can specify a custom comment parser in the config:
help-custom.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from command_tree import CommandTree, Config
from command_tree.doc_string_parser import DocStringInfo, ParserBase
class MyDocStringParser(ParserBase):
def parse(self, content):
info = DocStringInfo()
# parse the content and put into a DocStringInfo instance ...
return info
config = Config(docstring_parser = MyDocStringParser())
tree = CommandTree(config)
@tree.root()
class Root(object):
@tree.leaf()
@tree.argument()
def command1(self, arg1):
"""Help for command1
Parameters
----------
arg1 : int
Description of arg1
"""
return int(arg1) / 2
print(tree.execute())
|