click-compose 2026.9.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- click_compose/__init__.py +106 -0
- click_compose/py.typed +0 -0
- click_compose-2026.9.8.dist-info/METADATA +174 -0
- click_compose-2026.9.8.dist-info/RECORD +7 -0
- click_compose-2026.9.8.dist-info/WHEEL +5 -0
- click_compose-2026.9.8.dist-info/licenses/LICENSE +21 -0
- click_compose-2026.9.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Composable Click callback utilities for building flexible CLI
|
|
3
|
+
applications.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from collections.abc import Callable, Sequence
|
|
7
|
+
from typing import TypeVar
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
from beartype import beartype
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
U = TypeVar("U")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@beartype
|
|
17
|
+
def sequence_validator(
|
|
18
|
+
*,
|
|
19
|
+
validator: Callable[[click.Context | None, click.Parameter | None, T], U],
|
|
20
|
+
) -> Callable[
|
|
21
|
+
[click.Context | None, click.Parameter | None, Sequence[T] | None],
|
|
22
|
+
Sequence[U] | None,
|
|
23
|
+
]:
|
|
24
|
+
"""Wrap a single-value validator to apply it to a sequence of values.
|
|
25
|
+
|
|
26
|
+
This function takes a Click callback that validates a single value and
|
|
27
|
+
returns a new callback that applies the same validation to each element
|
|
28
|
+
in a sequence. The validator can transform the type of each element.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
validator: A Click callback that validates a single value.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
A Click callback that validates a sequence of values.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def callback(
|
|
38
|
+
ctx: click.Context | None,
|
|
39
|
+
param: click.Parameter | None,
|
|
40
|
+
value: Sequence[T] | None,
|
|
41
|
+
) -> Sequence[U] | None:
|
|
42
|
+
"""Apply the validator to each element in the sequence."""
|
|
43
|
+
if value is None:
|
|
44
|
+
return None
|
|
45
|
+
return_values: list[U] = []
|
|
46
|
+
for item in value:
|
|
47
|
+
returned_value = validator(ctx, param, item)
|
|
48
|
+
return_values.append(returned_value)
|
|
49
|
+
return return_values
|
|
50
|
+
|
|
51
|
+
return callback
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@beartype
|
|
55
|
+
def deduplicate(
|
|
56
|
+
ctx: click.Context | None,
|
|
57
|
+
param: click.Parameter | None,
|
|
58
|
+
sequence: Sequence[T] | None,
|
|
59
|
+
) -> Sequence[T] | None:
|
|
60
|
+
"""
|
|
61
|
+
Return the sequence with duplicates removed while preserving
|
|
62
|
+
order.
|
|
63
|
+
"""
|
|
64
|
+
# We "use" the parameters to silence unused-argument tooling.
|
|
65
|
+
del ctx
|
|
66
|
+
del param
|
|
67
|
+
|
|
68
|
+
if sequence is None:
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
return tuple(dict.fromkeys(sequence).keys())
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@beartype
|
|
75
|
+
def multi_callback(
|
|
76
|
+
*,
|
|
77
|
+
callbacks: Sequence[Callable[..., T]],
|
|
78
|
+
) -> Callable[[click.Context | None, click.Parameter | None, T], T]:
|
|
79
|
+
"""Create a Click-compatible callback that applies multiple callbacks
|
|
80
|
+
in
|
|
81
|
+
sequence.
|
|
82
|
+
|
|
83
|
+
This function takes a sequence of Click callbacks and returns a new
|
|
84
|
+
callback that applies each callback in order, threading the value through
|
|
85
|
+
each one. Each callback can transform the type, allowing for flexible
|
|
86
|
+
pipelines of transformations and validations.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
callbacks: A sequence of Click callbacks to apply in order.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
A Click callback that applies all the given callbacks in sequence.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def callback(
|
|
96
|
+
ctx: click.Context | None,
|
|
97
|
+
param: click.Parameter | None,
|
|
98
|
+
value: T,
|
|
99
|
+
) -> T:
|
|
100
|
+
"""Apply each callback in sequence to the value."""
|
|
101
|
+
result = value
|
|
102
|
+
for cb in callbacks:
|
|
103
|
+
result = cb(ctx, param, result)
|
|
104
|
+
return result
|
|
105
|
+
|
|
106
|
+
return callback
|
click_compose/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: click-compose
|
|
3
|
+
Version: 2026.9.8
|
|
4
|
+
Summary: Composable Click callback utilities for building flexible CLI applications.
|
|
5
|
+
Author-email: Adam Dangoor <adamdangoor@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Source, https://github.com/adamtheturtle/click-compose
|
|
8
|
+
Keywords: callbacks,cli,click,validation
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/x-rst
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: beartype>=0.22.9
|
|
22
|
+
Requires-Dist: click>=8.3.1
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
click-compose
|
|
26
|
+
=============
|
|
27
|
+
|
|
28
|
+
|Build Status| |PyPI|
|
|
29
|
+
|
|
30
|
+
Composable Click callback utilities for building flexible CLI applications.
|
|
31
|
+
|
|
32
|
+
.. |Build Status| image:: https://github.com/adamtheturtle/click-compose/actions/workflows/ci.yml/badge.svg?branch=main
|
|
33
|
+
:target: https://github.com/adamtheturtle/click-compose/actions/workflows/ci.yml
|
|
34
|
+
.. |PyPI| image:: https://badge.fury.io/py/click-compose.svg
|
|
35
|
+
:target: https://badge.fury.io/py/click-compose
|
|
36
|
+
|
|
37
|
+
.. contents::
|
|
38
|
+
:local:
|
|
39
|
+
|
|
40
|
+
Installation
|
|
41
|
+
------------
|
|
42
|
+
|
|
43
|
+
.. code-block:: shell
|
|
44
|
+
|
|
45
|
+
$ pip install click-compose
|
|
46
|
+
|
|
47
|
+
Or with ``uv``:
|
|
48
|
+
|
|
49
|
+
.. code-block:: shell
|
|
50
|
+
|
|
51
|
+
$ uv add click-compose
|
|
52
|
+
|
|
53
|
+
Quick Start
|
|
54
|
+
-----------
|
|
55
|
+
|
|
56
|
+
``click-compose`` provides utilities for composing Click callbacks:
|
|
57
|
+
|
|
58
|
+
multi_callback
|
|
59
|
+
~~~~~~~~~~~~~~
|
|
60
|
+
|
|
61
|
+
Combine multiple callbacks into a single callback that applies them in sequence:
|
|
62
|
+
|
|
63
|
+
.. code-block:: python
|
|
64
|
+
|
|
65
|
+
"""Example of using multi_callback to combine validators."""
|
|
66
|
+
|
|
67
|
+
import click
|
|
68
|
+
|
|
69
|
+
from click_compose import multi_callback
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def validate_positive(
|
|
73
|
+
_ctx: click.Context,
|
|
74
|
+
_param: click.Parameter,
|
|
75
|
+
value: int,
|
|
76
|
+
) -> int:
|
|
77
|
+
"""Validate that value is positive."""
|
|
78
|
+
if value <= 0:
|
|
79
|
+
msg = "Must be positive"
|
|
80
|
+
raise click.BadParameter(message=msg)
|
|
81
|
+
return value
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
MAX_VALUE = 100
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def validate_max_100(
|
|
88
|
+
_ctx: click.Context,
|
|
89
|
+
_param: click.Parameter,
|
|
90
|
+
value: int,
|
|
91
|
+
) -> int:
|
|
92
|
+
"""Validate that value is at most 100."""
|
|
93
|
+
if value > MAX_VALUE:
|
|
94
|
+
msg = "Must be <= 100"
|
|
95
|
+
raise click.BadParameter(message=msg)
|
|
96
|
+
return value
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@click.command()
|
|
100
|
+
@click.option(
|
|
101
|
+
"--count",
|
|
102
|
+
type=int,
|
|
103
|
+
callback=multi_callback(callbacks=[validate_positive, validate_max_100]),
|
|
104
|
+
)
|
|
105
|
+
def cmd(count: int) -> None:
|
|
106
|
+
"""Example command with multiple validators."""
|
|
107
|
+
click.echo(message=f"Count: {count}")
|
|
108
|
+
|
|
109
|
+
sequence_validator
|
|
110
|
+
~~~~~~~~~~~~~~~~~~
|
|
111
|
+
|
|
112
|
+
Apply a validator to each element in a sequence (useful with ``multiple=True``):
|
|
113
|
+
|
|
114
|
+
.. code-block:: python
|
|
115
|
+
|
|
116
|
+
"""Example of using sequence_validator with multiple values."""
|
|
117
|
+
|
|
118
|
+
import click
|
|
119
|
+
|
|
120
|
+
from click_compose import sequence_validator
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def validate_positive(
|
|
124
|
+
_ctx: click.Context | None,
|
|
125
|
+
_param: click.Parameter | None,
|
|
126
|
+
value: int,
|
|
127
|
+
) -> int:
|
|
128
|
+
"""Validate that value is positive."""
|
|
129
|
+
if value <= 0:
|
|
130
|
+
msg = "Must be positive"
|
|
131
|
+
raise click.BadParameter(message=msg)
|
|
132
|
+
return value
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@click.command()
|
|
136
|
+
@click.option(
|
|
137
|
+
"--numbers",
|
|
138
|
+
multiple=True,
|
|
139
|
+
type=int,
|
|
140
|
+
callback=sequence_validator(validator=validate_positive),
|
|
141
|
+
)
|
|
142
|
+
def cmd(numbers: tuple[int, ...]) -> None:
|
|
143
|
+
"""Example command with sequence validation."""
|
|
144
|
+
click.echo(message=f"Sum: {sum(numbers)}")
|
|
145
|
+
|
|
146
|
+
deduplicate
|
|
147
|
+
~~~~~~~~~~~
|
|
148
|
+
|
|
149
|
+
Remove duplicates from a sequence while preserving order (useful with ``multiple=True``):
|
|
150
|
+
|
|
151
|
+
.. code-block:: python
|
|
152
|
+
|
|
153
|
+
"""Example of using ``deduplicate`` to remove duplicate values."""
|
|
154
|
+
|
|
155
|
+
import click
|
|
156
|
+
|
|
157
|
+
from click_compose import deduplicate
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@click.command()
|
|
161
|
+
@click.option(
|
|
162
|
+
"--tags",
|
|
163
|
+
multiple=True,
|
|
164
|
+
type=str,
|
|
165
|
+
callback=deduplicate,
|
|
166
|
+
)
|
|
167
|
+
def cmd(tags: tuple[str, ...]) -> None:
|
|
168
|
+
"""Example command that removes duplicate tags."""
|
|
169
|
+
click.echo(message=f"Unique tags: {', '.join(tags)}")
|
|
170
|
+
|
|
171
|
+
Documentation
|
|
172
|
+
-------------
|
|
173
|
+
|
|
174
|
+
See the `full documentation <https://adamtheturtle.github.io/click-compose/>`__.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
click_compose/__init__.py,sha256=oBDHYOys7KbmkY0qj492Agnb36j_7rR60iDkw5VeqhQ,2881
|
|
2
|
+
click_compose/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
click_compose-2026.9.8.dist-info/licenses/LICENSE,sha256=N3qm7laipxyuXkWpYllj89AObvPbtWgoABmaKfhQYL4,1069
|
|
4
|
+
click_compose-2026.9.8.dist-info/METADATA,sha256=GeTU2l4MZIjhZv93J49iobth61KEKtRHysGZkBs0Lr0,4426
|
|
5
|
+
click_compose-2026.9.8.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
6
|
+
click_compose-2026.9.8.dist-info/top_level.txt,sha256=D6w2GKY-8Qwmbiv3G8DyrtwEjiu-yd7I6n6IpPIUauU,14
|
|
7
|
+
click_compose-2026.9.8.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Adam Dangoor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
click_compose
|