Skip to content

Commit 97c1d18

Browse files
committed
Teach the optparse CLI about the parameter aliases it had forgotten in python-babel#311
Vaguely refs python-babel#390
1 parent c313325 commit 97c1d18

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

babel/messages/frontend.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class Command(_Command):
9898
# declared in the base class.)
9999
boolean_options = ()
100100

101+
#: Option aliases, to retain standalone command compatibility.
102+
#: Distutils does not support option aliases, but optparse does.
103+
#: This maps the distutils argument name to an iterable of aliases
104+
#: that are usable with optparse.
105+
option_aliases = {}
106+
101107
#: Log object. To allow replacement in the script command line runner.
102108
log = distutils_log
103109

@@ -311,6 +317,12 @@ class extract_messages(Command):
311317
]
312318
as_args = 'input-paths'
313319
multiple_value_options = ('add-comments', 'keywords')
320+
option_aliases = {
321+
'keywords': ('--keyword',),
322+
'mapping-file': ('--mapping',),
323+
'output': ('--output',),
324+
'strip-comments': ('--strip-comment-tags',),
325+
}
314326

315327
def initialize_options(self):
316328
self.charset = 'utf-8'
@@ -870,6 +882,7 @@ def _configure_command(self, cmdname, argv):
870882
strs = ["--%s" % name]
871883
if short:
872884
strs.append("-%s" % short)
885+
strs.extend(cmdclass.option_aliases.get(name, ()))
873886
if name == as_args:
874887
parser.usage += "<%s>" % name
875888
elif name in cmdclass.boolean_options:

tests/messages/test_frontend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,12 @@ def configure_distutils_command(cmdline):
12671267

12681268

12691269
@pytest.mark.parametrize("split", (False, True))
1270-
def test_extract_keyword_args_384(split):
1270+
@pytest.mark.parametrize("arg_name", ("-k", "--keyword", "--keywords"))
1271+
def test_extract_keyword_args_384(split, arg_name):
12711272
# This is a regression test for https://github.com/python-babel/babel/issues/384
1273+
# and it also tests that the rest of the forgotten aliases/shorthands implied by
1274+
# https://github.com/python-babel/babel/issues/390 are re-remembered (or rather
1275+
# that the mechanism for remembering them again works).
12721276

12731277
kwarg_specs = [
12741278
"gettext_noop",
@@ -1282,9 +1286,9 @@ def test_extract_keyword_args_384(split):
12821286
]
12831287

12841288
if split: # Generate a command line with multiple -ks
1285-
kwarg_text = " ".join("-k %s" % kwarg_spec for kwarg_spec in kwarg_specs)
1289+
kwarg_text = " ".join("%s %s" % (arg_name, kwarg_spec) for kwarg_spec in kwarg_specs)
12861290
else: # Generate a single space-separated -k
1287-
kwarg_text = "-k \"%s\"" % " ".join(kwarg_specs)
1291+
kwarg_text = "%s \"%s\"" % (arg_name, " ".join(kwarg_specs))
12881292

12891293
# (Both of those invocation styles should be equivalent, so there is no parametrization from here on out)
12901294

0 commit comments

Comments
 (0)