Skip to content
Myr

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 file

Take it

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

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

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

lines 15–16why, one step back
15how to do that, see :ref:`setuptools-integration`. Click currently
16only supports completion for Bash and Zsh.

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

lines 24–29why, one step back
24least 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 -r

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

lines 30–47why, one step back
30
31Additionally, 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 os47

Merge pull request #772 from stopthatcow/feature/update_bash_completion_docsMarkus Unterwaditzerbefore adoptioneb0dcd4

line 48why, one step back
48 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]

Merge pull request #755 from stopthatcow/feature/bash_autocompletion_6.6Markus Unterwaditzerbefore adoption5f2ff4f

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–82why, one step back
56
5758Completion 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 back
83

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

lines 84–96why, one step back
84
85Activation86----------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 will

Fix typos in docs (#623)Radovan Bastbefore adoptionb5239af

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

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

lines 98–103why, one step back
98
99For 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``::

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

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

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

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:

Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptionc0b3542

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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::

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

lines 130–131why, one step back
130
131