将不同的输入解析为不同的 Python 函数
Parse different inputs into different Python functions
我有一个名为 functions.py 的文件,其中包含多个函数(func1、func2、func3..)
我想编写一个解析器,允许我以这种方式在 terminal/cluster 上 运行 这些函数:
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} ...
PROG:错误:无效选择:“2”(从 'func1'、'func2' 中选择)
我已经阅读了这些内容,但仍然不知道该怎么做:
https://docs.python.org/3/library/argparse.html#module-argparse
Call function based on argparse
How to use argparse subparsers correctly?
谢谢!
add_subparsers
自动为您添加一个位置参数,您不需要显式添加 func
参数。
但是,您确实需要跟踪使用了哪个子解析器。 docs 解释得很好。
在知道使用了哪个解析器以及要传递哪些参数后,您可以使用从 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
我有一个名为 functions.py 的文件,其中包含多个函数(func1、func2、func3..) 我想编写一个解析器,允许我以这种方式在 terminal/cluster 上 运行 这些函数:
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} ... PROG:错误:无效选择:“2”(从 'func1'、'func2' 中选择)
我已经阅读了这些内容,但仍然不知道该怎么做:
https://docs.python.org/3/library/argparse.html#module-argparse
Call function based on argparse
How to use argparse subparsers correctly?
谢谢!
add_subparsers
自动为您添加一个位置参数,您不需要显式添加 func
参数。
但是,您确实需要跟踪使用了哪个子解析器。 docs 解释得很好。
在知道使用了哪个解析器以及要传递哪些参数后,您可以使用从 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