python argparse -- 自定义错误信息
python argparse -- customizing error messages
我想为使用 argparse
库的命令行程序中的特定使用错误生成自定义错误消息。我知道我可以通过子类化 argparse.ArgumentParser
:
来覆盖错误的一般表示
class HelpParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
sys.exit(2)
parser = HelpParser(... ...)
args = parser.parse_args()
但是当调用我的error
方法时,message
已经被库格式化了。例如,
> python prog.py old stuff
usage: prog [-h] {hot,cold,rain,snow} ...
prog: error: argument subparser: invalid choice: 'old' (choose from u'hot', u'cold', u'rain', u'snow')
如何更改 error:
之后的内容的显示方式,例如
usage: prog [-h] {hot,cold,rain,snow} ...
error: 'old' is not a valid option. select from 'hot', 'cold', 'rain', 'snow'
?
查看 the source code,您可以通过覆盖此方法来覆盖 this particular
错误消息:
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)
问题是,如果您想覆盖所有可能的错误消息,您基本上必须重新编写此模块。所有各种错误消息都经过预先格式化 - 在检测每种错误类型的各种方法中。
添加到@Gerrat 的回答中,_
函数被导入为
from gettext import gettext as _, ngettext
它正在使用 gettext
模块 https://docs.python.org/2/library/gettext.html 来启用国际化。我不熟悉那个模块,但大概你可以用它来执行一定量的英语释义。但也许没有你想要的那么多。
错误消息会经过多个级别。 _check_values
等函数编写基本消息。 ArgumentError
添加参数名称(在您的示例中为 argument subparser:
)。 parser.error
添加 usage
和 prog
。 parser.exit
负责 sys.exit
步骤。
def error(self, message):
...
self.print_usage(_sys.stderr)
args = {'prog': self.prog, 'message': message}
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
我想为使用 argparse
库的命令行程序中的特定使用错误生成自定义错误消息。我知道我可以通过子类化 argparse.ArgumentParser
:
class HelpParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
sys.exit(2)
parser = HelpParser(... ...)
args = parser.parse_args()
但是当调用我的error
方法时,message
已经被库格式化了。例如,
> python prog.py old stuff
usage: prog [-h] {hot,cold,rain,snow} ...
prog: error: argument subparser: invalid choice: 'old' (choose from u'hot', u'cold', u'rain', u'snow')
如何更改 error:
之后的内容的显示方式,例如
usage: prog [-h] {hot,cold,rain,snow} ...
error: 'old' is not a valid option. select from 'hot', 'cold', 'rain', 'snow'
?
查看 the source code,您可以通过覆盖此方法来覆盖 this particular
错误消息:
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)
问题是,如果您想覆盖所有可能的错误消息,您基本上必须重新编写此模块。所有各种错误消息都经过预先格式化 - 在检测每种错误类型的各种方法中。
添加到@Gerrat 的回答中,_
函数被导入为
from gettext import gettext as _, ngettext
它正在使用 gettext
模块 https://docs.python.org/2/library/gettext.html 来启用国际化。我不熟悉那个模块,但大概你可以用它来执行一定量的英语释义。但也许没有你想要的那么多。
错误消息会经过多个级别。 _check_values
等函数编写基本消息。 ArgumentError
添加参数名称(在您的示例中为 argument subparser:
)。 parser.error
添加 usage
和 prog
。 parser.exit
负责 sys.exit
步骤。
def error(self, message):
...
self.print_usage(_sys.stderr)
args = {'prog': self.prog, 'message': message}
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)