bashcomplete.rst
This file
Path
docs/bashcomplete.rst
163 lines · 4.7 KiB
Last changed by
Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
History of this fileTake it
The walk stopped after 200 commits: lines older than c67daae point at it.
Merge branch '7.x'David Lordbefore adoption8f36eab
lines 1–2why, one step back1Shell Completion2================Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 3–5why, one step back34.. versionadded:: 2.05Merge branch '7.x'David Lordbefore adoption8f36eab
lines 6–7why, one step back6Click can provide tab completion for commands, options, and choice7values. Bash, Zsh, and Fish are supportedImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 8why, one step back8Merge branch '7.x'David Lordbefore adoption8f36eab
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`.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 12–16why, one step back121314What it Completes15-----------------16Merge branch '7.x'David Lordbefore adoption8f36eab
line 17why, one step back17Generally, the shell completion support will complete commands,Merge branch '7.x'David Lordbefore adoptionc6ef735
line 18why, one step back18options, and any option or argument values where the type isMerge branch '7.x'David Lordbefore adoption8f36eab
lines 19–20why, one step back19:class:`click.Choice`. Options are only listed if at least a dash has20been entered.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 21why, one step back21Merge branch '7.x'David Lordbefore adoption8f36eab
lines 22–23why, one step back22.. code-block:: text23Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 24–28why, one step back24 $ repo <TAB><TAB>25 clone commit copy delete setuser26 $ repo clone -<TAB><TAB>27 --deep --help --rev --shallow -r28Merge branch '7.x'David Lordbefore adoption8f36eab
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:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 34why, one step back34Merge branch '7.x'David Lordbefore adoption8f36eab
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.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 39why, one step back39Merge branch '7.x'David Lordbefore adoption8f36eab
lines 40–41why, one step back40Here is an example of using a callback function to generate dynamic41suggestions:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 42why, one step back42Merge branch '7.x'David Lordbefore adoption8f36eab
line 43why, one step back43.. code-block:: pythonImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
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]Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 49–52why, one step back4950 @click.command()51 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)52 def cmd1(envvar):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 53–54why, one step back53 click.echo(f"Environment variable: {envvar}")54 click.echo(f"Value: {os.environ[envvar]}")Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
lines 55–56why, one step back5556Merge branch '7.x'David Lordbefore adoption8f36eab
lines 57–58why, one step back57Completion help strings58-----------------------Merge pull request #869 from stopthatcow/feature/866David Lordbefore adoptiond37ff75
line 59why, one step back59Merge branch '7.x'David Lordbefore adoption8f36eab
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 back65Merge branch '7.x'David Lordbefore adoption8f36eab
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 back68Merge branch '7.x'David Lordbefore adoption8f36eab
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):Merge branch '7.x'David Lordbefore adoption8f36eab
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–81why, 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):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 82why, one step back82 click.echo(f"Chosen color is {color}")Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 83–87why, one step back838485Activation86----------87Merge branch '7.x'David Lordbefore adoption8f36eab
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.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 94why, one step back94Merge branch '7.x'David Lordbefore adoption8f36eab
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.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 98why, one step back98Merge branch '7.x'David Lordbefore adoption8f36eab
line 99why, one step back99Here are examples for a ``foo-bar`` script.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 100why, one step back100Merge branch '7.x'David Lordbefore adoption8f36eab
line 101why, one step back101For Bash, add this to ``~/.bashrc``:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 102why, one step back102Merge branch '7.x'David Lordbefore adoption8f36eab
line 103why, one step back103.. code-block:: textImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 104why, one step back104Merge branch '7.x'David Lordbefore adoption8f36eab
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)"112Merge branch '7.x'David Lordbefore adoption8f36eab
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 back114Merge branch '7.x'David Lordbefore adoption8f36eab
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.121122Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
lines 123–125why, one step back123Activation Script124-----------------125Merge branch '7.x'David Lordbefore adoption8f36eab
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.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 128why, one step back128Merge branch '7.x'David Lordbefore adoption8f36eab
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.Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 133why, one step back133Merge branch '7.x'David Lordbefore adoption8f36eab
line 134why, one step back134For Bash:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 135why, one step back135Merge branch '7.x'David Lordbefore adoption8f36eab
line 136why, one step back136.. code-block:: textImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 137why, one step back137Merge branch '7.x'David Lordbefore adoption8f36eab
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.shImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 145why, one step back145Merge branch '7.x'David Lordbefore adoption8f36eab
line 146why, one step back146For Fish:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 147why, one step back147Merge branch '7.x'David Lordbefore adoption8f36eab
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.shImprove docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 158why, one step back158Merge branch '7.x'David Lordbefore adoption8f36eab
line 159why, one step back159For Fish, add the file to the completions directory:Improve docs for Choice type (addresses issue #892) (#900)Stefan Scherfkebefore adoptionc67daae
line 160why, one step back160Merge branch '7.x'David Lordbefore adoption8f36eab
lines 161–163why, one step back161.. code-block:: text162163 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fish