click-extended 0.0.3__tar.gz → 0.1.0__tar.gz
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_extended-0.1.0/PKG-INFO +257 -0
- click_extended-0.1.0/README.md +194 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended/__init__.py +10 -1
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended/errors.py +121 -3
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended/types.py +10 -11
- click_extended-0.1.0/click_extended.egg-info/PKG-INFO +257 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/pyproject.toml +5 -2
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_argument.py +322 -6
- click_extended-0.1.0/tests/test_child_node.py +909 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_env.py +2 -10
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_option.py +113 -1
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_parent_node.py +36 -168
- click_extended-0.0.3/PKG-INFO +0 -101
- click_extended-0.0.3/README.md +0 -38
- click_extended-0.0.3/click_extended.egg-info/PKG-INFO +0 -101
- click_extended-0.0.3/tests/test_child_node.py +0 -525
- {click_extended-0.0.3 → click_extended-0.1.0}/AUTHORS.md +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/LICENSE +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended.egg-info/SOURCES.txt +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended.egg-info/dependency_links.txt +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended.egg-info/requires.txt +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/click_extended.egg-info/top_level.txt +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/setup.cfg +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_command.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_global_node.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_group.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_root_node.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_tag.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_transform.py +0 -0
- {click_extended-0.0.3 → click_extended-0.1.0}/tests/test_tree.py +0 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: click_extended
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An extension to Click with additional features like automatic async support, aliasing and a modular decorator system.
|
|
5
|
+
Author-email: Marcus Fredriksson <marcus@marcusfredriksson.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Marcus Fredriksson
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/marcusfrdk/click-extended
|
|
29
|
+
Project-URL: Repository, https://github.com/marcusfrdk/click-extended
|
|
30
|
+
Project-URL: Issues, https://github.com/marcusfrdk/click-extended/issues
|
|
31
|
+
Keywords: click,cli,command-line,alias,aliasing,command,group,decorator,terminal,console
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
40
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
41
|
+
Classifier: Topic :: System :: Shells
|
|
42
|
+
Classifier: Topic :: Utilities
|
|
43
|
+
Classifier: Typing :: Typed
|
|
44
|
+
Requires-Python: >=3.10
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
License-File: LICENSE
|
|
47
|
+
License-File: AUTHORS.md
|
|
48
|
+
Requires-Dist: click>=8.3.0
|
|
49
|
+
Requires-Dist: python-dotenv>=1.2.1
|
|
50
|
+
Provides-Extra: build
|
|
51
|
+
Requires-Dist: build; extra == "build"
|
|
52
|
+
Requires-Dist: twine; extra == "build"
|
|
53
|
+
Provides-Extra: dev
|
|
54
|
+
Requires-Dist: pytest>=8.4.2; extra == "dev"
|
|
55
|
+
Requires-Dist: pytest-cov>=7.0.0; extra == "dev"
|
|
56
|
+
Requires-Dist: pytest-asyncio>=1.2.0; extra == "dev"
|
|
57
|
+
Requires-Dist: mypy>=1.18.2; extra == "dev"
|
|
58
|
+
Requires-Dist: pylint>=3.0.0; extra == "dev"
|
|
59
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
60
|
+
Requires-Dist: black>=25.9.0; extra == "dev"
|
|
61
|
+
Requires-Dist: pre-commit>=4.3.0; extra == "dev"
|
|
62
|
+
Dynamic: license-file
|
|
63
|
+
|
|
64
|
+

|
|
65
|
+
|
|
66
|
+
# Click Extended
|
|
67
|
+
|
|
68
|
+

|
|
69
|
+

|
|
70
|
+

|
|
71
|
+

|
|
72
|
+

|
|
73
|
+

|
|
74
|
+

|
|
75
|
+

|
|
76
|
+

|
|
77
|
+
|
|
78
|
+
An extension of the [Click](https://github.com/pallets/click) library with additional features like aliasing, asynchronous support, an extended decorator API and more.
|
|
79
|
+
|
|
80
|
+
## Features
|
|
81
|
+
|
|
82
|
+
- **Aliasing**: Add multiple aliases to a group or command.
|
|
83
|
+
- **Async support**: Automatically run both synchronous and asynchronous functions.
|
|
84
|
+
- **Extensible decorator API**: Extend the Click decorator API with custom decorators like validators, transformers, and more.
|
|
85
|
+
- **Type-hint First**: Built using the type-hinting system to it's full potential.
|
|
86
|
+
- **Environment variables**: Automatically validate and inject environment variables into the function.
|
|
87
|
+
- **Help alias**: The `-h` and `--help` automatically show the help menu unless overridden.
|
|
88
|
+
|
|
89
|
+
## Installation
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pip install click-extended
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Requirements
|
|
96
|
+
|
|
97
|
+
- **Python**: 3.10 or higher
|
|
98
|
+
|
|
99
|
+
## Quick Start
|
|
100
|
+
|
|
101
|
+
### Basic Command
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from click_extended import command, option
|
|
105
|
+
|
|
106
|
+
@command()
|
|
107
|
+
@option("--name", default="World", help="Name to greet")
|
|
108
|
+
@option("--count", type=int, default=1, help="Number of greetings")
|
|
109
|
+
def greet(name: str, count: int):
|
|
110
|
+
"""Greet someone multiple times."""
|
|
111
|
+
for _ in range(count):
|
|
112
|
+
print(f"Hello, {name}!")
|
|
113
|
+
|
|
114
|
+
if __name__ == "__main__":
|
|
115
|
+
greet()
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
$ python app.py --name Alice --count 3
|
|
120
|
+
Hello, Alice!
|
|
121
|
+
Hello, Alice!
|
|
122
|
+
Hello, Alice!
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Async Support
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
import asyncio
|
|
129
|
+
|
|
130
|
+
from click_extended import command, option
|
|
131
|
+
|
|
132
|
+
@command()
|
|
133
|
+
@option("--url", required=True, help="URL to fetch")
|
|
134
|
+
async def fetch(url: str):
|
|
135
|
+
"""Fetch data from a URL asynchronously."""
|
|
136
|
+
await asyncio.sleep(1)
|
|
137
|
+
print(f"Fetched data from {url}")
|
|
138
|
+
|
|
139
|
+
if __name__ == "__main__":
|
|
140
|
+
fetch()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Command Aliases
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from click_extended import command, option
|
|
147
|
+
|
|
148
|
+
@command(aliases=["hi", "hello"])
|
|
149
|
+
@option("--name", default="World")
|
|
150
|
+
def greet(name: str):
|
|
151
|
+
"""Greet someone."""
|
|
152
|
+
print(f"Hello, {name}!")
|
|
153
|
+
|
|
154
|
+
if __name__ == "__main__":
|
|
155
|
+
greet()
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
$ python app.py greet --name Alice
|
|
160
|
+
Hello, Alice!
|
|
161
|
+
$ python app.py hi --name Bob
|
|
162
|
+
Hello, Bob!
|
|
163
|
+
$ python app.py hello --name Charlie
|
|
164
|
+
Hello, Charlie!
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Environment Variables
|
|
168
|
+
|
|
169
|
+
Environment variables are automatically loaded from the `.env` file, but as long as the variable is defined in your system environment, it will work.
|
|
170
|
+
|
|
171
|
+
```txt
|
|
172
|
+
API_TOKEN=secret123
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from click_extended import command, option, env
|
|
177
|
+
|
|
178
|
+
@command()
|
|
179
|
+
@option("--token", help="API token")
|
|
180
|
+
@env("API_TOKEN", name="token", required=True)
|
|
181
|
+
def api_call(token: str):
|
|
182
|
+
"""Make an API call with authentication."""
|
|
183
|
+
print(f"Using token: {token[:8]}...")
|
|
184
|
+
|
|
185
|
+
if __name__ == "__main__":
|
|
186
|
+
api_call()
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
$ python app.py
|
|
191
|
+
Using token: secret12...
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Custom Validators
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
from click_extended import command, option, ChildNode, ProcessContext
|
|
198
|
+
from click_extended.errors import ValidationError
|
|
199
|
+
|
|
200
|
+
class IsPositive(ChildNode):
|
|
201
|
+
"""Validate that a number is positive."""
|
|
202
|
+
|
|
203
|
+
def process(self, value: float | int, context: ProcessContext):
|
|
204
|
+
if value <= 0:
|
|
205
|
+
raise ValidationError(f"{value} is not positive")
|
|
206
|
+
|
|
207
|
+
def is_positive(*args, **kwargs):
|
|
208
|
+
"""Validate positive numbers."""
|
|
209
|
+
return IsPositive.as_decorator(*args, **kwargs)
|
|
210
|
+
|
|
211
|
+
@command()
|
|
212
|
+
@option("--count", type=int, required=True)
|
|
213
|
+
@is_positive()
|
|
214
|
+
def process(count: int):
|
|
215
|
+
"""Process a positive number of items."""
|
|
216
|
+
print(f"Processing {count} items")
|
|
217
|
+
|
|
218
|
+
if __name__ == "__main__":
|
|
219
|
+
process()
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
$ python app.py --count 5
|
|
224
|
+
Processing 5 items
|
|
225
|
+
$ python app.py --count -1
|
|
226
|
+
Usage: app.py [OPTIONS]
|
|
227
|
+
Try 'app.py --help' for help.
|
|
228
|
+
|
|
229
|
+
Error (--count): -1 is not positive
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Documentation
|
|
233
|
+
|
|
234
|
+
### Core Concepts
|
|
235
|
+
|
|
236
|
+
- [Commands and Groups](./docs/ROOT_NODE.md) - CLI entry points
|
|
237
|
+
- [Options, Arguments, and Environment Variables](./docs/PARENT_NODE.md) - Parameter sources
|
|
238
|
+
- [Validators and Transformers](./docs/CHILD_NODE.md) - Value processing
|
|
239
|
+
- [Global Nodes](./docs/GLOBAL_NODE.md) - Tree-level operations
|
|
240
|
+
- [Tags](./docs/TAG.md) - Cross-parameter validation
|
|
241
|
+
- [Tree Architecture](./docs/TREE.md) - Internal structure
|
|
242
|
+
|
|
243
|
+
### Guides
|
|
244
|
+
|
|
245
|
+
- [Migrating from Click](./docs/MIGRATING_FROM_CLICK.md) - Upgrade guide
|
|
246
|
+
|
|
247
|
+
## Contributing
|
|
248
|
+
|
|
249
|
+
Contributors are more than welcome to work on this project. Read the [contribution documentation](./CONTRIBUTING.md) to learn more.
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
254
|
+
|
|
255
|
+
## Acknowledgements
|
|
256
|
+
|
|
257
|
+
This project is built on top of the [Click](https://github.com/pallets/click) library.
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# Click Extended
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
An extension of the [Click](https://github.com/pallets/click) library with additional features like aliasing, asynchronous support, an extended decorator API and more.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Aliasing**: Add multiple aliases to a group or command.
|
|
20
|
+
- **Async support**: Automatically run both synchronous and asynchronous functions.
|
|
21
|
+
- **Extensible decorator API**: Extend the Click decorator API with custom decorators like validators, transformers, and more.
|
|
22
|
+
- **Type-hint First**: Built using the type-hinting system to it's full potential.
|
|
23
|
+
- **Environment variables**: Automatically validate and inject environment variables into the function.
|
|
24
|
+
- **Help alias**: The `-h` and `--help` automatically show the help menu unless overridden.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install click-extended
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Requirements
|
|
33
|
+
|
|
34
|
+
- **Python**: 3.10 or higher
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
### Basic Command
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from click_extended import command, option
|
|
42
|
+
|
|
43
|
+
@command()
|
|
44
|
+
@option("--name", default="World", help="Name to greet")
|
|
45
|
+
@option("--count", type=int, default=1, help="Number of greetings")
|
|
46
|
+
def greet(name: str, count: int):
|
|
47
|
+
"""Greet someone multiple times."""
|
|
48
|
+
for _ in range(count):
|
|
49
|
+
print(f"Hello, {name}!")
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
greet()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
$ python app.py --name Alice --count 3
|
|
57
|
+
Hello, Alice!
|
|
58
|
+
Hello, Alice!
|
|
59
|
+
Hello, Alice!
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Async Support
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import asyncio
|
|
66
|
+
|
|
67
|
+
from click_extended import command, option
|
|
68
|
+
|
|
69
|
+
@command()
|
|
70
|
+
@option("--url", required=True, help="URL to fetch")
|
|
71
|
+
async def fetch(url: str):
|
|
72
|
+
"""Fetch data from a URL asynchronously."""
|
|
73
|
+
await asyncio.sleep(1)
|
|
74
|
+
print(f"Fetched data from {url}")
|
|
75
|
+
|
|
76
|
+
if __name__ == "__main__":
|
|
77
|
+
fetch()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Command Aliases
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from click_extended import command, option
|
|
84
|
+
|
|
85
|
+
@command(aliases=["hi", "hello"])
|
|
86
|
+
@option("--name", default="World")
|
|
87
|
+
def greet(name: str):
|
|
88
|
+
"""Greet someone."""
|
|
89
|
+
print(f"Hello, {name}!")
|
|
90
|
+
|
|
91
|
+
if __name__ == "__main__":
|
|
92
|
+
greet()
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
$ python app.py greet --name Alice
|
|
97
|
+
Hello, Alice!
|
|
98
|
+
$ python app.py hi --name Bob
|
|
99
|
+
Hello, Bob!
|
|
100
|
+
$ python app.py hello --name Charlie
|
|
101
|
+
Hello, Charlie!
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Environment Variables
|
|
105
|
+
|
|
106
|
+
Environment variables are automatically loaded from the `.env` file, but as long as the variable is defined in your system environment, it will work.
|
|
107
|
+
|
|
108
|
+
```txt
|
|
109
|
+
API_TOKEN=secret123
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from click_extended import command, option, env
|
|
114
|
+
|
|
115
|
+
@command()
|
|
116
|
+
@option("--token", help="API token")
|
|
117
|
+
@env("API_TOKEN", name="token", required=True)
|
|
118
|
+
def api_call(token: str):
|
|
119
|
+
"""Make an API call with authentication."""
|
|
120
|
+
print(f"Using token: {token[:8]}...")
|
|
121
|
+
|
|
122
|
+
if __name__ == "__main__":
|
|
123
|
+
api_call()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
$ python app.py
|
|
128
|
+
Using token: secret12...
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Custom Validators
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from click_extended import command, option, ChildNode, ProcessContext
|
|
135
|
+
from click_extended.errors import ValidationError
|
|
136
|
+
|
|
137
|
+
class IsPositive(ChildNode):
|
|
138
|
+
"""Validate that a number is positive."""
|
|
139
|
+
|
|
140
|
+
def process(self, value: float | int, context: ProcessContext):
|
|
141
|
+
if value <= 0:
|
|
142
|
+
raise ValidationError(f"{value} is not positive")
|
|
143
|
+
|
|
144
|
+
def is_positive(*args, **kwargs):
|
|
145
|
+
"""Validate positive numbers."""
|
|
146
|
+
return IsPositive.as_decorator(*args, **kwargs)
|
|
147
|
+
|
|
148
|
+
@command()
|
|
149
|
+
@option("--count", type=int, required=True)
|
|
150
|
+
@is_positive()
|
|
151
|
+
def process(count: int):
|
|
152
|
+
"""Process a positive number of items."""
|
|
153
|
+
print(f"Processing {count} items")
|
|
154
|
+
|
|
155
|
+
if __name__ == "__main__":
|
|
156
|
+
process()
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
$ python app.py --count 5
|
|
161
|
+
Processing 5 items
|
|
162
|
+
$ python app.py --count -1
|
|
163
|
+
Usage: app.py [OPTIONS]
|
|
164
|
+
Try 'app.py --help' for help.
|
|
165
|
+
|
|
166
|
+
Error (--count): -1 is not positive
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Documentation
|
|
170
|
+
|
|
171
|
+
### Core Concepts
|
|
172
|
+
|
|
173
|
+
- [Commands and Groups](./docs/ROOT_NODE.md) - CLI entry points
|
|
174
|
+
- [Options, Arguments, and Environment Variables](./docs/PARENT_NODE.md) - Parameter sources
|
|
175
|
+
- [Validators and Transformers](./docs/CHILD_NODE.md) - Value processing
|
|
176
|
+
- [Global Nodes](./docs/GLOBAL_NODE.md) - Tree-level operations
|
|
177
|
+
- [Tags](./docs/TAG.md) - Cross-parameter validation
|
|
178
|
+
- [Tree Architecture](./docs/TREE.md) - Internal structure
|
|
179
|
+
|
|
180
|
+
### Guides
|
|
181
|
+
|
|
182
|
+
- [Migrating from Click](./docs/MIGRATING_FROM_CLICK.md) - Upgrade guide
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
Contributors are more than welcome to work on this project. Read the [contribution documentation](./CONTRIBUTING.md) to learn more.
|
|
187
|
+
|
|
188
|
+
## License
|
|
189
|
+
|
|
190
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
191
|
+
|
|
192
|
+
## Acknowledgements
|
|
193
|
+
|
|
194
|
+
This project is built on top of the [Click](https://github.com/pallets/click) library.
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"""Initialization file for the 'click_extended' module."""
|
|
2
2
|
|
|
3
|
-
from click_extended.core._child_node import ChildNode
|
|
3
|
+
from click_extended.core._child_node import ChildNode, ProcessContext
|
|
4
|
+
from click_extended.core._global_node import GlobalNode
|
|
5
|
+
from click_extended.core._node import Node
|
|
4
6
|
from click_extended.core._parent_node import ParentNode
|
|
7
|
+
from click_extended.core._root_node import RootNode
|
|
8
|
+
from click_extended.core._tree import Tree
|
|
5
9
|
from click_extended.core.argument import argument
|
|
6
10
|
from click_extended.core.command import command
|
|
7
11
|
from click_extended.core.env import env
|
|
@@ -11,7 +15,12 @@ from click_extended.core.tag import tag
|
|
|
11
15
|
|
|
12
16
|
__all__ = [
|
|
13
17
|
"ChildNode",
|
|
18
|
+
"ProcessContext",
|
|
19
|
+
"GlobalNode",
|
|
20
|
+
"Node",
|
|
14
21
|
"ParentNode",
|
|
22
|
+
"RootNode",
|
|
23
|
+
"Tree",
|
|
15
24
|
"argument",
|
|
16
25
|
"command",
|
|
17
26
|
"env",
|
|
@@ -3,21 +3,100 @@
|
|
|
3
3
|
# pylint: disable=too-many-arguments
|
|
4
4
|
# pylint: disable=too-many-positional-arguments
|
|
5
5
|
|
|
6
|
+
import typing as t
|
|
7
|
+
|
|
8
|
+
import click
|
|
9
|
+
from click import ClickException
|
|
10
|
+
from click._compat import get_text_stderr
|
|
11
|
+
from click.utils import echo
|
|
12
|
+
|
|
6
13
|
|
|
7
14
|
class ClickExtendedError(Exception):
|
|
8
15
|
"""Base exception for exceptions defined in the `click_extended` library."""
|
|
9
16
|
|
|
17
|
+
|
|
18
|
+
class CatchableError(ClickExtendedError):
|
|
19
|
+
"""Base exception for exceptions raised inside a child node.
|
|
20
|
+
|
|
21
|
+
These exceptions are caught by the framework and reformatted with
|
|
22
|
+
parameter context before being displayed to the user.
|
|
23
|
+
"""
|
|
24
|
+
|
|
10
25
|
def __init__(self, message: str) -> None:
|
|
11
26
|
"""
|
|
12
|
-
Initialize a
|
|
27
|
+
Initialize a CatchableError.
|
|
13
28
|
|
|
14
29
|
Args:
|
|
15
|
-
message
|
|
16
|
-
The message to show.
|
|
30
|
+
message: The error message describing what went wrong.
|
|
17
31
|
"""
|
|
18
32
|
super().__init__(message)
|
|
19
33
|
|
|
20
34
|
|
|
35
|
+
class ValidationError(CatchableError):
|
|
36
|
+
"""Exception raised when validation fails in a child node."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class TransformError(CatchableError):
|
|
40
|
+
"""Exception raised when transformation fails in a child node."""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class ParameterError(ClickException):
|
|
44
|
+
"""Exception raised when parameter validation or transformation fails.
|
|
45
|
+
|
|
46
|
+
This exception is raised by the framework after catching a CatchableError
|
|
47
|
+
and adding parameter context information.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
exit_code = 2 # Match Click's UsageError exit code
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
message: str,
|
|
55
|
+
param_hint: str | None = None,
|
|
56
|
+
ctx: click.Context | None = None,
|
|
57
|
+
) -> None:
|
|
58
|
+
"""
|
|
59
|
+
Initialize a ParameterError.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
message: The error message from the validator/transformer.
|
|
63
|
+
param_hint: The parameter name (e.g., '--config', 'PATH').
|
|
64
|
+
ctx: The Click context for displaying usage information.
|
|
65
|
+
"""
|
|
66
|
+
super().__init__(message)
|
|
67
|
+
self.param_hint = param_hint
|
|
68
|
+
self.ctx = ctx
|
|
69
|
+
|
|
70
|
+
def format_message(self) -> str:
|
|
71
|
+
"""Format the error message with parameter context."""
|
|
72
|
+
if self.param_hint:
|
|
73
|
+
return f"({self.param_hint}): {self.message}"
|
|
74
|
+
return self.message
|
|
75
|
+
|
|
76
|
+
def show(self, file: t.IO[t.Any] | None = None) -> None:
|
|
77
|
+
"""Display the error with usage information (like Click does)."""
|
|
78
|
+
if file is None:
|
|
79
|
+
file = get_text_stderr()
|
|
80
|
+
|
|
81
|
+
color = None
|
|
82
|
+
|
|
83
|
+
if self.ctx is not None:
|
|
84
|
+
color = self.ctx.color
|
|
85
|
+
|
|
86
|
+
echo(self.ctx.get_usage(), file=file, color=color)
|
|
87
|
+
|
|
88
|
+
if self.ctx.command.get_help_option(self.ctx) is not None:
|
|
89
|
+
hint = (
|
|
90
|
+
f"Try '{self.ctx.command_path} "
|
|
91
|
+
f"{self.ctx.help_option_names[0]}' for help."
|
|
92
|
+
)
|
|
93
|
+
echo(hint, file=file, color=color)
|
|
94
|
+
|
|
95
|
+
echo("", file=file)
|
|
96
|
+
|
|
97
|
+
echo(f"Error {self.format_message()}", file=file, color=color)
|
|
98
|
+
|
|
99
|
+
|
|
21
100
|
class NoParentError(ClickExtendedError):
|
|
22
101
|
"""Exception raised when no `ParentNode` has been defined."""
|
|
23
102
|
|
|
@@ -126,3 +205,42 @@ class DuplicateNameError(ClickExtendedError):
|
|
|
126
205
|
f"must be unique within a command."
|
|
127
206
|
)
|
|
128
207
|
super().__init__(message)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class TypeMismatchError(ClickExtendedError):
|
|
211
|
+
"""Exception raised when a child node doesn't support the parent's type."""
|
|
212
|
+
|
|
213
|
+
def __init__(
|
|
214
|
+
self,
|
|
215
|
+
name: str,
|
|
216
|
+
parent_name: str,
|
|
217
|
+
parent_type: type | None,
|
|
218
|
+
supported_types: list[type],
|
|
219
|
+
) -> None:
|
|
220
|
+
"""
|
|
221
|
+
Initialize a new `TypeMismatchError` instance.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
name (str):
|
|
225
|
+
The name of the decorator.
|
|
226
|
+
parent_name (str):
|
|
227
|
+
The name of the parent node.
|
|
228
|
+
parent_type (type | None):
|
|
229
|
+
The actual type of the parent.
|
|
230
|
+
supported_types (list[type]):
|
|
231
|
+
List of types supported by the child node.
|
|
232
|
+
"""
|
|
233
|
+
|
|
234
|
+
def get_type_name(type_obj: type) -> str:
|
|
235
|
+
"""Get type name, handling both regular types and UnionType."""
|
|
236
|
+
return getattr(type_obj, "__name__", str(type_obj))
|
|
237
|
+
|
|
238
|
+
type_names = ", ".join(get_type_name(t) for t in supported_types)
|
|
239
|
+
parent_type_name = get_type_name(parent_type) if parent_type else "None"
|
|
240
|
+
|
|
241
|
+
message = (
|
|
242
|
+
f"Decorator '{name}' does not support "
|
|
243
|
+
f"parent '{parent_name}' with type '{parent_type_name}'. "
|
|
244
|
+
f"Supported types: {type_names}"
|
|
245
|
+
)
|
|
246
|
+
super().__init__(message)
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"""Types used in `click_extended` which can be useful for users."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
from
|
|
6
|
-
|
|
7
|
-
from click_extended.core._tree import Tree
|
|
3
|
+
# pylint: disable=invalid-name
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Callable
|
|
6
|
+
|
|
8
7
|
from click_extended.core.argument import Argument
|
|
9
8
|
from click_extended.core.command import Command
|
|
10
9
|
from click_extended.core.env import Env
|
|
@@ -12,17 +11,17 @@ from click_extended.core.group import Group
|
|
|
12
11
|
from click_extended.core.option import Option
|
|
13
12
|
from click_extended.core.tag import Tag
|
|
14
13
|
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from click_extended.core._parent_node import ParentNode
|
|
16
|
+
|
|
15
17
|
Tags = dict[str, Tag]
|
|
16
18
|
Siblings = list[str]
|
|
17
|
-
Parent = ParentNode | Tag
|
|
19
|
+
Parent = "ParentNode | Tag"
|
|
20
|
+
Decorator = Callable[[Callable[..., Any]], Callable[..., Any]]
|
|
18
21
|
|
|
19
22
|
__all__ = [
|
|
20
|
-
"
|
|
21
|
-
"Node",
|
|
23
|
+
"Decorator",
|
|
22
24
|
"Parent",
|
|
23
|
-
"ParentNode",
|
|
24
|
-
"RootNode",
|
|
25
|
-
"Tree",
|
|
26
25
|
"Argument",
|
|
27
26
|
"Command",
|
|
28
27
|
"Env",
|