@@ 166,11 166,18 @@ def command(fnc:Callable, command_name:str=None, group_name:str=''):
e.g. `Optional[str]` and default value `None`
Function docstring is used to set command's help and description.
+ To set arguments help string, add a line in docstring like
+
+ @param argname : argument help
+
+ Exacmple:
-
@command
def one(name, debug:bool, value="default", switchoff=True):
- "First subcommand"
+ \"""First subcommand
+
+ @param debug: enable debug output
+ ""\"
...
@command
@@ 201,8 208,8 @@ def command(fnc:Callable, command_name:str=None, group_name:str=''):
optional arguments:
-h, --help show this help message and exit
- --debug
- --value VALUE
+ --debug enable debug output
+ --value VALUE (default 'default')
--switchoff
$ script two -h
@@ 219,17 226,26 @@ def command(fnc:Callable, command_name:str=None, group_name:str=''):
"""
- #parser = argparse.ArgumentParser(description=fnc.__doc__)
+ description:str = fnc.__doc__ or ""
+
+ # extract "@param name help str" from docstring
+ args_help:Dict[str,str] = {}
+ for l in description.split("\n"):
+ if l.strip().startswith("@param "):
+ p_name, p_help = l.replace("@param", "").strip().split(":",1)
+ args_help[p_name.strip()] = p_help.strip()
+ description = description.replace(l, "")
+
help:Optional[str] = None
try:
- help = fnc.__doc__ or ""
+ help = description
help = help.split('\n')[0].strip()
except (AttributeError, IndexError):
help = None
command:CommandType = {
'help' : help,
- 'description' : fnc.__doc__,
+ 'description' : description,
'func' : fnc,
'args' : {},
}
@@ 275,6 291,12 @@ def command(fnc:Callable, command_name:str=None, group_name:str=''):
del arg['default']
del arg['type']
+ # build arg help text
+ a_help = args_help.get(param.name, "")
+ if arg.get('default') is not None:
+ a_help += f" (default '{arg['default']}')"
+ arg['help'] = a_help.strip()
+
# "arg_name" to "arg-name"
name = name.replace("_", "-")
command['args'][name] = arg