bashcomplete.rst
This file
Path
docs/bashcomplete.rst
131 lines · 4.5 KiB
Last changed by
Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
History of this fileTake it
The walk stopped after 200 commits: lines older than 7951a73 point at it.
Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 1–14why, 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 aboutMerge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
lines 15–16why, one step back15how to do that, see :ref:`setuptools-integration`. Click currently16only supports completion for Bash and Zsh.Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 17–20why, one step back1718What it Completes19-----------------20Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 21–23why, one step back21Generally, 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 atAlways work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 24–29why, one step back24least 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 -rMerge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 30–47why, one step back3031Additionally, custom suggestions can be provided for arguments and options with32the ``autocompletion`` parameter. ``autocompletion`` should 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 os47Merge pull request #772 from stopthatcow/feature/update_bash_completion_docsMarkus Unterwaditzerbefore adoptioneb0dcd4
line 48why, one step back48 def get_env_vars(ctx, args, incomplete):Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 49why, one step back49 return [k for k in os.environ.keys() if incomplete in k]Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
lines 50–55why, one step back5051 @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])Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
lines 56–82why, one step back565758Completion 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)Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f
line 83why, one step back83Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 84–96why, one step back8485Activation86----------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 willFix typos in docs (#623)Radovan Bastbefore adoptionb5239af
line 97why, one step back97spit out the activation script which can be trivially activated.Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 98–103why, one step back9899For 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)"103Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 104why, one step back104For zsh users add this to your ``.zshrc``::Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
line 105why, one step back105Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
lines 106–109why, one step back106 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"107108From this point onwards, your script will have autocompletion enabled.109Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
lines 110–122why, one step back110Activation 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.sh122Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 123why, one step back123For zsh:Always work on Unicode in Python 2.Armin Ronacherbefore adoption7951a73
line 124why, one step back124Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 125why, one step back125 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.shMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 126why, one step back126Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 127why, one step back127And then you would put this into your .bashrc or .zshrc instead::Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 128why, one step back128Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 129why, one step back129 . /path/to/foo-bar-complete.shMerge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 130–131why, one step back130131