工作中遇到这样一个场景,某个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