Skip to content

Commit

Permalink
Fix using a dict in alt= parameter of clize.run (#102)
Browse files Browse the repository at this point in the history
* Fix using a dict in alt= parameter

* fix uncovered test mocks
  • Loading branch information
epsy committed Jul 7, 2023
1 parent 162f849 commit 83bf033
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
10 changes: 5 additions & 5 deletions clize/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, fn, owner=None, alt=(), extra=(),
update_wrapper(self, fn)
self.func = fn
self.owner = owner
self.alt = util.maybe_iter(alt)
self.alt = util.dict_from_names(alt)
self.extra = extra
self.help_names = help_names
self.help_aliases = [util.name_py2cli(s, kw=True) for s in help_names]
Expand All @@ -103,7 +103,7 @@ def _key(self):
return (
self.func,
self.owner,
tuple(self.alt),
tuple(self.alt.items()),
tuple(self.extra),
tuple(self.help_names),
tuple(self.help_aliases),
Expand Down Expand Up @@ -215,7 +215,7 @@ def helper(self):
@util.property_once
def signature(self):
"""The `.parser.CliSignature` object used to parse arguments."""
extra = itertools.chain(self._process_alt(self.alt), self.extra)
extra = itertools.chain(self._process_alt(), self.extra)
with self._move_warnings_to_func():
return parser.CliSignature.from_signature(
self.func_signature,
Expand All @@ -242,14 +242,14 @@ def _move_warnings_to_func(self):
registry = module_globals.setdefault("__warningregistry__", {})
warnings.warn_explicit(warning.message, warning.category, filename, lineno, module, registry, module_globals)

def _process_alt(self, alt):
def _process_alt(self):
if self.help_names:
p = parser.FallbackCommandParameter(
func=self.helper.cli, undocumented=self.hide_help,
aliases=self.help_aliases)
yield p

for name, func in util.dict_from_names(alt).items():
for name, func in self.alt.items():
func = self.get_cli(func)
param = parser.AlternateCommandParameter(
undocumented=False, func=func,
Expand Down
26 changes: 26 additions & 0 deletions clize/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,32 @@ def func2(): return '2'
self.assertFalse(stderr.getvalue())
self.assertEqual(stdout.getvalue(), '2\n')

def test_alt_list(self):
def base(): return '0'
def func1(): return '1'
def func2(): return '2'
alt = [func1, func2]
self.crun(base, alt=alt, args=['test'])
stdout, stderr = self.crun(base, alt=alt, args=['test', '--func1'])
self.assertFalse(stderr.getvalue())
self.assertEqual(stdout.getvalue(), '1\n')
stdout, stderr = self.crun(base, alt=alt, args=['test', '--func2'])
self.assertFalse(stderr.getvalue())
self.assertEqual(stdout.getvalue(), '2\n')

def test_alt_dict(self):
def base(): return '0'
def func1(): return '1'
def func2(): return '2'
alt = {'1': func1, '2': func2}
self.crun(base, alt=alt, args=['test'])
stdout, stderr = self.crun(base, alt=alt, args=['test', '-1'])
self.assertFalse(stderr.getvalue())
self.assertEqual(stdout.getvalue(), '1\n')
stdout, stderr = self.crun(base, alt=alt, args=['test', '-2'])
self.assertFalse(stderr.getvalue())
self.assertEqual(stdout.getvalue(), '2\n')

def test_disable_help(self):
def func1(): raise NotImplementedError
stdout, stderr = self.crun(
Expand Down
7 changes: 7 additions & 0 deletions docs/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
Release notes
=============

.. _v5.0.1:

5.0.1 (2023-07-06)
------------------

* Fixed ``run(..., alt={...})`` not working correctly with dicts

.. _v5.0:
.. _v5.0.0:

Expand Down

0 comments on commit 83bf033

Please sign in to comment.