Skip to content
Myr

bashcomplete.rst

This file

Path

docs/bashcomplete.rst

163 lines · 4.7 KiB

Last changed by

Merge branch '7.x'David Lordbefore adoption8f36eab

History of this file

Take it

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

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 1–2why, one step back
1Shell Completion
2================

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

lines 3–5why, one step back
3
4.. versionadded:: 2.05

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 6–7why, one step back
6Click can provide tab completion for commands, options, and choice
7values. Bash, Zsh, and Fish are supported

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 8why, one step back
8

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 9–11why, one step back
9Completion is only available if a script is installed and invoked
10through an entry point, not through the ``python`` command. See11:ref:`setuptools-integration`.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

lines 12–16why, one step back
12
1314What it Completes15-----------------16

Merge branch '7.x'David Lordbefore adoption8f36eab

line 17why, one step back
17Generally, the shell completion support will complete commands,

Merge branch '7.x'David Lordbefore adoptionc6ef735

line 18why, one step back
18options, and any option or argument values where the type is

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 19–20why, one step back
19:class:`click.Choice`. Options are only listed if at least a dash has
20been entered.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 21why, one step back
21

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 22–23why, one step back
22.. code-block:: text
23

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

lines 24–27why, one step back
24 $ repo <TAB><TAB>
25 clone commit copy delete setuser26 $ repo clone -<TAB><TAB>27 --deep --help --rev --shallow -r

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

line 28why, one step back
28

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 29–33why, one step back
29Custom completions can be provided for argument and option values by
30providing an ``autocompletion`` function that returns a list of strings.31This is useful when the suggestions need to be dynamically generated32completion time. The callback function will be passed 3 keyword33arguments:

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

line 34why, one step back
34

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 35–38why, one step back
35- ``ctx`` - The current command context.
36- ``args`` - The list of arguments passed in.37- ``incomplete`` - The partial word that is being completed. May38 be an empty string if no characters have been entered yet.

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

line 39why, one step back
39

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 40–41why, one step back
40Here is an example of using a callback function to generate dynamic
41suggestions:

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

line 42why, one step back
42

Merge branch '7.x'David Lordbefore adoption8f36eab

line 43why, one step back
43.. code-block:: python

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

lines 44–46why, one step back
44
45 import os46

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

line 47why, one step back
47 def get_env_vars(ctx, args, incomplete):

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

line 48why, one step back
48 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 49–54why, one step back
49
50 @click.command()51 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)52 def cmd1(envvar):53 click.echo('Environment variable: %s' % envvar)54 click.echo('Value: %s' % os.environ[envvar])

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

lines 55–56why, one step back
55
56

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 57–58why, one step back
57Completion help strings
58-----------------------

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

line 59why, one step back
59

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 60–64why, one step back
60ZSH and fish support showing documentation strings for completions.
61These are taken from the help parameters of options and subcommands. For62dynamically generated completions a help string can be provided by63returning a tuple instead of a string. The first element of the tuple is64the completion and the second is the help string to display.

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

line 65why, one step back
65

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 66–67why, one step back
66Here is an example of using a callback function to generate dynamic
67suggestions with help strings:

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

line 68why, one step back
68

Merge branch '7.x'David Lordbefore adoption8f36eab

line 69why, one step back
69.. code-block:: python

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

lines 70–73why, one step back
70
71 import os7273 def get_colors(ctx, args, incomplete):

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 74–76why, one step back
74 colors = [('red', 'a warm color'),
75 ('blue', 'a cool color'),76 ('green', 'the other starter color')]

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

lines 77–82why, one step back
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 '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

lines 84–87why, one step back
84
85Activation86----------87

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 88–93why, one step back
88In order to activate shell completion, you need to inform your shell
89that completion is available for your script. Any Click application90automatically provides support for that. If the program is executed with91a special ``_<PROG_NAME>_COMPLETE`` variable, the completion mechanism92is triggered instead of the normal command. ``<PROG_NAME>`` is the93executable name in uppercase with dashes replaced by underscores.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 94why, one step back
94

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 95–97why, one step back
95If your tool is called ``foo-bar``, then the variable is called
96``_FOO_BAR_COMPLETE``. By exporting it with the ``source_{shell}``97value it will output the activation script to evaluate.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 98why, one step back
98

Merge branch '7.x'David Lordbefore adoption8f36eab

line 99why, one step back
99Here are examples for a ``foo-bar`` script.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 100why, one step back
100

Merge branch '7.x'David Lordbefore adoption8f36eab

line 101why, one step back
101For Bash, add this to ``~/.bashrc``:

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 102why, one step back
102

Merge branch '7.x'David Lordbefore adoption8f36eab

line 103why, one step back
103.. code-block:: text

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 104why, one step back
104

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 105–110why, one step back
105 eval "$(_FOO_BAR_COMPLETE=source_bash foo-bar)"
106107For Zsh, add this to ``~/.zshrc``:108109.. code-block:: text110

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

lines 111–112why, one step back
111 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"
112

Merge branch '7.x'David Lordbefore adoption8f36eab

line 113why, one step back
113For Fish, add this to ``~/.config/fish/completions/foo-bar.fish``:

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

line 114why, one step back
114

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 115–122why, one step back
115.. code-block:: text
116117 eval (env _FOO_BAR_COMPLETE=source_fish foo-bar)118119Open a new shell to enable completion. Or run the ``eval`` command120directly in your current shell to enable it temporarily.121122

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

lines 123–125why, one step back
123Activation Script
124-----------------125

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 126–127why, one step back
126The above ``eval`` examples will invoke your application every time a
127shell is started. This may slow down shell startup time significantly.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 128why, one step back
128

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 129–132why, one step back
129Alternatively, export the generated completion code as a static script
130to be executed. You can ship this file with your builds; tools like Git131do this. At least Zsh will also cache the results of completion files,132but not ``eval`` scripts.

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 133why, one step back
133

Merge branch '7.x'David Lordbefore adoption8f36eab

line 134why, one step back
134For Bash:

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 135why, one step back
135

Merge branch '7.x'David Lordbefore adoption8f36eab

line 136why, one step back
136.. code-block:: text

Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption598ad33

line 137why, one step back
137

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 138–143why, one step back
138 _FOO_BAR_COMPLETE=source_bash foo-bar > foo-bar-complete.sh
139140For Zsh:141142.. code-block:: text143

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

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 145why, one step back
145

Merge branch '7.x'David Lordbefore adoption8f36eab

line 146why, one step back
146For Fish:

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 147why, one step back
147

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 148–156why, one step back
148.. code-block:: text
149150 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh151152In ``.bashrc`` or ``.zshrc``, source the script instead of the ``eval``153command:154155.. code-block:: text156

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

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

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 158why, one step back
158

Merge branch '7.x'David Lordbefore adoption8f36eab

line 159why, one step back
159For Fish, add the file to the completions directory:

Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a

line 160why, one step back
160

Merge branch '7.x'David Lordbefore adoption8f36eab

lines 161–163why, one step back
161.. code-block:: text
162163 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fish