让我们以下面的示例为例。解析器包含两个参数-inputfile和verbosity。 Set_verbosity_level()函数用于将模块级/全局变量(在我的现实生活中为程序包级变量)的值控制为0-4。 CheckFile()函数在输入文件中执行测试(实际情况取决于类型)。 我想根据详细程度在CheckFile()中打印消息。问题在于argparse在Set_verbosity_level()之前调用CheckFile(),因此在CheckFile中,详细级别始终为0 /默认值。
所以我的问题是,是否有任何解决方案可以迫使argparse先评估一些参数...
import argparse
VERBOSITY = 0
def Set_verbosity_level():
"""Set the verbosity level.
"""
def type_func(value):
a_value = int(value)
globals()['VERBOSITY'] = value
print("Verbosity inside Set_verbosity_level(): " + str(globals()['VERBOSITY']))
return value
return type_func
class CheckFile(argparse.FileType):
"""
Check whatever in the file
"""
def __init__(self, mode='r', **kwargs):
super(CheckFile, self).__init__(mode, **kwargs)
def __call__(self, string):
# Do whatever processing/checking/transformation
# e.g print some message according to verbosity
print("Verbosity inside CheckFile(): " + str(globals()['VERBOSITY']))
return super(CheckFile, self).__call__(string)
def make_parser():
"""The main argument parser."""
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument("-V",
"--verbosity",
default=0,
type=Set_verbosity_level(),
help="Increase output verbosity.",
required=False)
parser.add_argument('-i', '--inputfile',
help="Input file",
type=CheckFile(mode='r'),
required=True)
return parser
if __name__ == '__main__':
myparser = make_parser()
args = myparser.parse_args()
print("Verbosity in Main: " + str(VERBOSITY))
调用此脚本将给出:
$python test.py -i test.bed -V 2
Verbosity inside CheckFile(): 0
Verbosity inside Set_verbosity_level(): 2
Verbosity in Main: 2
答案 0 :(得分:1)
我不知道您可以强制首先读取argparse变量,但是您可以在主要功能中使用命令行解析器中内置的pythons:
import sys
# Your classes here #
if __name__ == '__main__':
verbosity = 0
for i, sysarg in enumerate(sys.argv):
if str(sysarg).strip().lower().replace('-','') in ['v', 'verbose']:
try:
verbosity = sys.argv[i + 1]
except IndexError:
print("No verbosity level specified")
# more code
它不是很优雅,也不是argparse,但这是确保您首先获得详细信息的一种方法。
您还可以更新您的CheckFile
类,使其包含详细程度检查功能:
class CheckFile(argparse.FileType):
"""
Check whatever in the file
"""
def __init__(self, mode='r', **kwargs):
super(CheckFile, self).__init__(mode, **kwargs)
def _check_verbosity(self):
verbosity = 0
for i, sysarg in enumerate(sys.argv):
if str(sysarg).strip().lower().replace('-','') in ['v', 'verbose']:
try:
verbosity = sys.argv[i + 1]
except IndexError:
print("No verbosity level specified")
return verbosity
def __call__(self, string):
# Do whatever processing/checking/transformation
# e.g print some message according to verbosity
print("Verbosity inside CheckFile(): {}".format(self._check_verbosity()))
return super(CheckFile, self).__call__(string)
同样,我知道这并不是您的argparse问题的真正答案,但这是您的问题的解决方案
答案 1 :(得分:1)
argparse
按照列出的顺序处理命令行参数,因此,如果仅交换给定选项的顺序,它将以所需的详细程度输出:
python test.py -V 2 -i test.bed
这将输出:
Verbosity inside Set_verbosity_level(): 2
Verbosity inside CheckFile(): 2
没有其他方法可以告诉argparse
以不同于列出的顺序处理命令行参数。