Skip to content
Myr

Merge pull request #2202 from pallets/command-decorator

Before adoption: this commit came in with the history the project adopted, so no landing records it.

refactor command decorator

authored by David Lord · committed by GitHub · parents 4a88877, 570582b · Browse files · 1 file+20 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+20 added−28 removed

  • other:+20 added−28 removed

1 file+20 added−28 removed

modified:src/click/decorators.py+20 added-28 removed
119 unmodified lines
120 )121 return decorator122123124def _make_command(125 f: F,126 name: t.Optional[str],127 attrs: t.MutableMapping[str, t.Any],128 cls: t.Type[Command],129) -> Command:130 if isinstance(f, Command):131 raise TypeError("Attempted to convert a callback into a command twice.")132133 try:134 params = f.__click_params__ # type: ignore135 params.reverse()136 del f.__click_params__ # type: ignore137 except AttributeError:138 params = []139140 if attrs.get("help") is None:141 attrs["help"] = inspect.getdoc(f)142143 return cls(144 name=name or f.__name__.lower().replace("_", "-"),145 callback=f,146 params=params,147 **attrs,148 )149150124@t.overload125def command(126 name: t.Optional[str] = None,127 cls: t.Optional[t.Type[Command]] = None,
47 unmodified lines
175 func = name176 name = None177178 def decorator(f: t.Callable[..., t.Any]) -> Command:206 cmd = _make_command(f, name, attrs, cls) # type: ignore179 if isinstance(f, Command):180 raise TypeError("Attempted to convert a callback into a command twice.")181182 try:183 params = f.__click_params__ # type: ignore184 except AttributeError:185 params = []186 else:187 params.reverse()188 del f.__click_params__ # type: ignore189190 if attrs.get("help") is None:191 attrs["help"] = f.__doc__192193 cmd = cls( # type: ignore[misc]194 name=name or f.__name__.lower().replace("_", "-"), # type: ignore[arg-type]195 callback=f,196 params=params,197 **attrs,198 )199 cmd.__doc__ = f.__doc__200 return cmd201202 if func is not None:
275 unmodified lines