Skip to content

Commit 0a2b3e7

Browse files
committed
Improve shell-completion wrappers
1 parent f51cea4 commit 0a2b3e7

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

mike/arguments.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from argparse import *
2+
3+
_ArgumentParser = ArgumentParser
4+
_Action = Action
5+
6+
7+
# Add some simple wrappers to make it easier to specify shell-completion
8+
# behaviors.
9+
10+
def _add_complete(argument, complete):
11+
if complete is not None:
12+
argument.complete = complete
13+
return argument
14+
15+
16+
class Action(_Action):
17+
def __init__(self, *args, complete=None, **kwargs):
18+
super().__init__(*args, **kwargs)
19+
_add_complete(self, complete)
20+
21+
22+
class ArgumentParser(_ArgumentParser):
23+
@staticmethod
24+
def _wrap_complete(action):
25+
def wrapper(*args, complete=None, **kwargs):
26+
return _add_complete(action(*args, **kwargs), complete)
27+
28+
return wrapper
29+
30+
def __init__(self, *args, **kwargs):
31+
super().__init__(*args, **kwargs)
32+
for k, v in self._registries['action'].items():
33+
self._registries['action'][k] = self._wrap_complete(v)

mike/driver.py

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import argparse
21
import os
32
import sys
43

5-
from . import commands
4+
from . import arguments, commands
65
from . import git_utils
76
from . import mkdocs_utils
87
from .app_version import version as app_version
@@ -56,23 +55,6 @@
5655
"""
5756

5857

59-
class CompletingArgumentParser(argparse.ArgumentParser):
60-
@staticmethod
61-
def _wrap_complete(action):
62-
def wrapper(*args, complete=None, **kwargs):
63-
argument = action(*args, **kwargs)
64-
if complete is not None:
65-
argument.complete = complete
66-
return argument
67-
68-
return wrapper
69-
70-
def __init__(self, *args, **kwargs):
71-
super().__init__(*args, **kwargs)
72-
for k, v in self._registries['action'].items():
73-
self._registries['action'][k] = self._wrap_complete(v)
74-
75-
7658
def add_git_arguments(parser, *, commit=True, prefix=True):
7759
# Add this whenever we add git arguments since we pull the remote and
7860
# branch from mkdocs.yml.
@@ -245,7 +227,7 @@ def generate_completion(parser, args):
245227

246228

247229
def main():
248-
parser = CompletingArgumentParser(prog='mike', description=description)
230+
parser = arguments.ArgumentParser(prog='mike', description=description)
249231
subparsers = parser.add_subparsers(metavar='COMMAND')
250232
subparsers.required = True
251233

@@ -345,7 +327,7 @@ def main():
345327
'help', help='show this help message and exit', add_help=False
346328
)
347329
help_p.set_defaults(func=help)
348-
help_p.add_argument('subcommand', metavar='CMD', nargs=argparse.REMAINDER,
330+
help_p.add_argument('subcommand', metavar='CMD', nargs=arguments.REMAINDER,
349331
help='subcommand to request help for')
350332

351333
completion_p = subparsers.add_parser(

test/unit/test_arguments.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from unittest import TestCase
2+
3+
from mike import arguments
4+
5+
6+
class TestParser(TestCase):
7+
def test_complete(self):
8+
p = arguments.ArgumentParser()
9+
arg = p.add_argument('--arg', complete='file')
10+
self.assertEqual(arg.complete, 'file')
11+
12+
def test_complete_group(self):
13+
p = arguments.ArgumentParser()
14+
g = p.add_argument_group()
15+
arg = g.add_argument('--arg', complete='file')
16+
self.assertEqual(arg.complete, 'file')
17+
18+
def test_complete_action(self):
19+
class MyAction(arguments.Action):
20+
def __call__(self, parser, namespace, values, option_string=None):
21+
setattr(namespace, self.dest, values.upper())
22+
23+
p = arguments.ArgumentParser()
24+
arg = p.add_argument('--arg', action=MyAction, complete='file')
25+
self.assertEqual(arg.complete, 'file')
26+
self.assertEqual(p.parse_args(['--arg=foo']),
27+
arguments.Namespace(arg='FOO'))

0 commit comments

Comments
 (0)