Skip to content
Myr

bashcomplete.rst

This file

Path

docs/bashcomplete.rst

106 lines · 3.6 KiB

Last changed by

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

History of this file

Take it

The walk stopped after 200 commits: lines older than 8d0dfa5 point at it.

Added subcommand chaining.Armin Ronacherbefore adoption8d0dfa5

lines 1–15why, one step back
1Bash Complete
2=============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 currently

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

lines 16–17why, one step back
16only supports completion for Bash. Zsh support is available through Zsh's
17bash completion compatibility mode.

Added subcommand chaining.Armin Ronacherbefore adoption8d0dfa5

lines 18–21why, one step back
18
19What it Completes20-----------------21

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

lines 22–24why, one step back
22Generally, the Bash completion support will complete subcommands, options
23and any option or argument values where the type is click.Choice.24Subcommands and choices are always listed whereas options only if at

Added subcommand chaining.Armin Ronacherbefore adoption8d0dfa5

lines 25–31why, one step back
25least 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 -r31

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

lines 32–58why, one step back
32Additionally, custom suggestions can be provided for arguments and options with
33the ``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])5758

Added subcommand chaining.Armin Ronacherbefore adoption8d0dfa5

lines 59–70why, one step back
59Activation
60----------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 will

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

line 71why, one step back
71spit out the activation script which can be trivially activated.

Added subcommand chaining.Armin Ronacherbefore adoption8d0dfa5

lines 72–95why, one step back
72
73For 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)"7778From this point onwards, your script will have Bash completion enabled.7980Activation Script81-----------------8283The above activation example will always invoke your application on84startup. This might be slowing down the shell activation time85significantly if you have many applications. Alternatively, you could also86ship a file with the contents of that, which is what Git and other systems87are doing.8889This can be easily accomplished::9091 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh9293And then you would put this into your bashrc instead::9495 . /path/to/foo-bar-complete.sh

Fix test for delayed progress bar displayJan Christoph Bischkobefore adoption82f9c62

lines 96–106why, one step back
96
97Zsh Compatibility98----------------99100To enable Bash completion in Zsh, add the following lines to your .zshrc:101102 autoload bashcompinit103 bashcompinit104105See https://github.com/pallets/click/issues/323 for more information on106this issue.