# -*- coding: utf-8 -*-importostry:importtypingexcept:passimportargparsefrompygitrepo.actionsimportActions,actionsfrompygitrepo.repo_configimportRepoConfigfrompygitrepo.pkg.mini_colormaimportFore,StyleSUB_COMMAND="sub_command"defcolorful(message):returnFore.CYAN+message+Style.RESET_ALLparser=argparse.ArgumentParser(prog="pygitrepo",)subparser=parser.add_subparsers(title="sub commands",description="run automation script",prog="hello",dest=SUB_COMMAND,)# A dictionary object provide access to the underlying function using# subcommand nameaction_mapper=dict()# type: typing.Dict[str, callable]formethod_name,methodinActions.__dict__.items():ifnotmethod_name.startswith("_"):subcommand_parser=subparser.add_parser(name=method._subcommand_name,help=Fore.CYAN+method._subcommand_help+Style.RESET_ALL)subcommand_parser.add_argument("--do-dry-run",action="store_true",help="display info, doesn't take effect",)action_mapper[method._subcommand_name]=getattr(actions,method_name)# manually add more important subc ```````ommand parserclassAddtionalSubCommandEnum:get_value=NoneforkinAddtionalSubCommandEnum.__dict__:ifnotk.startswith("_"):setattr(AddtionalSubCommandEnum,k,k.replace("_","-"))get_value_parser=subparser.add_parser(name=AddtionalSubCommandEnum.get_value,help=Fore.CYAN+"pass python data to shell scripts"+Style.RESET_ALL)get_value_parser.add_argument(dest="attr_name",type=str)
[docs]defget_value(config,attr_name):""" echo the value of an attribute .. code-block:: bash dir_project_root="$(pgr get-value dir_project_root)" :type config: RepoConfig """ifisinstance(getattr(RepoConfig,attr_name),property):print(getattr(config,attr_name))else:print(getattr(config,attr_name).get_value())
[docs]defmain():# pragma: no cover""" Command Line Interface entry point. """# defined arguments stored in args, a named tuple object# additional undefined arguments stored in unknownargs,unknown=parser.parse_known_args()ifargs.sub_commandisNone:parser.parse_args(["-h"])returnrepo_config=RepoConfig()repo_config.read_pygitrepo_config_file()ifargs.sub_commandinaction_mapper:action_mapper[args.sub_command](repo_config,_dry_run=args.do_dry_run,_args=unknown,)elifargs.sub_command==AddtionalSubCommandEnum.get_value:get_value(repo_config,args.attr_name)else:raiseNotImplementedError