我爲需要遵循確切格式的參數定義了自定義正則表達式類型。我使用了來自另一篇文章(regex custom type)的代碼,該代碼非常有用。我的問題是,我正在寫單元測試,我期望正則表達式失敗並試圖聲明argparse.ArgumentError
被提出(assertRaises(argparse.ArgumentError, parser.parse_args(inargs.split()))
)。問題是argparse似乎捕獲了ArgumentError並拋出了一個通用錯誤,使我無法驗證失敗的原因。我錯過了什麼嗎?Python:引發argparse.ArgumentError後,argparse引發一般錯誤
這裏是回溯:
Error
Traceback (most recent call last):
File "/Users/markebbert/PyCharmProjects/newproject/unittests.py", line 203, in test_set_operation_parameter
self.assertRaises(argparse.ArgumentError, parser.parse_args(inargs.split()))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1688, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1727, in parse_known_args
self.error(str(err))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2347, in error
self.exit(2, _('%s: error: %s\n') % (self.prog, message))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2335, in exit
_sys.exit(status)
SystemExit: 2
這是我的定義的自定義類型和解析器代碼:
class RegexValidator(object):
"""
Performs regular expression match on value.
If match fails an ArgumentError is raised
"""
def __init__(self, pattern, statement=None):
self.pattern = re.compile(pattern)
self.statement = statement
if not self.statement:
self.statement = "must match pattern %s" % self.pattern
def __call__(self, string):
match = self.pattern.search(string)
if not match:
raise argparse.ArgumentError(None, self.statement)
return string
operatorRV = RegexValidator(
"^((\w+)=)?[iIuUcC]\[(\w+(\[\w+(,\w+)*\])?)(:\w+(\[\w+(,\w+)*\])?)*\]$",
"Set operations must conform to...")
parser = argparse.ArgumentParser(
description='Compare variants across individuals',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
group.add_argument('-s', '--set-operation', dest='operation', nargs='+',
type=operatorRV,
help="blah.")
這裏是單元測試:
# Fail for ending colon
inargs = "-s out=i[one[id1]:]"
self.assertRaises(argparse.ArgumentError, parser.parse_args(inargs.split()))
Argparse提出了'SystemExit',因爲這是一個命令行工具是應該做的;錯誤時返回系統退出代碼。 –
你能詳細說明一下嗎?因爲它是一個python模塊,所以我不會考慮argparse命令行工具。與任何模塊一樣,我希望能夠「捕捉」argparse的錯誤並自己處理它們。 –
這是一個用來編譯*命令行工具的庫。我已經添加了答案。 –