基于任意位置分段的python argpaser实现

工作中遇到这样一个场景,某个executor具有某些args,同时在--params出现后,后面所有的args都被认为是用户script需要的args,这部分arg的key和value可能以任意形式出现,并且可能与executor自身的args重复,此时要求不进行处理。 如果--params没有出现,则使用所有参数parse executor args忽略unknown key。

example input #1:

executor --file xx.py --name xx_name --params --my_key xx --file my_file

expected output #1:

parser return executor_args and func_args.
executor_args.file = xx.py (not 'my_file')
executor_args.name = xx_name

func_args = [--my_key xx --file my_file] 


example input #2 (missing --params):

executor --file xx.py --name xx_name --my_key xx --file my_file

expected output #2:

parser return executor_args and func_args.
executor_args.file = my_file
executor_args.name = xx_name

func_args = [] 

实现这一argparser,同时保证不影响auto gen的 --help使用,parser的实现如下:

class _ExecutorParser(argparse.ArgumentParser):  # Extend argparser

    def __init__(self):  # add args for help
        super().__init__()
        self.add_argument("-f", "--file", required=True, help="The executed file name.")
        self.add_argument(
            "--name",
            help="The dsl component name. Optional "
                 "if there is only one dsl component in the file.")
        self.add_argument(
            '--params', action='store_true',
            help="A mark to split parameters, dsl component function parameters goes after this.")

    def parse_known_args(self, args=None, namespace=None):
        if args is None:
            # args default to the system args
            args = sys.argv[1:]
        else:
            # make sure that args are mutable
            args = list(args)
        # Test parse once for -h or something else
        parsed_args, execution_parameters = super().parse_known_args(args, namespace)
        if '--params' not in args:
            logging.warning(
                "The '--params' mark to split parameters not found, "
                "execute function with no parameters.")
            return parsed_args, []
        # Recalculate execution params by split args from '--params'
        execution_index = args.index('--params')
        execution_parameters = args[execution_index:]
        # Recalculate parsed args as there may be duplicate keys in execution parameters.
        parsed_args, _ = super().parse_known_args(args[:execution_index + 1], namespace)
        if execution_parameters:
            # Pop the execution parameters key
            execution_parameters.pop(0)
        return parsed_args, execution_parameters

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值