import sys
import argparse
from functools import partial
import logging
from . import custom_help, script, config
logger = logging.getLogger(__name__)
class EnhanceHelpFormater(argparse.RawDescriptionHelpFormatter):
def __init__(self, prog, cmd_name=None, script_name=None, option=None):
super().__init__(prog)
self._cmd_name = cmd_name
self._script_name = script_name
self._option = option
def format_help(self) -> str:
original_help = super().format_help()
opt = self._option
if self._cmd_name == "run_example":
opt.append("--run_example")
extend_help = config.COMMAND_HELP_MAP.get(self._cmd_name) + original_help
if self._cmd_name != "run_example" and self._cmd_name != "deploy_op":
extend_help += custom_help.get_tools_help(self._script_name + opt)
return extend_help
def create_parser(name):
parser = argparse.ArgumentParser(description="算子工具一站式平台")
parser.add_argument("--version", "-v", action="store_true", help="显示算子工具一站式平台的版本号")
subparsers = parser.add_subparsers(dest="command", help="操作命令")
for cmd_name, cmd_help in config.COMMAND_HELP_MAP.items():
script_name = script.get_script_name(cmd_name, name)
options = script.remove_opt_from_options(sys.argv, "script")
formatter_class = partial(EnhanceHelpFormater, cmd_name=cmd_name, script_name=script_name, option=options[2:])
subparser = subparsers.add_parser(cmd_name, help=cmd_help, formatter_class=formatter_class)
if cmd_name in config.SCRIPT_SUPPORTED_COMMANDS:
script_help = "指定构建脚本入口,当前只有build和run_example命令支持此参数"
subparser.add_argument("--script", default=config.COMMAND_SCRIPT_MAP.get(cmd_name), help=script_help)
if cmd_name == "opprof":
type_help = "--type=onboard/simulator (运行模式onboard(上板)和simulator(仿真)两种运行模式,默认为onboard)"
subparser.add_argument("--type", default="onboard", help=type_help)
if cmd_name == "build":
subparser.add_argument("--debug", action="store_true", help="调试构建场景,自动添加所需构建参数")
subparser.add_argument("--sanitizer", action="store_true", help="mssanitizer检测构建场景,自动添加所需构建参数")
return parser