将不同的输入解析为不同的Python函数

将不同的输入解析为不同的Python函数,python,parsing,command-line-arguments,argparse,Python,Parsing,Command Line Arguments,Argparse,我有一个名为functions.py的文件,其中包含多个函数(func1、func2、func3..) 我想编写一个解析器,它允许我以这种方式在终端/集群上运行这些函数: python parser.py -function func1 -inputf1_1 10 inputf1_2 20 我也可以 python parser.py -function func2 -inputf2 'abc' 我知道如何使用不同的解析器文件(parser_func1.py、parser_func2.py)实

我有一个名为functions.py的文件,其中包含多个函数(func1、func2、func3..) 我想编写一个解析器,它允许我以这种方式在终端/集群上运行这些函数:

python parser.py -function func1 -inputf1_1 10 inputf1_2 20
我也可以

python parser.py -function func2 -inputf2 'abc'
我知道如何使用不同的解析器文件(parser_func1.py、parser_func2.py)实现这一点,但我更希望只有一个parser.py文件

这就是我的代码目前的样子:

import argparse


def func1(x,y):
    print (x*y)

def func2(text):
    print (text)

ap = argparse.ArgumentParser()


FUNCTION_MAP = {'func1' : func1,
                'func2' : func2}


# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('command', choices=FUNCTION_MAP.keys(), help='choose function to run')
subparsers = parser.add_subparsers(help='sub-command help')

# create the parser for the "a" command
parser_a = subparsers.add_parser('func1', help='func1 help')
parser_a.add_argument("-x", "--x", required=True, help="number 1")
parser_a.add_argument("-y", "--y", required=True, help="number 2")


# create the parser for the "b" command
parser_b = subparsers.add_parser('func2', help='func2 help')
parser_b.add_argument("-text", "--text", required=True, help="text you want to print")


args = parser.parse_args()

func = FUNCTION_MAP[args.command]
#I don't know how to put the correct inputs inside the func()
func()
当我现在跑步时:

python functions.py func1 -x 10 -y 2
我得到这个错误: 用法:PROG[-h]{func1,func2}{func1,func2}。。。 程序:错误:无效选择:“2”(从“func1”、“func2”中选择)

我读过这些,但仍然不知道如何做:


谢谢大家!

add_subparser
自动为您添加位置参数,您不需要显式添加
func
参数

但是,您确实需要跟踪使用了哪个子Parser。政府对此解释得很好

在知道使用了哪个解析器和要传递的参数之后,可以使用从
args

以下是完整的代码:

import argparse


def func1(x,y):
    print (x*y)

def func2(text):
    print (text)

ap = argparse.ArgumentParser()


FUNCTION_MAP = {'func1' : func1,
                'func2' : func2}


# create the top-level parser
parser = argparse.ArgumentParser(prog='PROG')
# Just add the subparser, no need to add any other argument.
# Pass the "dest" argument so that we can figure out which parser was used.
subparsers = parser.add_subparsers(help='sub-command help', dest='subparser_name')

# create the parser for the "a" command
parser_a = subparsers.add_parser('func1', help='func1 help')
# Keep the names here in sync with the argument names for "func1"
# also make sure the expected type is the same (int in this case)
parser_a.add_argument("-x", "--x", required=True, help="number 1", type=int)
parser_a.add_argument("-y", "--y", required=True, help="number 2", type=int)


# create the parser for the "b" command
parser_b = subparsers.add_parser('func2', help='func2 help')
parser_b.add_argument("-text", "--text", required=True, help="text you want to print")


args = parser.parse_args()

# subparser_name has either "func1" or "func2".
func = FUNCTION_MAP[args.subparser_name]
# the passed arguments can be taken into a dict like this
func_args = vars(args)
# remove "subparser_name" - it's not a valid argument
del func_args['subparser_name']
# now call the function with it's arguments
func(**func_args)
现在,如果我这样调用脚本:

PROG func1 -x 1 -y 3
我得到的结果是:

3
如果我这样称呼它:

PROG func2 --text=hello
我得到:

hello

太棒了,谢谢!它起作用了!在文档的下方有一个示例,使用
set\u defaults
为每个子命令调用特定函数,这在这里是更合适的方法。