bashcomplete.rst
This file
Path
docs/bashcomplete.rst
131 lines · 4.5 KiB
Last changed by
Merge pull request #1126 from kislyuk/patch-1Stephen Rosenbefore adoption9848366
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`. Click currently16only supports completion for Bash and Zsh.1718What it Completes19-----------------2021Generally, the Bash completion support will complete subcommands, options22and any option or argument values where the type is click.Choice.23Subcommands and choices are always listed whereas options only if at24least a dash has been provided. Example::2526 $ repo <TAB><TAB>27 clone commit copy delete setuser28 $ repo clone -<TAB><TAB>29 --deep --help --rev --shallow -r3031Additionally, custom suggestions can be provided for arguments and options with32the ``autocompletion`` parameter. ``autocompletion`` should be a callback function33that returns a list of strings. This is useful when the suggestions need to be34dynamically generated at bash completion time. The callback function will be35passed 3 keyword arguments:3637- ``ctx`` - The current click context.38- ``args`` - The list of arguments passed in.39- ``incomplete`` - The partial word that is being completed, as a string. May40 be an empty string ``''`` if no characters have been entered yet.4142Here is an example of using a callback function to generate dynamic suggestions:4344.. click:example::4546 import os4748 def get_env_vars(ctx, args, incomplete):49 return [k for k in os.environ.keys() if incomplete in k]5051 @click.command()52 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)53 def cmd1(envvar):54 click.echo('Environment variable: %s' % envvar)55 click.echo('Value: %s' % os.environ[envvar])565758Completion help strings (ZSH only)59----------------------------------6061ZSH supports showing documentation strings for completions. These are taken62from the help parameters of options and subcommands. For dynamically generated63completions a help string can be provided by returning a tuple instead of a64string. The first element of the tuple is the completion and the second is the65help string to display.6667Here is an example of using a callback function to generate dynamic suggestions with help strings:6869.. click:example::7071 import os7273 def get_colors(ctx, args, incomplete):74 colors = [('red', 'help string for the color red'),75 ('blue', 'help string for the color blue'),76 ('green', 'help string for the color green')]77 return [c for c in colors if incomplete in c[0]]7879 @click.command()80 @click.argument("color", type=click.STRING, autocompletion=get_colors)81 def cmd1(color):82 click.echo('Chosen color is %s' % color)838485Activation86----------8788In order to activate Bash completion, you need to inform Bash that89completion is available for your script, and how. Any Click application90automatically provides support for that. The general way this works is91through a magic environment variable called ``_<PROG_NAME>_COMPLETE``,92where ``<PROG_NAME>`` is your application executable name in uppercase93with dashes replaced by underscores.9495If your tool is called ``foo-bar``, then the magic variable is called96``_FOO_BAR_COMPLETE``. By exporting it with the ``source`` value it will97spit out the activation script which can be trivially activated.9899For instance, to enable Bash completion for your ``foo-bar`` script, this100is what you would need to put into your ``.bashrc``::101102 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"103104For zsh users add this to your ``.zshrc``::105106 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"107108From this point onwards, your script will have autocompletion enabled.109110Activation Script111-----------------112113The above activation example will always invoke your application on114startup. This might be slowing down the shell activation time115significantly if you have many applications. Alternatively, you could also116ship a file with the contents of that, which is what Git and other systems117are doing.118119This can be easily accomplished::120121 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh122123For zsh:124125 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh126127And then you would put this into your .bashrc or .zshrc instead::128129 . /path/to/foo-bar-complete.sh130131