From ee8abd6c5c59ce2dad8f2f0f78b44fe2e10ab5d9 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sun, 17 Apr 2016 18:20:10 +0300 Subject: [PATCH] Teach the optparse CLI about the parameter aliases it had forgotten in #311 Vaguely refs #390 --- babel/messages/frontend.py | 13 +++++++++++++ tests/messages/test_frontend.py | 10 +++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/babel/messages/frontend.py b/babel/messages/frontend.py index ee1e1855..d190a2c0 100644 --- a/babel/messages/frontend.py +++ b/babel/messages/frontend.py @@ -98,6 +98,12 @@ class Command(_Command): # declared in the base class.) boolean_options = () + #: Option aliases, to retain standalone command compatibility. + #: Distutils does not support option aliases, but optparse does. + #: This maps the distutils argument name to an iterable of aliases + #: that are usable with optparse. + option_aliases = {} + #: Log object. To allow replacement in the script command line runner. log = distutils_log @@ -311,6 +317,12 @@ class extract_messages(Command): ] as_args = 'input-paths' multiple_value_options = ('add-comments', 'keywords') + option_aliases = { + 'keywords': ('--keyword',), + 'mapping-file': ('--mapping',), + 'output-file': ('--output',), + 'strip-comments': ('--strip-comment-tags',), + } def initialize_options(self): self.charset = 'utf-8' @@ -870,6 +882,7 @@ class CommandLineInterface(object): strs = ["--%s" % name] if short: strs.append("-%s" % short) + strs.extend(cmdclass.option_aliases.get(name, ())) if name == as_args: parser.usage += "<%s>" % name elif name in cmdclass.boolean_options: diff --git a/tests/messages/test_frontend.py b/tests/messages/test_frontend.py index 26f6cce1..7488bcc5 100644 --- a/tests/messages/test_frontend.py +++ b/tests/messages/test_frontend.py @@ -1267,8 +1267,12 @@ def configure_distutils_command(cmdline): @pytest.mark.parametrize("split", (False, True)) -def test_extract_keyword_args_384(split): +@pytest.mark.parametrize("arg_name", ("-k", "--keyword", "--keywords")) +def test_extract_keyword_args_384(split, arg_name): # This is a regression test for https://github.com/python-babel/babel/issues/384 + # and it also tests that the rest of the forgotten aliases/shorthands implied by + # https://github.com/python-babel/babel/issues/390 are re-remembered (or rather + # that the mechanism for remembering them again works). kwarg_specs = [ "gettext_noop", @@ -1282,9 +1286,9 @@ def test_extract_keyword_args_384(split): ] if split: # Generate a command line with multiple -ks - kwarg_text = " ".join("-k %s" % kwarg_spec for kwarg_spec in kwarg_specs) + kwarg_text = " ".join("%s %s" % (arg_name, kwarg_spec) for kwarg_spec in kwarg_specs) else: # Generate a single space-separated -k - kwarg_text = "-k \"%s\"" % " ".join(kwarg_specs) + kwarg_text = "%s \"%s\"" % (arg_name, " ".join(kwarg_specs)) # (Both of those invocation styles should be equivalent, so there is no parametrization from here on out) -- 2.47.2