clean up completion docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 347b45b · Browse files · 2 files·+93 added−62 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+93 added−62 removed
- other:+93 added−62 removed
Provenance
- Commit
6bf782e- Tree
0318c23- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
347b45b
2 files changed+93 added−62 removed
modified:docs/bashcomplete.rst+91 added-61 removed
1-Bash Complete2-=============1+Shell Completion2+================3 4 .. versionadded:: 2.05 6-As of Click 2.0, there is built-in support for Bash completion for7-any Click script. There are certain restrictions on when this completion8-is available, but for the most part it should just work.6+Click can provide tab completion for commands, options, and choice7+values. Bash, Zsh, and Fish are supported8 10-Limitations11------------9+Completion is only available if a script is installed and invoked10+through an entry point, not through the ``python`` command. See11+:ref:`setuptools-integration`.12 13-Bash completion is only available if a script has been installed properly,14-and not executed through the ``python`` command. For information about15-how to do that, see :ref:`setuptools-integration`. Click currently16-only supports completion for Bash, Zsh and Fish.13 14 What it Completes15 -----------------16 21-Generally, the Bash completion support will complete subcommands,17+Generally, the shell completion support will complete commands,18 options, and any option or argument values where the type is23-:class:`click.Choice`. Subcommands and choices are always listed whereas24-options are only listed if at least a dash has been provided. ::19+:class:`click.Choice`. Options are only listed if at least a dash has20+been entered.21 22+.. code-block:: text23+24 $ repo <TAB><TAB>25 clone commit copy delete setuser26 $ repo clone -<TAB><TAB>27 --deep --help --rev --shallow -r28 31-Additionally, custom suggestions can be provided for arguments and options with32-the ``autocompletion`` parameter. ``autocompletion`` should a callback function33-that returns a list of strings. This is useful when the suggestions need to be34-dynamically generated at bash completion time. The callback function will be35-passed 3 keyword arguments:29+Custom completions can be provided for argument and option values by30+providing an ``autocompletion`` function that returns a list of strings.31+This is useful when the suggestions need to be dynamically generated32+completion time. The callback function will be passed 3 keyword33+arguments:34 37-- ``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.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.39 42-Here is an example of using a callback function to generate dynamic suggestions:40+Here is an example of using a callback function to generate dynamic41+suggestions:42 44-.. click:example::43+.. code-block:: python44 45 import os46 47 def get_env_vars(ctx, args, incomplete):5 unmodified lines53 click.echo('Environment variable: %s' % envvar)54 click.echo('Value: %s' % os.environ[envvar])55 56 58-Completion help strings (ZSH/fish only)59----------------------------------------57+Completion help strings58+-----------------------59 61-ZSH and fish support showing documentation strings for completions. These are taken62-from the help parameters of options and subcommands. For dynamically generated63-completions a help string can be provided by returning a tuple instead of a64-string. The first element of the tuple is the completion and the second is the65-help string to display.60+ZSH and fish support showing documentation strings for completions.61+These are taken from the help parameters of options and subcommands. For62+dynamically generated completions a help string can be provided by63+returning a tuple instead of a string. The first element of the tuple is64+the completion and the second is the help string to display.65 67-Here is an example of using a callback function to generate dynamic suggestions with help strings:66+Here is an example of using a callback function to generate dynamic67+suggestions with help strings:68 69-.. click:example::69+.. code-block:: python70 71 import os72 73 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')]74+ colors = [('red', 'a warm color'),75+ ('blue', 'a cool color'),76+ ('green', 'the other starter color')]77 return [c for c in colors if incomplete in c[0]]78 79 @click.command()80 @click.argument("color", type=click.STRING, autocompletion=get_colors)3 unmodified lines84 85 Activation86 ----------87 88-In order to activate Bash completion, you need to inform Bash that89-completion is available for your script, and how. Any Click application90-automatically provides support for that. The general way this works is91-through a magic environment variable called ``_<PROG_NAME>_COMPLETE``,92-where ``<PROG_NAME>`` is your application executable name in uppercase93-with dashes replaced by underscores.88+In order to activate shell completion, you need to inform your shell89+that completion is available for your script. Any Click application90+automatically provides support for that. If the program is executed with91+a special ``_<PROG_NAME>_COMPLETE`` variable, the completion mechanism92+is triggered instead of the normal command. ``<PROG_NAME>`` is the93+executable name in uppercase with dashes replaced by underscores.94 95-If your tool is called ``foo-bar``, then the magic variable is called96-``_FOO_BAR_COMPLETE``. By exporting it with the ``source`` value it will97-spit out the activation script which can be trivially activated.95+If your tool is called ``foo-bar``, then the variable is called96+``_FOO_BAR_COMPLETE``. By exporting it with the ``source_{shell}``97+value it will output the activation script to evaluate.98 99-For instance, to enable Bash completion for your ``foo-bar`` script, this100-is what you would need to put into your ``.bashrc``::99+Here are examples for a ``foo-bar`` script.100 102- eval "$(_FOO_BAR_COMPLETE=source foo-bar)"101+For Bash, add this to ``~/.bashrc``:102 104-For zsh users add this to your ``.zshrc``::103+.. code-block:: text104 105+ eval "$(_FOO_BAR_COMPLETE=source_bash foo-bar)"106+107+For Zsh, add this to ``~/.zshrc``:108+109+.. code-block:: text110+111 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"112 108-From this point onwards, your script will have autocompletion enabled.113+For Fish, add this to ``~/.config/fish/completions/foo-bar.fish``:114 115+.. code-block:: text116+117+ eval (env _FOO_BAR_COMPLETE=source_fish foo-bar)118+119+Open a new shell to enable completion. Or run the ``eval`` command120+directly in your current shell to enable it temporarily.121+122+123 Activation Script124 -----------------125 113-The above activation example will always invoke your application on114-startup. This might be slowing down the shell activation time115-significantly if you have many applications. Alternatively, you could also116-ship a file with the contents of that, which is what Git and other systems117-are doing.126+The above ``eval`` examples will invoke your application every time a127+shell is started. This may slow down shell startup time significantly.128 119-This can be easily accomplished::129+Alternatively, export the generated completion code as a static script130+to be executed. You can ship this file with your builds; tools like Git131+do this. At least Zsh will also cache the results of completion files,132+but not ``eval`` scripts.133 121- _FOO_BAR_COMPLETE=source foo-bar > foo-bar-complete.sh134+For Bash:135 123-For zsh:136+.. code-block:: text137 138+ _FOO_BAR_COMPLETE=source_bash foo-bar > foo-bar-complete.sh139+140+For Zsh:141+142+.. code-block:: text143+144 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh145 127-And then you would put this into your .bashrc or .zshrc instead::146+For Fish:147 148+.. code-block:: text149+150+ _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh151+152+In ``.bashrc`` or ``.zshrc``, source the script instead of the ``eval``153+command:154+155+.. code-block:: text156+157 . /path/to/foo-bar-complete.sh158 131-Or for fish, you can just add the file to the completions directory:159+For Fish, add the file to the completions directory:160 161+.. code-block:: text162+163 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fishmodified:CHANGES.rst+2 added-1 removed
60 unmodified lines61 - Add a repr to ``Command``, ``Group``, ``Option``, and ``Argument``,62 showing the name for friendlier debugging. :issue:`1267`63 - Completion doesn't consider option names if a value starts with64 ``-`` after the ``--`` separator. :issue:`1247`65-- ZSH Completion escapes special characters in values. :pr:`1418`65+- ZSH completion escapes special characters in values. :pr:`1418`66+- Add completion support for Fish shell. :pr:`1423`67 68 69 Version 7.070 -----------540 unmodified lines