Skip to content

Commit

Permalink
use add_completion_class as a decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Oct 3, 2020
1 parent 6e0af87 commit 598f69c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 4 additions & 2 deletions docs/shell-completion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ The example code is for a made up shell "My Shell" or "mysh" for short.

.. code-block:: python
from click.shell_completion import add_completion_class
from click.shell_completion import ShellComplete
_mysh_source = """\
%(complete_func)s {
response=$(%(complete_var)s=complete_mysh %(prog_name)s)
Expand All @@ -216,12 +219,11 @@ The example code is for a made up shell "My Shell" or "mysh" for short.
call-on-complete %(prog_name)s %(complete_func)s
"""
@add_completion_class
class MyshComplete(ShellComplete):
name = "mysh"
source_template = _mysh_source
add_completion_class("mysh", MyshComplete)
Next, implement :meth:`~ShellComplete.get_completion_args`. This must
get, parse, and return the complete args and incomplete value from the
completion script. For example, for the Bash implementation the
Expand Down
22 changes: 17 additions & 5 deletions src/click/shell_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ class ShellComplete:
.. versionadded:: 8.0
"""

name = None
"""Name to register the shell as with :func:`add_completion_class`.
This is used in completion instructions (``source_{name}`` and
``complete_{name}``).
"""
source_template = None
"""Completion script template formatted by :meth:`source`. This must
be provided by subclasses.
Expand Down Expand Up @@ -234,6 +239,7 @@ def complete(self):
class BashComplete(ShellComplete):
"""Shell completion for Bash."""

name = "bash"
source_template = _SOURCE_BASH

def _check_version(self):
Expand Down Expand Up @@ -279,6 +285,7 @@ def format_completion(self, item):
class ZshComplete(ShellComplete):
"""Shell completion for Zsh."""

name = "zsh"
source_template = _SOURCE_ZSH

def get_completion_args(self):
Expand All @@ -301,6 +308,7 @@ def format_completion(self, item):
class FishComplete(ShellComplete):
"""Shell completion for Fish."""

name = "fish"
source_template = _SOURCE_FISH

def get_completion_args(self):
Expand Down Expand Up @@ -331,16 +339,20 @@ def format_completion(self, item):
}


def add_completion_class(name, completion_class):
def add_completion_class(cls, name=None):
"""Register a :class:`ShellComplete` subclass under the given name.
The name will be provided by the completion instruction environment
variable during completion.
:param name: Name to register the class under.
:param completion_class: The completion class that will handle
completion for the shell.
:param cls: The completion class that will handle completion for the
shell.
:param name: Name to register the class under. Defaults to the
class's ``name`` attribute.
"""
_available_shells[name] = completion_class
if name is None:
name = cls.name

_available_shells[name] = cls


def get_completion_class(shell):
Expand Down

0 comments on commit 598f69c

Please sign in to comment.