让我们来看下面的例子.解析器包含两个参数-inputfile和verbosity. Set_verbosity_level()函数用于将模块级/全局变量(在我的现实生活中为程序包级变量)的值控制为0-4. CheckFile()函数在输入文件中执行测试(实际情况取决于类型).
我想根据详细程度在CheckFile()中打印消息.问题是argparse在Set_verbosity_level()之前调用CheckFile(),因此详细程度级别始终为0 /默认值在CheckFile中…
所以我的问题是是否有任何解决方案可以迫使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
最佳答案
我不知道您可以强制首先读取argparse变量,但可以在主要功能中使用命令行解析器中内置的python:
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,但这是确保您首先获得详细信息的一种方法.
class CheckFile(argparse.FileType):
"""
Check whatever in the file
"""
def __init__(self,**kwargs):
super(CheckFile,**kwargs)
def _check_verbosity(self):
verbosity = 0
for i,sysarg in enumerate(sys.argv):
if str(sysarg).strip().lower().replace('-','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问题的真正答案,但这是您的问题的解决方案