Skip to content
Myr

bashcomplete.rst

This file

Path

docs/bashcomplete.rst

133 lines · 4.7 KiB

Last changed by

fish completionJonathan Lestrelinbefore adoption792f03f

History of this file

Take it

The walk stopped after 200 commits: lines older than 950463b point at it.

Fix typo (#817)scottbeldenbefore adoption950463b

lines 1–14why, 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 about

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 15why, one step back
15how to do that, see :ref:`setuptools-integration`. Click currently

fish completionJonathan Lestrelinbefore adoption792f03f

line 16why, one step back
16only supports completion for Bash, Zsh and Fish.

Fix typo (#817)scottbeldenbefore adoption950463b

lines 17–20why, one step back
17
18What it Completes19-----------------20

Merge pull request #1157 from theazureshadow/clarify-multiple-optionsDavid Lordbefore adoption59a4530

lines 21–24why, one step back
21Generally, the Bash completion support will complete subcommands,
22options, and any option or argument values where the type is23:class:`click.Choice`. Subcommands and choices are always listed whereas24options are only listed if at least a dash has been provided. ::

Fix typo (#817)scottbeldenbefore adoption950463b

lines 25–48why, one step back
25
26 $ 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 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):

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 49why, one step back
49 return [k for k in os.environ.keys() if incomplete in k]

Fix typo (#817)scottbeldenbefore adoption950463b

lines 50–55why, one step back
50
51 @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–57why, one step back
56
57

fish completionJonathan Lestrelinbefore adoption792f03f

lines 58–59why, one step back
58Completion help strings (ZSH/fish only)
59---------------------------------------

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

line 60why, one step back
60

fish completionJonathan Lestrelinbefore adoption792f03f

line 61why, one step back
61ZSH and fish support showing documentation strings for completions. These are taken

Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75

lines 62–82why, one step back
62from the help parameters of options and subcommands. For dynamically generated
63completions 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)

Fix typo (#817)scottbeldenbefore adoption950463b

lines 83–103why, one step back
83
8485Activation86----------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)"103

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

line 104why, one step back
104For zsh users add this to your ``.zshrc``::

Fix typo (#817)scottbeldenbefore adoption950463b

line 105why, one step back
105

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

lines 106–109why, one step back
106 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"
107108From this point onwards, your script will have autocompletion enabled.109

Fix typo (#817)scottbeldenbefore adoption950463b

lines 110–122why, one step back
110Activation Script
111-----------------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.sh122

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

line 123why, one step back
123For zsh:

Fix typo (#817)scottbeldenbefore adoption950463b

line 124why, one step back
124

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

line 125why, one step back
125 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh

Fix typo (#817)scottbeldenbefore adoption950463b

line 126why, one step back
126

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

line 127why, one step back
127And then you would put this into your .bashrc or .zshrc instead::

Fix typo (#817)scottbeldenbefore adoption950463b

line 128why, one step back
128

Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0

line 129why, one step back
129 . /path/to/foo-bar-complete.sh

Fix typo (#817)scottbeldenbefore adoption950463b

line 130why, one step back
130

fish completionJonathan Lestrelinbefore adoption792f03f

line 131why, one step back
131Or for fish, you can just add the file to the completions directory:

Fix typo (#817)scottbeldenbefore adoption950463b

line 132why, one step back
132

fish completionJonathan Lestrelinbefore adoption792f03f

line 133why, one step back
133 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fish