comparison lib/python3.8/site-packages/pip/_internal/commands/completion.py @ 0:9e54283cc701 draft

"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author guerler
date Mon, 27 Jul 2020 03:47:31 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9e54283cc701
1 # The following comment should be removed at some point in the future.
2 # mypy: disallow-untyped-defs=False
3
4 from __future__ import absolute_import
5
6 import sys
7 import textwrap
8
9 from pip._internal.cli.base_command import Command
10 from pip._internal.utils.misc import get_prog
11
12 BASE_COMPLETION = """
13 # pip %(shell)s completion start%(script)s# pip %(shell)s completion end
14 """
15
16 COMPLETION_SCRIPTS = {
17 'bash': """
18 _pip_completion()
19 {
20 COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
21 COMP_CWORD=$COMP_CWORD \\
22 PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
23 }
24 complete -o default -F _pip_completion %(prog)s
25 """,
26 'zsh': """
27 function _pip_completion {
28 local words cword
29 read -Ac words
30 read -cn cword
31 reply=( $( COMP_WORDS="$words[*]" \\
32 COMP_CWORD=$(( cword-1 )) \\
33 PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))
34 }
35 compctl -K _pip_completion %(prog)s
36 """,
37 'fish': """
38 function __fish_complete_pip
39 set -lx COMP_WORDS (commandline -o) ""
40 set -lx COMP_CWORD ( \\
41 math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
42 )
43 set -lx PIP_AUTO_COMPLETE 1
44 string split \\ -- (eval $COMP_WORDS[1])
45 end
46 complete -fa "(__fish_complete_pip)" -c %(prog)s
47 """,
48 }
49
50
51 class CompletionCommand(Command):
52 """A helper command to be used for command completion."""
53
54 ignore_require_venv = True
55
56 def __init__(self, *args, **kw):
57 super(CompletionCommand, self).__init__(*args, **kw)
58
59 cmd_opts = self.cmd_opts
60
61 cmd_opts.add_option(
62 '--bash', '-b',
63 action='store_const',
64 const='bash',
65 dest='shell',
66 help='Emit completion code for bash')
67 cmd_opts.add_option(
68 '--zsh', '-z',
69 action='store_const',
70 const='zsh',
71 dest='shell',
72 help='Emit completion code for zsh')
73 cmd_opts.add_option(
74 '--fish', '-f',
75 action='store_const',
76 const='fish',
77 dest='shell',
78 help='Emit completion code for fish')
79
80 self.parser.insert_option_group(0, cmd_opts)
81
82 def run(self, options, args):
83 """Prints the completion code of the given shell"""
84 shells = COMPLETION_SCRIPTS.keys()
85 shell_options = ['--' + shell for shell in sorted(shells)]
86 if options.shell in shells:
87 script = textwrap.dedent(
88 COMPLETION_SCRIPTS.get(options.shell, '') % {
89 'prog': get_prog(),
90 }
91 )
92 print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
93 else:
94 sys.stderr.write(
95 'ERROR: You must pass %s\n' % ' or '.join(shell_options)
96 )