bashcomplete.rst
This file
Path
docs/bashcomplete.rst
163 lines · 4.7 KiB
Last changed by
clean up completion docsDavid Lordbefore adoption6bf782e
History of this fileTake it
The walk stopped after 200 commits: lines older than 950463b point at it.
clean up completion docsDavid Lordbefore adoption6bf782e
lines 1–2why, one step back1Shell Completion2================Fix typo (#817)scottbeldenbefore adoption950463b
lines 3–5why, one step back34.. versionadded:: 2.05clean up completion docsDavid Lordbefore adoption6bf782e
lines 6–7why, one step back6Click can provide tab completion for commands, options, and choice7values. Bash, Zsh, and Fish are supportedFix typo (#817)scottbeldenbefore adoption950463b
line 8why, one step back8clean up completion docsDavid Lordbefore adoption6bf782e
lines 9–11why, one step back9Completion is only available if a script is installed and invoked10through an entry point, not through the ``python`` command. See11:ref:`setuptools-integration`.Fix typo (#817)scottbeldenbefore adoption950463b
lines 12–16why, one step back121314What it Completes15-----------------16clean up completion docsDavid Lordbefore adoption6bf782e
line 17why, one step back17Generally, the shell completion support will complete commands,Merge pull request #1157 from theazureshadow/clarify-multiple-optionsDavid Lordbefore adoption59a4530
line 18why, one step back18options, and any option or argument values where the type isclean up completion docsDavid Lordbefore adoption6bf782e
lines 19–20why, one step back19:class:`click.Choice`. Options are only listed if at least a dash has20been entered.Fix typo (#817)scottbeldenbefore adoption950463b
line 21why, one step back21clean up completion docsDavid Lordbefore adoption6bf782e
lines 22–23why, one step back22.. code-block:: text23Fix typo (#817)scottbeldenbefore adoption950463b
lines 24–28why, one step back24 $ repo <TAB><TAB>25 clone commit copy delete setuser26 $ repo clone -<TAB><TAB>27 --deep --help --rev --shallow -r28clean up completion docsDavid Lordbefore adoption6bf782e
lines 29–33why, one step back29Custom completions can be provided for argument and option values by30providing 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:Fix typo (#817)scottbeldenbefore adoption950463b
line 34why, one step back34clean up completion docsDavid Lordbefore adoption6bf782e
lines 35–38why, one step back35- ``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.Fix typo (#817)scottbeldenbefore adoption950463b
line 39why, one step back39clean up completion docsDavid Lordbefore adoption6bf782e
lines 40–41why, one step back40Here is an example of using a callback function to generate dynamic41suggestions:Fix typo (#817)scottbeldenbefore adoption950463b
line 42why, one step back42clean up completion docsDavid Lordbefore adoption6bf782e
line 43why, one step back43.. code-block:: pythonFix typo (#817)scottbeldenbefore adoption950463b
lines 44–47why, one step back4445 import os4647 def get_env_vars(ctx, args, incomplete):Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 48why, one step back48 return [k for k in os.environ.keys() if incomplete in k]Fix typo (#817)scottbeldenbefore adoption950463b
lines 49–54why, one step back4950 @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 back5556clean up completion docsDavid Lordbefore adoption6bf782e
lines 57–58why, one step back57Completion help strings58-----------------------Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 59why, one step back59clean up completion docsDavid Lordbefore adoption6bf782e
lines 60–64why, one step back60ZSH 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 back65clean up completion docsDavid Lordbefore adoption6bf782e
lines 66–67why, one step back66Here is an example of using a callback function to generate dynamic67suggestions with help strings:Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 68why, one step back68clean up completion docsDavid Lordbefore adoption6bf782e
line 69why, one step back69.. code-block:: pythonMerge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
lines 70–73why, one step back7071 import os7273 def get_colors(ctx, args, incomplete):clean up completion docsDavid Lordbefore adoption6bf782e
lines 74–76why, one step back74 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 back77 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–87why, one step back838485Activation86----------87clean up completion docsDavid Lordbefore adoption6bf782e
lines 88–93why, one step back88In order to activate shell completion, you need to inform your shell89that 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.Fix typo (#817)scottbeldenbefore adoption950463b
line 94why, one step back94clean up completion docsDavid Lordbefore adoption6bf782e
lines 95–97why, one step back95If your tool is called ``foo-bar``, then the variable is called96``_FOO_BAR_COMPLETE``. By exporting it with the ``source_{shell}``97value it will output the activation script to evaluate.Fix typo (#817)scottbeldenbefore adoption950463b
line 98why, one step back98clean up completion docsDavid Lordbefore adoption6bf782e
line 99why, one step back99Here are examples for a ``foo-bar`` script.Fix typo (#817)scottbeldenbefore adoption950463b
line 100why, one step back100clean up completion docsDavid Lordbefore adoption6bf782e
line 101why, one step back101For Bash, add this to ``~/.bashrc``:Fix typo (#817)scottbeldenbefore adoption950463b
line 102why, one step back102clean up completion docsDavid Lordbefore adoption6bf782e
line 103why, one step back103.. code-block:: textFix typo (#817)scottbeldenbefore adoption950463b
line 104why, one step back104clean up completion docsDavid Lordbefore adoption6bf782e
lines 105–110why, one step back105 eval "$(_FOO_BAR_COMPLETE=source_bash foo-bar)"106107For Zsh, add this to ``~/.zshrc``:108109.. code-block:: text110Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
lines 111–112why, one step back111 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"112clean up completion docsDavid Lordbefore adoption6bf782e
line 113why, one step back113For Fish, add this to ``~/.config/fish/completions/foo-bar.fish``:Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 114why, one step back114clean up completion docsDavid Lordbefore adoption6bf782e
lines 115–122why, one step back115.. code-block:: text116117 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.121122Fix typo (#817)scottbeldenbefore adoption950463b
lines 123–125why, one step back123Activation Script124-----------------125clean up completion docsDavid Lordbefore adoption6bf782e
lines 126–127why, one step back126The above ``eval`` examples will invoke your application every time a127shell is started. This may slow down shell startup time significantly.Fix typo (#817)scottbeldenbefore adoption950463b
line 128why, one step back128clean up completion docsDavid Lordbefore adoption6bf782e
lines 129–132why, one step back129Alternatively, export the generated completion code as a static script130to 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.Fix typo (#817)scottbeldenbefore adoption950463b
line 133why, one step back133clean up completion docsDavid Lordbefore adoption6bf782e
line 134why, one step back134For Bash:Fix typo (#817)scottbeldenbefore adoption950463b
line 135why, one step back135clean up completion docsDavid Lordbefore adoption6bf782e
line 136why, one step back136.. code-block:: textFix typo (#817)scottbeldenbefore adoption950463b
line 137why, one step back137clean up completion docsDavid Lordbefore adoption6bf782e
lines 138–143why, one step back138 _FOO_BAR_COMPLETE=source_bash foo-bar > foo-bar-complete.sh139140For Zsh:141142.. code-block:: text143Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 144why, one step back144 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.shFix typo (#817)scottbeldenbefore adoption950463b
line 145why, one step back145clean up completion docsDavid Lordbefore adoption6bf782e
line 146why, one step back146For Fish:Fix typo (#817)scottbeldenbefore adoption950463b
line 147why, one step back147clean up completion docsDavid Lordbefore adoption6bf782e
lines 148–156why, one step back148.. code-block:: text149150 _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:: text156Merge pull request #865 from stopthatcow/feature/323David Lordbefore adoption39e46e0
line 157why, one step back157 . /path/to/foo-bar-complete.shFix typo (#817)scottbeldenbefore adoption950463b
line 158why, one step back158clean up completion docsDavid Lordbefore adoption6bf782e
lines 159–161why, one step back159For Fish, add the file to the completions directory:160161.. code-block:: textFix typo (#817)scottbeldenbefore adoption950463b
line 162why, one step back162fish completionJonathan Lestrelinbefore adoption792f03f
line 163why, one step back163 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fish