1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Reverted writing the completion script to a tempfile and return script directly. Updated docs with usage instructions.

This commit is contained in:
Jannis Leidel 2009-11-07 19:18:03 +01:00
parent 9ed9ae107e
commit 22bef56391
2 changed files with 30 additions and 30 deletions

View file

@ -254,12 +254,20 @@ option to use pip and virtualenv in your buildouts.
Command line completion
-----------------------
pip comes with completion scripts for bash and zsh, which allow you
to use tab completion of the commands and options. Simply source the
special ``completion`` command, for bash e.g.::
pip comes with support for command line completion in bash and zsh and
allows you tab complete commands and options. To enable it you simply
need copy the required shell script to the your shell startup file
(e.g. ``.profile`` or ``.zprofile``) by running the special ``completion``
command, e.g. for bash::
source `pip completion --bash`
pip completion --bash >> ~/.profile
And for zsh::
source `pip completion --zsh`
pip completion --zsh >> ~/.zprofile
Alternatively, you can use the result of the ``completion`` command
directly with the eval function of you shell, e.g. by adding to your
startup file::
eval `pip completion --bash`

42
pip.py
View file

@ -1185,27 +1185,31 @@ class UnzipCommand(ZipCommand):
UnzipCommand()
BASH_COMPLETION = """#!/bin/sh
BASE_COMPLETION = """
# pip %(shell)s completion start%(script)s# pip %(shell)s completion end
"""
COMPLETION_SCRIPTS = {
'bash': """
_pip_completion()
{
COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
COMP_CWORD=$COMP_CWORD \\
PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip
"""
ZSH_COMPLETION = """#!/bin/sh
""", 'zsh': """
function _pip_completion {
local words cword
read -Ac words
read -cn cword
reply=( $( COMP_WORDS="$words[*]" \
COMP_CWORD=$(( cword-1 )) \
reply=( $( COMP_WORDS="$words[*]" \\
COMP_CWORD=$(( cword-1 )) \\
PIP_AUTO_COMPLETE=1 $words[1] ) )
}
compctl -K _pip_completion pip
"""
}
class CompletionCommand(Command):
name = 'completion'
@ -1228,24 +1232,12 @@ class CompletionCommand(Command):
help='Emit completion code for zsh')
def run(self, options, args):
"""Writes the completion code to a temp file and returns
the path to the file to be used with 'source'"""
if options.shell == 'bash':
print self.temp_completion_file(BASH_COMPLETION)
elif options.shell == 'zsh':
print self.temp_completion_file(ZSH_COMPLETION)
"""Prints the completion code of the given shell"""
if options.shell in ('bash', 'zsh'):
script = COMPLETION_SCRIPTS.get(options.shell, '')
print BASE_COMPLETION % {'script': script, 'shell': options.shell}
else:
print 'You must pass --bash or --zsh'
def temp_completion_file(self, content):
"""Creates a temporary file with the given content"""
temp_file, temp_path = tempfile.mkstemp('-completion', 'pip-')
try:
temp_fp = open(temp_file, 'w')
temp_fp.write(content)
finally:
temp_fp.close()
return temp_path
print 'ERROR: You must pass --bash or --zsh'
CompletionCommand()