Commits

Jannis Leidel committed ed51b27

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

  • Participants
  • Parent commits 95e2d0b
  • Branches trunk

Comments (0)

Files changed (2)

File docs/index.txt

 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`
 
 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'
             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()