summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2022-02-21 17:45:11 -0800
committerGitHub <noreply@github.com>2022-02-21 17:45:11 -0800
commitf6a681adc13d5961073c00e57044e3855c3b76bd (patch)
tree9507fdc94aa696cf89eb35701c40dd998e6c8019 /src
parent4a888774ef3c5c80cf3c416d7407e13ad22359f0 (diff)
parent570582b7a301336079334179d0b1d873e5e2af76 (diff)
downloadclick-f6a681adc13d5961073c00e57044e3855c3b76bd.tar.gz
Merge pull request #2202 from pallets/command-decorator
refactor command decorator
Diffstat (limited to 'src')
-rw-r--r--src/click/decorators.py48
1 files changed, 20 insertions, 28 deletions
diff --git a/src/click/decorators.py b/src/click/decorators.py
index 0a01925..ca86094 100644
--- a/src/click/decorators.py
+++ b/src/click/decorators.py
@@ -121,33 +121,6 @@ def pass_meta_key(
return decorator
-def _make_command(
- f: F,
- name: t.Optional[str],
- attrs: t.MutableMapping[str, t.Any],
- cls: t.Type[Command],
-) -> Command:
- if isinstance(f, Command):
- raise TypeError("Attempted to convert a callback into a command twice.")
-
- try:
- params = f.__click_params__ # type: ignore
- params.reverse()
- del f.__click_params__ # type: ignore
- except AttributeError:
- params = []
-
- if attrs.get("help") is None:
- attrs["help"] = inspect.getdoc(f)
-
- return cls(
- name=name or f.__name__.lower().replace("_", "-"),
- callback=f,
- params=params,
- **attrs,
- )
-
-
@t.overload
def command(
name: t.Optional[str] = None,
@@ -203,7 +176,26 @@ def command(
name = None
def decorator(f: t.Callable[..., t.Any]) -> Command:
- cmd = _make_command(f, name, attrs, cls) # type: ignore
+ if isinstance(f, Command):
+ raise TypeError("Attempted to convert a callback into a command twice.")
+
+ try:
+ params = f.__click_params__ # type: ignore
+ except AttributeError:
+ params = []
+ else:
+ params.reverse()
+ del f.__click_params__ # type: ignore
+
+ if attrs.get("help") is None:
+ attrs["help"] = f.__doc__
+
+ cmd = cls( # type: ignore[misc]
+ name=name or f.__name__.lower().replace("_", "-"), # type: ignore[arg-type]
+ callback=f,
+ params=params,
+ **attrs,
+ )
cmd.__doc__ = f.__doc__
return cmd