Python - 将参数传递给来自 Argparse 的不同方法 [英] Python - Pass Arguments to Different Methods from Argparse
问题描述
我正在编写一个相对简单的 Python 脚本,它支持几个不同的命令.不同的命令支持不同的选项,我希望能够将 argparse 解析的选项传递给指定命令的正确方法.
用法字符串如下所示:
用法:script.py [-h]{a, b, c}...script.py:错误:参数太少
我可以轻松调用适当的方法:
def a():...定义 b():...定义 c():...如果 __name__ == "__main__":解析器 = argparse.ArgumentParser()parser.set_defaults(method = a)...参数 = parser.parse_args()参数.方法()
但是,我必须向这些方法传递参数,而且它们都接受不同的参数集.
目前,我只是传递 argparse 返回的 Namespace
对象,如下所示:
def a(arguments):arg1 = getattr(参数,'arg1',无)...
这看起来有点尴尬,并且使方法更难重用,因为我必须将参数作为 dict
或命名空间而不是通常的参数传递.
我想以某种方式定义带参数的方法(就像您定义普通函数一样),并且仍然能够在传递适当参数的同时动态调用它们.像这样:
def a(arg1, arg2):...如果 __name__ == "__main__":解析器 = argparse.ArgumentParser()parser.set_defaults(method = a)...参数 = parser.parse_args()arguments.method() # <<<<参数以某种方式在这里传递
有什么想法吗?
我找到了一个很好的解决方案:
导入 argparsedef a(arg1, arg2, **kwargs):打印 arg1打印 arg2如果 __name__ == "__main__":解析器 = argparse.ArgumentParser()parser.set_defaults(method = a)parser.add_argument('arg1', type = str)parser.add_argument('arg2', type = str)参数 = parser.parse_args()arguments.method(**vars(arguments))
当然,如果方法的参数与 argparse 使用的参数的名称发生冲突,则存在一个小问题,尽管我认为这比传递 Namespace 对象并使用 getattr
更好.>
I'm writing a relatively simple Python script which supports a couple of different commands. The different commands support different options and I want to be able to pass the options parsed by argparse to the correct method for the specified command.
The usage string looks like so:
usage: script.py [-h]
{a, b, c}
...
script.py: error: too few arguments
I can easily call the appropriate method:
def a():
...
def b():
...
def c():
...
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.set_defaults(method = a)
...
arguments = parser.parse_args()
arguments.method()
However, I have to pass arguments to these methods and they all accept different sets of arguments.
Currently, I just pass the Namespace
object returned by argparse, like so:
def a(arguments):
arg1 = getattr(arguments, 'arg1', None)
...
This seems a little awkward, and makes the methods a little harder to reuse as I have to pass arguments as a dict
or namespace rather than as usual parameters.
I would like someway of defining the methods with parameters (as you would a normal function) and still be able to call them dynamically while passing appropriate parameters. Like so:
def a(arg1, arg2):
...
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.set_defaults(method = a)
...
arguments = parser.parse_args()
arguments.method() # <<<< Arguments passed here somehow
Any ideas?
I found quite a nice solution:
import argparse
def a(arg1, arg2, **kwargs):
print arg1
print arg2
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.set_defaults(method = a)
parser.add_argument('arg1', type = str)
parser.add_argument('arg2', type = str)
arguments = parser.parse_args()
arguments.method(**vars(arguments))
Of course there's a minor problem if the arguments of the method clash with the names of the arguments argparse uses, though I think this is preferable to passing the Namespace object around and using getattr
.
这篇关于Python - 将参数传递给来自 Argparse 的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!