-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from argparse import * | ||
|
||
_ArgumentParser = ArgumentParser | ||
_Action = Action | ||
|
||
|
||
# Add some simple wrappers to make it easier to specify shell-completion | ||
# behaviors. | ||
|
||
def _add_complete(argument, complete): | ||
if complete is not None: | ||
argument.complete = complete | ||
return argument | ||
|
||
|
||
class Action(_Action): | ||
def __init__(self, *args, complete=None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
_add_complete(self, complete) | ||
|
||
|
||
class ArgumentParser(_ArgumentParser): | ||
@staticmethod | ||
def _wrap_complete(action): | ||
def wrapper(*args, complete=None, **kwargs): | ||
return _add_complete(action(*args, **kwargs), complete) | ||
|
||
return wrapper | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
for k, v in self._registries['action'].items(): | ||
self._registries['action'][k] = self._wrap_complete(v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from unittest import TestCase | ||
|
||
from mike import arguments | ||
|
||
|
||
class TestParser(TestCase): | ||
def test_complete(self): | ||
p = arguments.ArgumentParser() | ||
arg = p.add_argument('--arg', complete='file') | ||
self.assertEqual(arg.complete, 'file') | ||
|
||
def test_complete_group(self): | ||
p = arguments.ArgumentParser() | ||
g = p.add_argument_group() | ||
arg = g.add_argument('--arg', complete='file') | ||
self.assertEqual(arg.complete, 'file') | ||
|
||
def test_complete_action(self): | ||
class MyAction(arguments.Action): | ||
def __call__(self, parser, namespace, values, option_string=None): | ||
setattr(namespace, self.dest, values.upper()) | ||
|
||
p = arguments.ArgumentParser() | ||
arg = p.add_argument('--arg', action=MyAction, complete='file') | ||
self.assertEqual(arg.complete, 'file') | ||
self.assertEqual(p.parse_args(['--arg=foo']), | ||
arguments.Namespace(arg='FOO')) |