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
The walk stopped after 200 commits: lines older than 2534940 point at it.
Added link to example github issue.Armin Ronacherbefore adoption2534940
lines 1–15why, 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 currentlyMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 16–17why, one step back16only supports completion for Bash. Zsh support is available through Zsh's17bash completion compatibility mode.Added link to example github issue.Armin Ronacherbefore adoption2534940
lines 18–21why, one step back1819What it Completes20-----------------21Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 22–24why, one step back22Generally, 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 atAdded link to example github issue.Armin Ronacherbefore adoption2534940
lines 25–30why, one step back25least 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 -rMerge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 31–48why, one step back3132Additionally, 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 os48Merge pull request #772 from stopthatcow/feature/update_bash_completion_docsMarkus Unterwaditzerbefore adoptioneb0dcd4
line 49why, one step back49 def get_env_vars(ctx, args, incomplete):Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 50–57why, one step back50 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])57Added link to example github issue.Armin Ronacherbefore adoption2534940
lines 58–70why, one step back5859Activation60----------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 willFix typos in docs (#623)Radovan Bastbefore adoptionb5239af
line 71why, one step back71spit out the activation script which can be trivially activated.Added link to example github issue.Armin Ronacherbefore adoption2534940
lines 72–77why, one step back7273For 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)"77Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 78why, one step back78For zsh users add this to your ``.zshrc``::Added link to example github issue.Armin Ronacherbefore adoption2534940
line 79why, one step back79Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
lines 80–83why, one step back80 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"8182From this point onwards, your script will have autocompletion enabled.83Added link to example github issue.Armin Ronacherbefore adoption2534940
lines 84–96why, one step back84Activation 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.sh96Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 97why, one step back97For zsh:Added link to example github issue.Armin Ronacherbefore adoption2534940
line 98why, one step back98Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 99why, one step back99 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.shMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 100why, one step back100Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 101why, one step back101And then you would put this into your .bashrc or .zshrc instead::Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 102why, one step back102Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 103why, one step back103 . /path/to/foo-bar-complete.shMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 104–105why, one step back104105