
class DefaultsAndTypesHelpFormatter(argparse.HelpFormatter):
    def _get_help_string(self, action):
        help = action.help
        if '%(default)' not in action.help:
            if action.default is not argparse.SUPPRESS:
                defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
                if action.option_strings or action.nargs in defaulting_nargs:
                    help += ' (default: %(default)s)'
                if action.type:
                    help += ' (type: %(type)s)'
        return help


def check_params(params=None):
    import sys
    import defaults

    if params is None:
        myargs = sys.argv[1:]
    else:
        myargs = [params["template"]]
        for key, value in params["params"].items():
            myargs.append("--"+key)
            myargs.append(str(value))

    ###
    parser = argparse.ArgumentParser(
        formatter_class=DefaultsAndTypesHelpFormatter)

    #parser.add_argument('-o', '--ob', nargs="?", default=None, help='OB JSON file')

    subparsers = parser.add_subparsers(
        dest='template', help='sub-command help')

    import importlib
    import os.path
    import pkgutil
    pkgpath = os.path.dirname(defaults.__file__)
    modules = [name for _, name, _ in pkgutil.iter_modules([pkgpath])]

    print(modules)

    for module in modules:
        mod = importlib.import_module(defaults.__name__+"."+module)

        parser_a = subparsers.add_parser(mod.d["template"],
                                         help=mod.d["help"],
                                         formatter_class=DefaultsAndTypesHelpFormatter)

        for d in mod.d["params"]:
            parser_a.add_argument(*(["--"+d.pop("name")]), **d)

    ###
    args = parser.parse_args(myargs)

    return args
