Debian Codegraph @

examples/click-completion-command

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 from __future__ import print_function, absolute_import
5
6 import click_completion
7 import os
8
9 import click
10
11 import click_completion.core
12
13
14 def custom_startswith(string, incomplete):
15 """A custom completion matching that supports case insensitive matching"""
16 if os.environ.get('_CLICK_COMPLETION_COMMAND_CASE_INSENSITIVE_COMPLETE'):
17 string = string.lower()
18 incomplete = incomplete.lower()
19 return string.startswith(incomplete)
20
21
22 click_completion.core.startswith = custom_startswith
23 click_completion.init()
24
25
26 cmd_help = """Shell completion for click-completion-command
27
28 Available shell types:
29
30 \b
31 %s
32
33 Default type: auto
34 """ % "\n ".join('{:<12} {}'.format(k, click_completion.core.shells[k]) for k in sorted(
35 click_completion.core.shells.keys()))
36
37
38 @click.group(help=cmd_help)
39 def completion():
40 pass
41
42
43 @completion.command()
44 @click.option('-i', '--case-insensitive/--no-case-insensitive', help="Case insensitive completion")
45 @click.argument('shell', required=False, type=click_completion.DocumentedChoice(click_completion.core.shells))
46 def show(shell, case_insensitive):
47 """Show the click-completion-command completion code"""
48 extra_env = {'_CLICK_COMPLETION_COMMAND_CASE_INSENSITIVE_COMPLETE': 'ON'} if case_insensitive else {}
49 click.echo(click_completion.core.get_code(shell, extra_env=extra_env))
50
51
52 @completion.command()
53 @click.option('--append/--overwrite', help="Append the completion code to the file", default=None)
54 @click.option('-i', '--case-insensitive/--no-case-insensitive', help="Case insensitive completion")
55 @click.argument('shell', required=False, type=click_completion.DocumentedChoice(click_completion.core.shells))
56 @click.argument('path', required=False)
57 def install(append, case_insensitive, shell, path):
58 """Install the click-completion-command completion"""
59 extra_env = {'_CLICK_COMPLETION_COMMAND_CASE_INSENSITIVE_COMPLETE': 'ON'} if case_insensitive else {}
60 shell, path = click_completion.core.install(shell=shell, path=path, append=append, extra_env=extra_env)
61 click.echo('%s completion installed in %s' % (shell, path))
62
63
64 if __name__ == "__main__":
65 completion()