@@ 9,26 9,32 @@ import inspect
import argparse
from typing import Callable, Optional, TypedDict, Dict, Any, Union, get_origin, get_args
try:
- import argcomplete
+ import argcomplete # type: ignore
except ImportError:
argcomplete = None
__version__ = "0.4.1"
+OptStr = Optional[str]
+
+
class NameClashException(Exception):
pass
+
class CommandType(TypedDict):
help:Optional[str]
description:Optional[str]
func:Callable
args:Dict[str, Any]
+
class CommandGroup(TypedDict):
help:Optional[str]
description:Optional[str]
commands:Dict[str,CommandType]
+
commands:Dict[str,CommandGroup] = {
'' : {
'help': '',
@@ 37,10 43,12 @@ commands:Dict[str,CommandGroup] = {
}
}
+
def is_optional(field):
return get_origin(field) is Union and \
type(None) in get_args(field)
+
def get_parser(*args, **kwargs) -> argparse.ArgumentParser:
"""Build command line parser
@@ 79,7 87,7 @@ def execute(parser:argparse.ArgumentParser):
func(**kwargs)
-def run(prog:str=None, usage:str=None, description:str=None, **kwargs):
+def run(prog:OptStr=None, usage:OptStr=None, description:OptStr=None, **kwargs):
"""Run your application"""
parser = get_parser(prog=prog, usage=usage, description=description, **kwargs)
execute(parser)
@@ 139,7 147,7 @@ class group():
@_optional_arg_decorator
-def command(fnc:Callable, command_name:str=None, group_name:str=''):
+def command(fnc:Callable, command_name:OptStr=None, group_name:str=''):
"""Build subcommand from function
Subcommand name will be the function name and arguments are parsed to build the command line.