Coverage for typer / _click / decorators.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-09 00:15 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-11-09 00:15 +0000
1from collections.abc import Callable 1abcdefg
2from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar 1abcdefg
4from .core import Command, Context, Parameter 1abcdefg
5from .utils import echo 1abcdefg
7if TYPE_CHECKING: 1abcdefg
8 from ..core import TyperGroup, TyperOption
10 GrpType = TypeVar("GrpType", bound=TyperGroup)
13P = ParamSpec("P") 1abcdefg
15R = TypeVar("R") 1abcdefg
16T = TypeVar("T") 1abcdefg
17_AnyCallable = Callable[..., Any] 1abcdefg
20CmdType = TypeVar("CmdType", bound=Command) 1abcdefg
23def option( 1abcdefg
24 param_decls: list[str], cls: type["TyperOption"] | None = None, **attrs: Any
25) -> Callable[[Command], Command]:
26 """Attaches an option to the command."""
27 if cls is None: 1abcdefg
28 # avoid circular imports
29 from ..core import TyperOption 1abcdefg
31 cls = TyperOption 1abcdefg
33 def decorator(f: Command) -> Command: 1abcdefg
34 param = cls(param_decls=param_decls, **attrs) 1abcdefg
35 f.params.append(param) 1abcdefg
36 return f 1abcdefg
38 return decorator 1abcdefg
41def help_option(param_decls: list[str]) -> Callable[[Command], Command]: 1abcdefg
42 """Help option which prints the help page and exits the program."""
44 def show_help(ctx: Context, param: Parameter, value: bool) -> None: 1abcdefg
45 """Callback that print the help page on ``<stdout>`` and exits."""
46 if value and not ctx.resilient_parsing: 1abcdefg
47 echo(ctx.get_help(), color=ctx.color) 1abcdefg
48 ctx.exit() 1abcdefg
50 assert len(param_decls) > 0, "At least one help option should be provided" 1abcdefg
52 return option( 1abcdefg
53 param_decls,
54 is_flag=True,
55 expose_value=False,
56 is_eager=True,
57 help="Show this message and exit.",
58 callback=show_help,
59 required=False,
60 )