bashcomplete.rst
This file
Path
docs/bashcomplete.rst
105 lines · 3.5 KiB
Last changed by
Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
History of this fileTake it
1Bash Complete2=============34.. versionadded:: 2.056As of Click 2.0, there is built-in support for Bash completion for7any Click script. There are certain restrictions on when this completion8is available, but for the most part it should just work.910Limitations11-----------1213Bash completion is only available if a script has been installed properly,14and not executed through the ``python`` command. For information about15how to do that, see :ref:`setuptools-integration`. Also, Click currently16only supports completion for Bash. Zsh support is available through Zsh's17bash completion compatibility mode.1819What it Completes20-----------------2122Generally, the Bash completion support will complete subcommands, options23and any option or argument values where the type is click.Choice.24Subcommands and choices are always listed whereas options only if at25least a dash has been provided. Example::2627 $ repo <TAB><TAB>28 clone commit copy delete setuser29 $ repo clone -<TAB><TAB>30 --deep --help --rev --shallow -r3132Additionally, custom suggestions can be provided for arguments and options with33the ``autocompletion`` parameter. ``autocompletion`` should a callback function34that returns a list of strings. This is useful when the suggestions need to be35dynamically generated at bash completion time. The callback function will be36passed 3 keyword arguments:3738- ``ctx`` - The current click context.39- ``args`` - The list of arguments passed in.40- ``incomplete`` - The partial word that is being completed, as a string. May41 be an empty string ``''`` if no characters have been entered yet.4243Here is an example of using a callback function to generate dynamic suggestions:4445.. click:example::4647 import os4849 def get_env_vars(ctx, args, incomplete):50 return os.environ.keys()5152 @click.command()53 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)54 def cmd1(envvar):55 click.echo('Environment variable: %s' % envvar)56 click.echo('Value: %s' % os.environ[envvar])575859Activation60----------6162In order to activate Bash completion, you need to inform Bash that63completion is available for your script, and how. Any Click application64automatically provides support for that. The general way this works is65through a magic environment variable called ``_<PROG_NAME>_COMPLETE``,66where ``<PROG_NAME>`` is your application executable name in uppercase67with dashes replaced by underscores.6869If your tool is called ``foo-bar``, then the magic variable is called70``_FOO_BAR_COMPLETE``. By exporting it with the ``source`` value it will71spit out the activation script which can be trivially activated.7273For instance, to enable Bash completion for your ``foo-bar`` script, this74is what you would need to put into your ``.bashrc``::7576 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"7778For zsh users add this to your ``.zshrc``::7980 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"8182From this point onwards, your script will have autocompletion enabled.8384Activation Script85-----------------8687The above activation example will always invoke your application on88startup. This might be slowing down the shell activation time89significantly if you have many applications. Alternatively, you could also90ship a file with the contents of that, which is what Git and other systems91are doing.9293This can be easily accomplished::9495 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh9697For zsh:9899 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh100101And then you would put this into your .bashrc or .zshrc instead::102103 . /path/to/foo-bar-complete.sh104105