Added localization support to optparse error messages.

The optparse module does not seem to support this by default. The solution
is not very pretty, but it works.
This commit is contained in:
Adam Waldenberg 2013-07-10 03:11:15 +02:00
parent 0d2bf9b0a8
commit a05403df60
2 changed files with 22 additions and 3 deletions

View File

@ -108,7 +108,7 @@ def __handle_version__(__option__, __opt_str__, __value__, __parser__):
sys.exit(0)
def main():
parser = optparse.OptionParser(add_help_option=False)
parser = optval.OptionParser(add_help_option=False)
try:
parser.add_option("-c", action="store_true", dest="checkout_missing")
@ -145,8 +145,9 @@ def main():
#We need the repo above to be set before we read the git config.
config.init(__run__)
except (format.InvalidFormatError, optval.InvalidOptionArgument) as msg:
print(sys.argv[0], "\b:", unicode(msg))
except (format.InvalidFormatError, optval.InvalidOptionArgument, optval.OptionParsingError) as msg:
print(sys.argv[0], "\b:", end=" ")
print(msg)
print(_("Try `{0} --help' for more information.").format(sys.argv[0]))
sys.exit(2)

View File

@ -18,11 +18,15 @@
from __future__ import unicode_literals
import optparse
import sys
class InvalidOptionArgument(Exception):
pass
class OptionParsingError(RuntimeError):
pass
def __handle_boolean_argument__(option, __opt_str__, value, parser, *__args__, **kwargs):
if isinstance(value, bool):
return value
@ -59,3 +63,17 @@ def add_option(parser, *args, **kwargs):
sys.argv.insert(i + 1, "true")
parser.add_option(*args, **kwargs)
class OptionParser(optparse.OptionParser):
def error(self, msg):
if msg.find("requires") != -1:
variable = msg.split()[0]
raise OptionParsingError(_("option '{0}' requires an argument").format(variable))
else:
variable = msg.split()[-1]
if variable[1] == "-":
raise OptionParsingError("unrecognized option '{0}'".format(variable))
else:
raise OptionParsingError("invalid option -- '{0}'".format(variable[1:]))
raise OptionParsingError("invalid command-line options")