Skip to content

Z Shell Completion Script

rshhh edited this page Mar 7, 2011 · 15 revisions

Z Shell Completion Script

This script will help you if you are using zsh for your shell.
This handles all options/commands documented, and optimizes the file glob behavior for installation from local files or directories.
Note that this is only for zsh, you can't use this on bash or any other shells.

Installation

1. Create a file named _cpanm in one of your $fpath directory.

The Completion/Unix directory might be a good place to put it in. (Unix is a directory for general external commands' scripts.)

For Ubuntu, it should be located at:
/usr/share/zsh/functions/Completion/Unix/

Note that you can't change the name of the script, it should be '_' + "command's name".

2. Paste the following code into _cpanm:

#compdef cpanm

local arguments curcontext="$curcontext"
typeset -A opt_args


arguments=(

# Commands
#  '(--install -i)'{--install,-i}'[Installs the modules]'
  '(- :)--self-upgrade[Upgrades itself]'
  '(- :)--info[Displays distribution info on CPAN]'
  '(--installdeps)--installdeps[Only install dependencies]'
  '(--look)--look[Download/unpack the distribution and then open the directory with your shell]'
  '(- :)'{--help,-h}'[Displays help information]'
  '(- :)'{--version,-V}'[Displays software version]'

# Options
  {--force,-f}'[Force install]'
  {--notest,-n}'[Do not run unit tests]'
  {--sudo,-S}'[sudo to run install commands]'
  '(-v --verbose --quiet -q)'{--verbose,-v}'[Turns on chatty output]'
  '(-q --quiet --verbose -v)'{--quiet,-q}'[Turns off all output]'
  {--local-lib,-l}'[Specify the install base to install modules]'
  {--local-lib-contained,-L}'[Specify the install base to install all non-core modules]'
  '--mirror[Specify the base URL for the mirror (e.g. http://cpan.cpantesters.org/)]:URLs:_urls'
  '--mirror-only[Use the mirror\\''s index file instead of the CPAN Meta DB]'
  '--prompt[Prompt when configure/build/test fails]'
  '--reinstall[Reinstall the distribution even if you already have the latest version installed]'
  '--interactive[Turns on interactive configure]'
#  '--uninst-shadows[Uninstalls the shadow files of the distribution that you\\''re installing]'

  '--auto-cleanup[Number of days that cpanm\\''s work directories expire in. Defaults to 7]'

  # Note: Normally with "--lwp", "--wget" and "--curl" options set to true (which is the default) cpanm tries LWP,
  #            Wget, cURL and HTTP::Tiny (in that order) and uses the first one available.
  # (So that the exclusions are not enabled here for the completion)
  '(--lwp)--lwp[Use LWP module to download stuff]'
  '(--wget)--wget[Use GNU Wget (if available) to download stuff]'
  '(--curl)--curl[Use cURL (if available) to download stuff]'

# Other completions
  '*:Local directory or archive:_files -/ -g "*.(tar.gz|tgz|tar.bz2|zip)(-.)"'
  #  '*::args: _normal' # this looks for default files (any files)
)
_arguments -s $arguments \
  && return 0

return 1 

3. Restart your shell

That's it!

Other Information

This script focuses on basic completion, so that this does not perform any complicated functions.
All arguments, commands, and options listed in the POD is supported.
See also: cpan-listchanges:Z Shell Completion Script

Reference

Clone this wiki locally