Debian Codegraph @

cloup/_params.py

raw
1 import click
2 from click.decorators import _param_memo
3
4 from cloup._util import click_version_ge_8_2
5
6
7 def make_arg_metavar(arg, ctx) -> str:
8 if click_version_ge_8_2:
9 return arg.make_metavar(ctx) # type: ignore[call-arg]
10 return arg.make_metavar() # type: ignore[call-arg]
11
12
13 class Argument(click.Argument):
14 """A :class:`click.Argument` with help text."""
15
16 def __init__(self, *args, help=None, **attrs):
17 super().__init__(*args, **attrs)
18 self.help = help
19
20 def get_help_record(self, ctx):
21 return make_arg_metavar(self, ctx), self.help or ""
22
23
24 class Option(click.Option):
25 """A :class:`click.Option` with an extra field ``group`` of type ``OptionGroup``."""
26
27 def __init__(self, *args, group=None, **attrs):
28 super().__init__(*args, **attrs)
29 self.group = group
30
31
32 GroupedOption = Option
33 """Alias of ``Option``."""
34
35
36 def argument(*param_decls, cls=None, **attrs):
37 ArgumentClass = cls or Argument
38
39 def decorator(f):
40 _param_memo(f, ArgumentClass(param_decls, **attrs))
41 return f
42
43 return decorator
44
45
46 def option(*param_decls, cls=None, group=None, **attrs):
47 """Attach an ``Option`` to the command.
48 Refer to :class:`click.Option` and :class:`click.Parameter` for more info
49 about the accepted parameters.
50
51 In your IDE, you won't see arguments relating to shell completion,
52 because they are different in Click 7 and 8 (both supported by Cloup):
53
54 - in Click 7, it's ``autocompletion``
55 - in Click 8, it's ``shell_complete``.
56
57 These arguments have different semantics, refer to Click's docs.
58 """
59 OptionClass = cls or Option
60
61 def decorator(f):
62 _param_memo(f, OptionClass(param_decls, **attrs))
63 new_option = f.__click_params__[-1]
64 new_option.group = group
65 if group and group.hidden:
66 new_option.hidden = True
67 return f
68
69 return decorator