bashcomplete.rst
This file
Path
docs/bashcomplete.rst
107 lines · 3.9 KiB
Last changed by
Merge remote-tracking branch 'click/master' into feature/bash_autocompletion_6.6Nicholas Wilesbefore adoption4fd8955
History of this fileTake it
The walk stopped after 200 commits: lines older than 5a90908 point at it.
Entering a context multiple times defers the cleanup.Armin Ronacherbefore adoption5a90908
lines 1–23why, one step back1Bash 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.1718Currently, Bash completion is an internal feature that is not customizable.19This might be relaxed in future versions.2021What it Completes22-----------------23Merge branch 'feature/parameter_autocompletion' of github.com:bb4242/click into feature/bash_completion_6.6Ibefore adoption483e943
line 24why, one step back24By default, the Bash completion support will complete subcommands andEntering a context multiple times defers the cleanup.Armin Ronacherbefore adoption5a90908
lines 25–31why, one step back25parameters. Subcommands are always listed whereas parameters only if at26least a dash has been provided. Example::2728 $ repo <TAB><TAB>29 clone commit copy delete setuser30 $ repo clone -<TAB><TAB>31 --deep --help --rev --shallow -rMerge branch 'feature/parameter_autocompletion' of github.com:bb4242/click into feature/bash_completion_6.6Ibefore adoption483e943
lines 32–69why, one step back32 33Additionally, custom suggestions can be provided for arguments with the34``autocompletion`` parameter. ``autocompletion`` may be a list of strings, as35in the following example:3637.. click:example::3839 @click.command()40 @click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])41 def cmd1(name):42 click.echo('Name: %s' % name)43 44Alternatively, ``autocompletion`` may be a callback function that returns a list45of strings. This is useful when the suggestions need to be dynamically generated46at bash completion time. The callback function will be passed 4 keyword47arguments:4849- ``ctx`` - The current click context.50- ``incomplete`` - The partial word that is being completed, as a string. May51 be an empty string ``''`` if no characters have been entered yet.52- ``cwords`` - The bash `COMP_WORDS <https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion>`_ array, as a list of strings.53- ``cword`` - The bash `COMP_CWORD <https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion>`_ variable, as an integer.5455Here is an example of using a callback function to generate dynamic suggestions:5657.. click:example::5859 import os60 61 def get_env_vars(ctx, incomplete, cwords, cword):62 return os.environ.keys()6364 @click.command()65 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)66 def cmd1(envvar):67 click.echo('Environment variable: %s' % envvar)68 click.echo('Value: %s' % os.environ[envvar])69 Entering a context multiple times defers the cleanup.Armin Ronacherbefore adoption5a90908
lines 70–82why, one step back7071Activation72----------7374In order to activate Bash completion, you need to inform Bash that75completion is available for your script, and how. Any Click application76automatically provides support for that. The general way this works is77through a magic environment variable called ``_<PROG_NAME>_COMPLETE``,78where ``<PROG_NAME>`` is your application executable name in uppercase79with dashes replaced by underscores.8081If your tool is called ``foo-bar``, then the magic variable is called82``_FOO_BAR_COMPLETE``. By exporting it with the ``source`` value it willMerge remote-tracking branch 'click/master' into feature/bash_autocompletion_6.6Nicholas Wilesbefore adoption4fd8955
line 83why, one step back83spit out the activation script which can be trivially activated.Entering a context multiple times defers the cleanup.Armin Ronacherbefore adoption5a90908
lines 84–107why, one step back8485For instance, to enable Bash completion for your ``foo-bar`` script, this86is what you would need to put into your ``.bashrc``::8788 eval "$(_FOO_BAR_COMPLETE=source foo-bar)"8990From this point onwards, your script will have Bash completion enabled.9192Activation Script93-----------------9495The above activation example will always invoke your application on96startup. This might be slowing down the shell activation time97significantly if you have many applications. Alternatively, you could also98ship a file with the contents of that, which is what Git and other systems99are doing.100101This can be easily accomplished::102103 _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh104105And then you would put this into your bashrc instead::106107 . /path/to/foo-bar-complete.sh