Skip to content

Commit 383342e

Browse files
committed
Add support for shell-completion
1 parent 5b205da commit 383342e

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
## v1.1.0 (in progress)
44

5+
### New features
56
- Add support for [`!ENV`][mkdocs-env] and [`INHERIT`][mkdocs-inherit] in
67
`mkdocs.yml`
8+
- Add `mike dump-completion` to generate shell-completion functions
79

810
[mkdocs-env]: https://www.mkdocs.org/user-guide/configuration/#environment-variables
911
[mkdocs-inherit]: https://www.mkdocs.org/user-guide/configuration/#configuration-inheritance

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ is what you might expect:
3939
pip install mike
4040
```
4141

42+
Once you've installed mike, you might also want to set up shell-completion for
43+
it. If you have [shtab][shtab] installed, you can do this with
44+
`mike dump-completion`, which will print the shell-completion code for your
45+
shell. For more details on how to set this up, consult shtab's
46+
[documentation][shtab-setup].
47+
4248
## Usage
4349

4450
### Initialization
@@ -261,4 +267,6 @@ This project is licensed under the [BSD 3-clause license](LICENSE).
261267
[bfg9000]: https://jimporter.github.io/bfg9000
262268
[material-insiders]: https://squidfunk.github.io/mkdocs-material/insiders/
263269
[setuptools]: https://pythonhosted.org/setuptools/
270+
[shtab]: https://github.com/iterative/shtab
271+
[shtab-setup]: https://github.com/iterative/shtab#cli-usage
264272
[jinja]: https://jinja.palletsprojects.com/

mike/driver.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import os
23
import sys
34

45
from . import commands
@@ -49,6 +50,10 @@
4950
Start the development server, serving pages from the target branch.
5051
"""
5152

53+
dump_completion_desc = """
54+
Print shell-completion functions.
55+
"""
56+
5257

5358
def add_git_arguments(parser, *, commit=True, prefix=True):
5459
# Add this whenever we add git arguments since we pull the remote and
@@ -211,6 +216,15 @@ def help(parser, args):
211216
parser.parse_args(args.subcommand + ['--help'])
212217

213218

219+
def dump_completion(parser, args):
220+
try:
221+
import shtab
222+
print(shtab.complete(parser, shell=args.shell))
223+
except ImportError:
224+
print('shtab not found; install via `pip install shtab`')
225+
return 1
226+
227+
214228
def main():
215229
parser = argparse.ArgumentParser(prog='mike', description=description)
216230
subparsers = parser.add_subparsers(metavar='COMMAND')
@@ -315,6 +329,16 @@ def main():
315329
help_p.add_argument('subcommand', metavar='CMD', nargs=argparse.REMAINDER,
316330
help='subcommand to request help for')
317331

332+
completion_p = subparsers.add_parser(
333+
'dump-completion', description=dump_completion_desc,
334+
help='print shell completion script'
335+
)
336+
completion_p.set_defaults(func=dump_completion)
337+
shell = (os.path.basename(os.environ['SHELL'])
338+
if 'SHELL' in os.environ else None)
339+
completion_p.add_argument('-s', '--shell', metavar='SHELL', default=shell,
340+
help='shell type (default: %(default)s)')
341+
318342
args = parser.parse_args()
319343
try:
320344
return args.func(parser, args)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def distribution_files(self):
9696

9797
install_requires=(['mkdocs >= 1.0', 'jinja2', 'pyyaml >= 5.1', 'verspec']),
9898
extras_require={
99-
'dev': ['coverage', 'flake8 >= 3.0'],
100-
'test': ['coverage', 'flake8 >= 3.0'],
99+
'dev': ['coverage', 'flake8 >= 3.0', 'shtab'],
100+
'test': ['coverage', 'flake8 >= 3.0', 'shtab'],
101101
},
102102

103103
entry_points={

test/integration/test_help.py renamed to test/integration/test_command_line.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ def test_help_subcommand(self):
1717
def test_help_subcommand_extra(self):
1818
output = assertPopen(['mike', 'help', 'deploy', '--rebase'])
1919
self.assertRegex(output, r'^usage: mike deploy')
20+
21+
22+
class DumpCompletionTest(unittest.TestCase):
23+
def test_completion(self):
24+
output = assertPopen(['mike', 'dump-completion', '-sbash'])
25+
self.assertRegex(output, r'^#!/usr/bin/env bash')

0 commit comments

Comments
 (0)