Path: blob/master/venv/Lib/site-packages/pip/_internal/commands/completion.py
811 views
# The following comment should be removed at some point in the future.1# mypy: disallow-untyped-defs=False23from __future__ import absolute_import45import sys6import textwrap78from pip._internal.cli.base_command import Command9from pip._internal.utils.misc import get_prog1011BASE_COMPLETION = """12# pip {shell} completion start{script}# pip {shell} completion end13"""1415COMPLETION_SCRIPTS = {16'bash': """17_pip_completion()18{{19COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\20COMP_CWORD=$COMP_CWORD \\21PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )22}}23complete -o default -F _pip_completion {prog}24""",25'zsh': """26function _pip_completion {{27local words cword28read -Ac words29read -cn cword30reply=( $( COMP_WORDS="$words[*]" \\31COMP_CWORD=$(( cword-1 )) \\32PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))33}}34compctl -K _pip_completion {prog}35""",36'fish': """37function __fish_complete_pip38set -lx COMP_WORDS (commandline -o) ""39set -lx COMP_CWORD ( \\40math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\41)42set -lx PIP_AUTO_COMPLETE 143string split \\ -- (eval $COMP_WORDS[1])44end45complete -fa "(__fish_complete_pip)" -c {prog}46""",47}484950class CompletionCommand(Command):51"""A helper command to be used for command completion."""5253ignore_require_venv = True5455def __init__(self, *args, **kw):56super(CompletionCommand, self).__init__(*args, **kw)5758cmd_opts = self.cmd_opts5960cmd_opts.add_option(61'--bash', '-b',62action='store_const',63const='bash',64dest='shell',65help='Emit completion code for bash')66cmd_opts.add_option(67'--zsh', '-z',68action='store_const',69const='zsh',70dest='shell',71help='Emit completion code for zsh')72cmd_opts.add_option(73'--fish', '-f',74action='store_const',75const='fish',76dest='shell',77help='Emit completion code for fish')7879self.parser.insert_option_group(0, cmd_opts)8081def run(self, options, args):82"""Prints the completion code of the given shell"""83shells = COMPLETION_SCRIPTS.keys()84shell_options = ['--' + shell for shell in sorted(shells)]85if options.shell in shells:86script = textwrap.dedent(87COMPLETION_SCRIPTS.get(options.shell, '').format(88prog=get_prog())89)90print(BASE_COMPLETION.format(script=script, shell=options.shell))91else:92sys.stderr.write(93'ERROR: You must pass {}\n' .format(' or '.join(shell_options))94)959697