Python:除了来自外部函数的参数外,还使用 argparse 读取命令行参数 [英] Python: read command line args with argparse in addition to those coming from an external function
问题描述
我在同一个包中有两个模块,module1.py
和 module2.py
.
I have two modules in the same package, module1.py
and module2.py
.
在 module1.py
中,我有一个使用 argparse
读取命令行参数的函数:
In module1.py
I have a function reading command-line args with argparse
:
import argparse
def parse_arguments_module1():
parser = argparse.ArgumentParser()
optional = parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
required.add_argument('--argA', help='argA', required=True)
required.add_argument('--argB', help='argB', required=True)
optional.add_argument('--argC', help='argC')
optional.add_argument('--argD', help='argD')
parser._action_groups.append(optional)
args = parser.parse_args()
return args
现在假设在 module2.py
中,我从 module1.py
导入 parse_arguments_module1()
并使用它(这有效):
Now suppose in module2.py
I import parse_arguments_module1()
from module1.py
and use it (this works):
from module1 import parse_arguments_module1
if __name__ == '__main__':
args = parse_arguments_module1()
print(args)
使用:
python3 module2.py --argA A --argB B --argC C
输出:
命名空间(argA='A', argB='B', argC='C', argD=None)
Namespace(argA='A', argB='B', argC='C', argD=None)
问题是:如何读取module2.py
中的参数(required
和/或optional
)additional 到 module1.py
的那些?(即 main
中的 args
包含更多参数,仅用于 module2.py
)
The question is: how to read arguments in module2.py
(required
and/or optional
) additional to those of module1.py
? (I.e. have args
in main
contain more arguments, just for module2.py
)
推荐答案
你需要使用 部分解析,使用parser.parse_known_args()
来实现你想要的,和/或明确地将你的参数作为列表传递.
You'd need to use partial parsing with parser.parse_known_args()
to achieve what you want, and / or pass your arguments as a list, explicitly.
通常,没有参数 parser.parse_args()
将 sys.argv[1:]
中的所有值(所以除了第一个元素之外的所有值)作为解析的输入.如果该列表中存在无法解析的元素,则会打印错误消息并调用 sys.exit(1)
;您的脚本将退出.
Normally, without arguments parser.parse_args()
takes all values from sys.argv[1:]
(so everything but the first element) as the input to parse. If there are elements in that list that can't be parsed, then an error message is printed and sys.exit(1)
is called; your script would exit.
因此,如果您希望 sys.argv
上的 some 参数转到一个解析器,而其余部分转到另一个解析器,则需要使用 parser.parse_known_args()
并将余数传递给另一个解析器.
So if you want some arguments on sys.argv
to go to one parser, and the remainder to another, you want to use parser.parse_known_args()
and pass the remainder to the other parser.
我将创建和配置 ArgumentParser()
实例与解析分开;所以在 module1
做:
I'd split out creating and configuring the ArgumentParser()
instances from parsing; so in module1
do:
def module1_argument_parser():
parser = argparse.ArgumentParser()
optional = parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
required.add_argument('--argA', help='argA', required=True)
required.add_argument('--argB', help='argB', required=True)
optional.add_argument('--argC', help='argC')
optional.add_argument('--argD', help='argD')
parser._action_groups.append(optional)
return parser
def parse_arguments_module1(args=None):
parser = module1_argument_parser()
return parser.parse_args(args)
if __name__ == '__main__':
args = parse_arguments_module1()
print(args)
和在module2
中,使用相同的结构,但重新使用module1
中的解析器:
and in module2
, use the same structure, but re-use the parser from module1
:
from module1 import module1_argument_parser
def module2_argument_parser():
parser = argparse.ArgumentParser()
# create argument switches, etc.
return parser
def parse_arguments_module2(args=None):
parser = module2_argument_parser()
return parser.parse_args(args)
if __name__ == '__main__':
module1_parser = module1_argument_parser()
module1_args, remainder = module1_parser.parse_known_args()
module2_args = parse_arguments_module2(remainder)
print(module1_args)
print(module2_args)
这篇关于Python:除了来自外部函数的参数外,还使用 argparse 读取命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!