cli3 2.0.0__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.
- cli/__init__.py +168 -0
- cli/arg.py +69 -0
- cli/cmd.py +473 -0
- cli/context.py +362 -0
- cli/errors.py +199 -0
- cli/flag.py +129 -0
- cli/help.py +228 -0
- cli/parser.py +440 -0
- cli/py.typed +0 -0
- cli/style.py +180 -0
- cli/suggest.py +256 -0
- cli/tests/__init__.py +15 -0
- cli/tests/_apps/__init__.py +19 -0
- cli/tests/_apps/v1.py +61 -0
- cli/tests/_apps/v2.py +76 -0
- cli/tests/parser.py +294 -0
- cli/tests/suggest.py +77 -0
- cli/utils.py +374 -0
- cli/validate.py +171 -0
- cli/wraps.py +371 -0
- cli3-2.0.0.dist-info/METADATA +218 -0
- cli3-2.0.0.dist-info/RECORD +25 -0
- cli3-2.0.0.dist-info/WHEEL +5 -0
- cli3-2.0.0.dist-info/licenses/LICENSE +21 -0
- cli3-2.0.0.dist-info/top_level.txt +1 -0
cli/wraps.py
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI Action Argument Construction Wrappers
|
|
3
|
+
"""
|
|
4
|
+
import asyncio
|
|
5
|
+
import inspect
|
|
6
|
+
import functools
|
|
7
|
+
import contextvars
|
|
8
|
+
from typing import (
|
|
9
|
+
Any, Awaitable, Callable, Dict, List, NamedTuple, Optional,
|
|
10
|
+
Set, Type, TypeVar, Union, cast)
|
|
11
|
+
from typing_extensions import Annotated, ParamSpec, get_args, get_origin
|
|
12
|
+
|
|
13
|
+
from . import T
|
|
14
|
+
from .arg import Arg
|
|
15
|
+
from .flag import Flag
|
|
16
|
+
from .cmd import Command
|
|
17
|
+
from .context import Context, get_dict
|
|
18
|
+
from .parser import ParseCtx
|
|
19
|
+
|
|
20
|
+
#** Variables **#
|
|
21
|
+
__all__ = [
|
|
22
|
+
'Doc',
|
|
23
|
+
'Inspected',
|
|
24
|
+
'Extra',
|
|
25
|
+
|
|
26
|
+
'wrap_async',
|
|
27
|
+
'call_async',
|
|
28
|
+
'get_signature',
|
|
29
|
+
'parse_doc',
|
|
30
|
+
'into_command',
|
|
31
|
+
'wrap_ctx',
|
|
32
|
+
'wrap_validator',
|
|
33
|
+
'wrap_suggestor',
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
P = ParamSpec('P')
|
|
37
|
+
R = TypeVar('R')
|
|
38
|
+
|
|
39
|
+
#: hidden attribute tied to context wrapper
|
|
40
|
+
WRAP_CTX_ATTR = '__cli_wrapped_ctx'
|
|
41
|
+
|
|
42
|
+
#: hidden attribute used to cache arg definitions
|
|
43
|
+
ARG_ATTR = '__cli_arg'
|
|
44
|
+
|
|
45
|
+
#: hidden attribute used to cache flag definitions
|
|
46
|
+
FLAG_ATTR = '__cli_flags'
|
|
47
|
+
|
|
48
|
+
#: hidden attribute used to cache extra definitions
|
|
49
|
+
EXTRA_ATTR = '__cli_extra'
|
|
50
|
+
|
|
51
|
+
#** Classes **#
|
|
52
|
+
|
|
53
|
+
class Doc(NamedTuple):
|
|
54
|
+
about: str
|
|
55
|
+
params: Dict[str, str]
|
|
56
|
+
|
|
57
|
+
class Inspected(NamedTuple):
|
|
58
|
+
args: List[str]
|
|
59
|
+
kwargs: List[str]
|
|
60
|
+
arg_splat: Optional[str]
|
|
61
|
+
kw_splat: Optional[str]
|
|
62
|
+
defaults: Dict[str, Any]
|
|
63
|
+
typehints: Dict[str, Type]
|
|
64
|
+
|
|
65
|
+
class Extra:
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
#** Functions **#
|
|
69
|
+
|
|
70
|
+
#NOTE: backport of `asyncio.to_thread` for python 3.8
|
|
71
|
+
async def to_thread_manual(func: Callable[..., T], /, *args, **kwargs) -> T:
|
|
72
|
+
loop = asyncio.get_running_loop()
|
|
73
|
+
ctx = contextvars.copy_context()
|
|
74
|
+
func_call = functools.partial(ctx.run, func, *args, **kwargs)
|
|
75
|
+
return await loop.run_in_executor(None, func_call)
|
|
76
|
+
to_thread = getattr(asyncio, 'to_thread', to_thread_manual)
|
|
77
|
+
|
|
78
|
+
def wrap_async(
|
|
79
|
+
action: Union[Callable[P, Awaitable[T]], Callable[P, T]],
|
|
80
|
+
) -> Callable[P, Awaitable[T]]:
|
|
81
|
+
"""
|
|
82
|
+
convert action into async-action
|
|
83
|
+
|
|
84
|
+
:param action: action callback
|
|
85
|
+
:return: async action callback
|
|
86
|
+
"""
|
|
87
|
+
if inspect.iscoroutinefunction(action):
|
|
88
|
+
return action
|
|
89
|
+
|
|
90
|
+
action = cast(Callable[P, T], action)
|
|
91
|
+
@functools.wraps(action)
|
|
92
|
+
async def inner(*args, **kwargs) -> T:
|
|
93
|
+
return await to_thread(action, *args, **kwargs)
|
|
94
|
+
return inner
|
|
95
|
+
|
|
96
|
+
def call_async(co: Union[Awaitable[T], T], loop: asyncio.AbstractEventLoop) -> T:
|
|
97
|
+
"""
|
|
98
|
+
ensure co-routine is run to completion if co-routine
|
|
99
|
+
"""
|
|
100
|
+
if inspect.isawaitable(co):
|
|
101
|
+
return loop.run_until_complete(co)
|
|
102
|
+
return co
|
|
103
|
+
|
|
104
|
+
@functools.lru_cache(maxsize=None)
|
|
105
|
+
def get_signature(callable: Callable) -> Inspected:
|
|
106
|
+
"""
|
|
107
|
+
parse elements of function signature from the given callable
|
|
108
|
+
"""
|
|
109
|
+
sig = inspect.signature(callable)
|
|
110
|
+
args = []
|
|
111
|
+
kwargs = []
|
|
112
|
+
defaults = {}
|
|
113
|
+
typehints = {}
|
|
114
|
+
arg_splat = None
|
|
115
|
+
kw_splat = None
|
|
116
|
+
for p in sig.parameters.values():
|
|
117
|
+
if p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD):
|
|
118
|
+
args.append(p.name)
|
|
119
|
+
elif p.kind == p.KEYWORD_ONLY:
|
|
120
|
+
kwargs.append(p.name)
|
|
121
|
+
elif p.kind == p.VAR_POSITIONAL:
|
|
122
|
+
arg_splat = p.name
|
|
123
|
+
elif p.kind == p.VAR_KEYWORD:
|
|
124
|
+
kw_splat = p.name
|
|
125
|
+
if p.annotation != p.empty:
|
|
126
|
+
typehints[p.name] = p.annotation
|
|
127
|
+
if p.default != p.empty:
|
|
128
|
+
defaults[p.name] = p.default
|
|
129
|
+
if p.name not in typehints:
|
|
130
|
+
typehints[p.name] = type(p.default)
|
|
131
|
+
return Inspected(args, kwargs, arg_splat, kw_splat, defaults, typehints)
|
|
132
|
+
|
|
133
|
+
@functools.lru_cache(maxsize=None)
|
|
134
|
+
def parse_doc(callable: Callable):
|
|
135
|
+
"""
|
|
136
|
+
parse a function docstring into description elements of parameters/command
|
|
137
|
+
"""
|
|
138
|
+
doc = callable.__doc__ or ''
|
|
139
|
+
about = []
|
|
140
|
+
strip = '@:<{}[]- \t\r'
|
|
141
|
+
descriptions = {}
|
|
142
|
+
for line in doc.splitlines():
|
|
143
|
+
line = line.strip()
|
|
144
|
+
if not line:
|
|
145
|
+
continue
|
|
146
|
+
|
|
147
|
+
if not line.startswith(':'):
|
|
148
|
+
about.append(line)
|
|
149
|
+
continue
|
|
150
|
+
|
|
151
|
+
details = line.strip('@:').strip().split(' ', 2)
|
|
152
|
+
if details[0].lower() != 'param':
|
|
153
|
+
continue
|
|
154
|
+
|
|
155
|
+
table = any(details[1].startswith(c) for c in '{<[:')
|
|
156
|
+
param, usage = details[2].split(' ', 1) if table else tuple(details[1:])
|
|
157
|
+
descriptions[param.strip(strip)] = usage.strip(strip)
|
|
158
|
+
return Doc(' '.join(about), descriptions)
|
|
159
|
+
|
|
160
|
+
def is_context(typedef: Type) -> bool:
|
|
161
|
+
"""
|
|
162
|
+
check if the given typdef annotation is context
|
|
163
|
+
"""
|
|
164
|
+
return inspect.isclass(typedef) and issubclass(typedef, Context)
|
|
165
|
+
|
|
166
|
+
def is_extra(typedef: Type) -> bool:
|
|
167
|
+
"""
|
|
168
|
+
check if the given typedef annotation is extra
|
|
169
|
+
"""
|
|
170
|
+
return get_origin(typedef) is Annotated \
|
|
171
|
+
and any(isinstance(arg, Extra) for arg in get_args(typedef))
|
|
172
|
+
|
|
173
|
+
def into_command(callable: Callable, cls: Type[Command] = Command) -> Command:
|
|
174
|
+
"""
|
|
175
|
+
convert function into a command definition using function doc/signature
|
|
176
|
+
|
|
177
|
+
:param callable: function to convert to a command
|
|
178
|
+
:param cls: command class factory
|
|
179
|
+
:return: command generated from function
|
|
180
|
+
"""
|
|
181
|
+
doc = parse_doc(callable)
|
|
182
|
+
signature = get_signature(callable)
|
|
183
|
+
args = []
|
|
184
|
+
flags = []
|
|
185
|
+
extra: Set[str] = getattr(callable, EXTRA_ATTR, set())
|
|
186
|
+
args_default = getattr(callable, ARG_ATTR, {})
|
|
187
|
+
flags_default = getattr(callable, FLAG_ATTR, {})
|
|
188
|
+
for name in signature.args:
|
|
189
|
+
typedef = signature.typehints.get(name, str)
|
|
190
|
+
if name in extra or is_context(typedef) or is_extra(typedef):
|
|
191
|
+
continue
|
|
192
|
+
kwargs = args_default.get(name) or {}
|
|
193
|
+
about = kwargs.pop('about', None) or doc.params.get(name) or ''
|
|
194
|
+
args.append(Arg(
|
|
195
|
+
name=name,
|
|
196
|
+
about=about,
|
|
197
|
+
default=signature.defaults.get(name, None),
|
|
198
|
+
type=typedef,
|
|
199
|
+
**kwargs
|
|
200
|
+
))
|
|
201
|
+
|
|
202
|
+
for name in signature.kwargs:
|
|
203
|
+
typedef = signature.typehints.get(name, str)
|
|
204
|
+
if name in extra or is_context(typedef) or is_extra(typedef):
|
|
205
|
+
continue
|
|
206
|
+
kwargs = flags_default.get(name) or {}
|
|
207
|
+
about = kwargs.pop('about', None) or doc.params.get(name) or ''
|
|
208
|
+
required = kwargs.pop('required', None) or (name not in signature.defaults)
|
|
209
|
+
flags.append(Flag(
|
|
210
|
+
name=name,
|
|
211
|
+
about=about,
|
|
212
|
+
default=signature.defaults.get(name, None),
|
|
213
|
+
required=required,
|
|
214
|
+
type=typedef,
|
|
215
|
+
**kwargs
|
|
216
|
+
))
|
|
217
|
+
|
|
218
|
+
if signature.arg_splat is not None:
|
|
219
|
+
name = signature.arg_splat
|
|
220
|
+
typedef = signature.typehints.get(name, str)
|
|
221
|
+
kwargs = args_default.get(name) or {}
|
|
222
|
+
about = kwargs.pop('about', None) or doc.params.get(name) or ''
|
|
223
|
+
args.append(Arg(
|
|
224
|
+
name=name,
|
|
225
|
+
about=about,
|
|
226
|
+
default=signature.defaults.get(name, None),
|
|
227
|
+
type=typedef,
|
|
228
|
+
repeat=True,
|
|
229
|
+
**kwargs
|
|
230
|
+
))
|
|
231
|
+
return cls(
|
|
232
|
+
name=callable.__name__,
|
|
233
|
+
about=doc.about,
|
|
234
|
+
args=args,
|
|
235
|
+
flags=flags,
|
|
236
|
+
action=callable,
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
@functools.lru_cache()
|
|
240
|
+
def wrap_ctx(callable: Callable[..., R]) -> Callable[[Context], R]:
|
|
241
|
+
"""
|
|
242
|
+
wrap function in handler that unwraps the context into its relevant args
|
|
243
|
+
|
|
244
|
+
:param callable: original function to wrap
|
|
245
|
+
:return: wrapped function
|
|
246
|
+
"""
|
|
247
|
+
if hasattr(callable, WRAP_CTX_ATTR):
|
|
248
|
+
return callable
|
|
249
|
+
|
|
250
|
+
signature = get_signature(callable)
|
|
251
|
+
def inner(ctx: Context) -> R:
|
|
252
|
+
used = set()
|
|
253
|
+
args = []
|
|
254
|
+
for arg in signature.args:
|
|
255
|
+
cast = signature.typehints[arg]
|
|
256
|
+
value = ctx.get(arg, cast)
|
|
257
|
+
used.add(arg)
|
|
258
|
+
args.append(value)
|
|
259
|
+
kwargs = {}
|
|
260
|
+
for kwarg in signature.kwargs:
|
|
261
|
+
cast = signature.typehints[kwarg]
|
|
262
|
+
value = ctx.get(kwarg, cast)
|
|
263
|
+
used.add(kwarg)
|
|
264
|
+
kwargs[kwarg] = value
|
|
265
|
+
if signature.arg_splat is not None:
|
|
266
|
+
splat: Optional[List] = ctx.get(signature.arg_splat, default=None)
|
|
267
|
+
if splat is None:
|
|
268
|
+
splat = []
|
|
269
|
+
for key, value in ctx.args.items():
|
|
270
|
+
if key not in signature.args:
|
|
271
|
+
used.add(key)
|
|
272
|
+
splat.append(value)
|
|
273
|
+
else:
|
|
274
|
+
used.add(signature.arg_splat)
|
|
275
|
+
args.extend(splat)
|
|
276
|
+
if signature.kw_splat is not None:
|
|
277
|
+
for key, value in ctx.args.items():
|
|
278
|
+
if key not in used and key not in kwargs:
|
|
279
|
+
kwargs[key] = value
|
|
280
|
+
for key, value in ctx.flags.items():
|
|
281
|
+
if key not in used and key not in kwargs:
|
|
282
|
+
kwargs[key] = value
|
|
283
|
+
for key, value in ctx.extra.items():
|
|
284
|
+
if key not in used and key not in kwargs:
|
|
285
|
+
kwargs[key] = value
|
|
286
|
+
return callable(*args, **kwargs)
|
|
287
|
+
|
|
288
|
+
setattr(inner, WRAP_CTX_ATTR, True)
|
|
289
|
+
return inner
|
|
290
|
+
|
|
291
|
+
@functools.lru_cache()
|
|
292
|
+
def wrap_validator(callable: Callable[..., R]) -> Callable[[ParseCtx, Any], R]:
|
|
293
|
+
"""
|
|
294
|
+
wrap validator function that unwraps the `ParseCtx` into its relevant args
|
|
295
|
+
|
|
296
|
+
:param callable: original function to wrap
|
|
297
|
+
:return: wrapped function
|
|
298
|
+
"""
|
|
299
|
+
if hasattr(callable, WRAP_CTX_ATTR):
|
|
300
|
+
return callable
|
|
301
|
+
|
|
302
|
+
signature = get_signature(callable)
|
|
303
|
+
def inner(ctx: ParseCtx, validate: Any) -> R:
|
|
304
|
+
used = set()
|
|
305
|
+
args = []
|
|
306
|
+
kwargs = {}
|
|
307
|
+
try:
|
|
308
|
+
for arg in signature.args[:-1]:
|
|
309
|
+
cast = signature.typehints[arg]
|
|
310
|
+
value = ctx.get_extra(arg, cast)
|
|
311
|
+
used.add(arg)
|
|
312
|
+
args.append(value)
|
|
313
|
+
for kwarg in signature.kwargs:
|
|
314
|
+
cast = signature.typehints[kwarg]
|
|
315
|
+
value = ctx.get_extra(kwarg, cast)
|
|
316
|
+
used.add(kwarg)
|
|
317
|
+
kwargs[kwarg] = value
|
|
318
|
+
if signature.kw_splat is not None:
|
|
319
|
+
for key, value in ctx.extra.items():
|
|
320
|
+
if key not in used and key not in kwargs:
|
|
321
|
+
kwargs[key] = value
|
|
322
|
+
except KeyError as e:
|
|
323
|
+
name = callable.__name__
|
|
324
|
+
arg = e.args[0]
|
|
325
|
+
raise TypeError(f'{name} is missing argument {arg!r}') from None
|
|
326
|
+
args.append(validate)
|
|
327
|
+
return callable(*args, **kwargs)
|
|
328
|
+
|
|
329
|
+
setattr(inner, WRAP_CTX_ATTR, True)
|
|
330
|
+
return inner
|
|
331
|
+
|
|
332
|
+
@functools.lru_cache()
|
|
333
|
+
def wrap_suggestor(callable: Callable[..., R]) -> Callable[[dict, str], R]:
|
|
334
|
+
"""
|
|
335
|
+
wrap suggestor function that unwraps extra args into its relevant args
|
|
336
|
+
|
|
337
|
+
:param callable: original function to wrap
|
|
338
|
+
:return: wrapped function
|
|
339
|
+
"""
|
|
340
|
+
if hasattr(callable, WRAP_CTX_ATTR):
|
|
341
|
+
return callable
|
|
342
|
+
|
|
343
|
+
signature = get_signature(callable)
|
|
344
|
+
def inner(extra: dict, suggest: str) -> R:
|
|
345
|
+
used = set()
|
|
346
|
+
args = []
|
|
347
|
+
kwargs = {}
|
|
348
|
+
try:
|
|
349
|
+
for arg in signature.args[:-1]:
|
|
350
|
+
cast = signature.typehints[arg]
|
|
351
|
+
value = get_dict(extra, arg, cast)
|
|
352
|
+
used.add(arg)
|
|
353
|
+
args.append(value)
|
|
354
|
+
for kwarg in signature.kwargs:
|
|
355
|
+
cast = signature.typehints[kwarg]
|
|
356
|
+
value = get_dict(extra, kwarg, cast)
|
|
357
|
+
used.add(kwarg)
|
|
358
|
+
kwargs[kwarg] = value
|
|
359
|
+
if signature.kw_splat is not None:
|
|
360
|
+
for key, value in extra.items():
|
|
361
|
+
if key not in used and key not in kwargs:
|
|
362
|
+
kwargs[key] = value
|
|
363
|
+
except KeyError as e:
|
|
364
|
+
name = callable.__name__
|
|
365
|
+
arg = e.args[0]
|
|
366
|
+
raise TypeError(f'{name} is missing argument {arg!r}') from None
|
|
367
|
+
args.append(suggest)
|
|
368
|
+
return callable(*args, **kwargs)
|
|
369
|
+
|
|
370
|
+
setattr(inner, WRAP_CTX_ATTR, True)
|
|
371
|
+
return inner
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cli3
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Highly configurable, dynamic, fast and easy command-line builder.
|
|
5
|
+
Author-email: Andrew C Scott <imgurbot12@gmail.com>
|
|
6
|
+
Project-URL: Repository, https://github.com/imgurbot12/cli
|
|
7
|
+
Keywords: cli,command,commandline
|
|
8
|
+
Classifier: Typing :: Typed
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
|
+
Requires-Python: >=3.8
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: typing_extensions>=4.7.1
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
## cli
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/cli3/)
|
|
30
|
+
[](https://pypi.org/project/cli3/)
|
|
31
|
+
[](https://github.com/imgurbot12/cli/blob/master/LICENSE)
|
|
32
|
+
[](https://github.com/imgurbot12/cli3)
|
|
33
|
+
|
|
34
|
+
A highly configurable, dynamic, fast, and easy solution to managing
|
|
35
|
+
a command-line application. The goal is to make cli development as simple
|
|
36
|
+
and effective as possible for any configuration enabling the developer to
|
|
37
|
+
focus on function over form.
|
|
38
|
+
|
|
39
|
+
Supports Python >= 3.8 and can run asynchronously or synchronously.
|
|
40
|
+
|
|
41
|
+
This library took inspiration from [urfave/cli](https://github.com/urfave/cli)
|
|
42
|
+
and [pallets/click](https://github.com/pallets/click).
|
|
43
|
+
|
|
44
|
+
### Install
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
$ pip install cli3
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Examples
|
|
51
|
+
|
|
52
|
+
#### Derivative vs declarative.
|
|
53
|
+
|
|
54
|
+
Derivative implementation.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import cli
|
|
58
|
+
from typing import List, Optional
|
|
59
|
+
|
|
60
|
+
#NOTE: Arguments and options are separated by `*`
|
|
61
|
+
# Anything that is a positional parameter is treated as an argument.
|
|
62
|
+
# Anything that is keyword only is treated as an option.
|
|
63
|
+
|
|
64
|
+
@cli.command
|
|
65
|
+
def example(
|
|
66
|
+
echo: List[str], *,
|
|
67
|
+
test: Optional[int] = None,
|
|
68
|
+
bool: Optional[bool] = None,
|
|
69
|
+
):
|
|
70
|
+
"""
|
|
71
|
+
this is an example
|
|
72
|
+
|
|
73
|
+
:param echo: arguments to echo to output
|
|
74
|
+
"""
|
|
75
|
+
cli.echo('example!', echo)
|
|
76
|
+
|
|
77
|
+
example()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Declarative implementation.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import cli
|
|
84
|
+
from typing import List
|
|
85
|
+
|
|
86
|
+
#NOTE: Arguments do not need to be marked or divided by arg/kwarg here.
|
|
87
|
+
# They are already defined declaratively within the command definition.
|
|
88
|
+
# Everything is just passed according to name if present (except for cli.Context).
|
|
89
|
+
|
|
90
|
+
def action(echo: List[str]):
|
|
91
|
+
cli.echo('example!', echo)
|
|
92
|
+
|
|
93
|
+
command = cli.Command(
|
|
94
|
+
name='example',
|
|
95
|
+
about='this is an example',
|
|
96
|
+
version='0.0.1',
|
|
97
|
+
args=[cli.Arg('echo', about='arguments to echo to output', repeat=True)],
|
|
98
|
+
flags=[cli.Flag[int]('test'), cli.Flag[bool]('bool')],
|
|
99
|
+
action=action,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
command.run()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Command groups and extras.
|
|
106
|
+
|
|
107
|
+
Derivative implementation(s).
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import cli
|
|
111
|
+
from typing import Annotated, List
|
|
112
|
+
|
|
113
|
+
#NOTE: Command groups (`@cli.group`) dont define their own args/options.
|
|
114
|
+
# They are intended to just be semantic helpers for actual commands.
|
|
115
|
+
# If you want functional commands within commands just use `@cli.command`.
|
|
116
|
+
|
|
117
|
+
#NOTE: pass extra values to children commands!
|
|
118
|
+
# They can be retrieved later in a variety of ways
|
|
119
|
+
|
|
120
|
+
@cli.group
|
|
121
|
+
def group(ctx: cli.Context):
|
|
122
|
+
ctx.extra['extra_arg'] = 'value!'
|
|
123
|
+
|
|
124
|
+
#NOTE: command1/command2/command3 are identical in function.
|
|
125
|
+
# 1. command1 denotes an argument as extra via a decorator.
|
|
126
|
+
# 2. command2 uses annotated type defintions to denote an extra.
|
|
127
|
+
# 3. command3 retrieves the extra directly from the context object.
|
|
128
|
+
# (cli.Context will get passed if a param with the type-annotation is present)
|
|
129
|
+
|
|
130
|
+
@group.command
|
|
131
|
+
@cli.extra('extra_arg')
|
|
132
|
+
def command1(args: List[int], extra_arg: str):
|
|
133
|
+
cli.echo('command1', args, extra_arg)
|
|
134
|
+
|
|
135
|
+
@group.command
|
|
136
|
+
def command2(args: List[int], extra_arg: Annotated[str, cli.Extra()]):
|
|
137
|
+
cli.echo('command2', args, extra_arg)
|
|
138
|
+
|
|
139
|
+
@group.command
|
|
140
|
+
def command3(ctx: cli.Context, args: List[int]):
|
|
141
|
+
extra_arg = ctx.get_extra('extra_arg', str)
|
|
142
|
+
cli.echo('command3', args, extra_arg)
|
|
143
|
+
|
|
144
|
+
group()
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Declarative implementation.
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
import cli
|
|
151
|
+
from typing import List
|
|
152
|
+
|
|
153
|
+
def groupc(ctx: cli.Context):
|
|
154
|
+
ctx.extra['extra_arg'] = 'value!'
|
|
155
|
+
|
|
156
|
+
def command1(args: List[int], extra_arg: str):
|
|
157
|
+
cli.echo('command1', args, extra_arg)
|
|
158
|
+
|
|
159
|
+
def command2(ctx: cli.Context, args: List[int]):
|
|
160
|
+
extra_arg = ctx.get_extra('extra_arg', str)
|
|
161
|
+
cli.echo('command2', args, extra_arg)
|
|
162
|
+
|
|
163
|
+
def command3(ctx: cli.Context, extra_arg: str):
|
|
164
|
+
args = ctx.get_arg('args', List[int])
|
|
165
|
+
cli.echo('command3', args, extra_arg)
|
|
166
|
+
|
|
167
|
+
group = cli.Command(
|
|
168
|
+
name='group',
|
|
169
|
+
action=groupc,
|
|
170
|
+
commands=[
|
|
171
|
+
cli.Command(
|
|
172
|
+
name='command1',
|
|
173
|
+
args=[cli.Arg[int]('args', repeat=True)],
|
|
174
|
+
action=command1,
|
|
175
|
+
),
|
|
176
|
+
cli.Command(
|
|
177
|
+
name='command2',
|
|
178
|
+
args=[cli.Arg[List[int]]('args')],
|
|
179
|
+
action=command2,
|
|
180
|
+
),
|
|
181
|
+
cli.Command(
|
|
182
|
+
name='command3',
|
|
183
|
+
args=[cli.Arg[int]('args', repeat=True)],
|
|
184
|
+
action=command3,
|
|
185
|
+
),
|
|
186
|
+
]
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
group()
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
#### Async support.
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
import cli
|
|
196
|
+
import asyncio
|
|
197
|
+
|
|
198
|
+
#NOTE: commands support sync/async functions interchangably.
|
|
199
|
+
# You can call the cli asyncly with the `run_async()` function as well.
|
|
200
|
+
|
|
201
|
+
@cli.group
|
|
202
|
+
async def group():
|
|
203
|
+
cli.echo('group called!')
|
|
204
|
+
|
|
205
|
+
@group.command('foo-command')
|
|
206
|
+
async def foo():
|
|
207
|
+
cli.echo('foo called!')
|
|
208
|
+
|
|
209
|
+
@group.command('bar-command', about='this is a sync function')
|
|
210
|
+
def bar():
|
|
211
|
+
cli.echo('bar called!')
|
|
212
|
+
|
|
213
|
+
run_async = True
|
|
214
|
+
if run_async:
|
|
215
|
+
asyncio.run(group.run_async())
|
|
216
|
+
else:
|
|
217
|
+
group()
|
|
218
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
cli/__init__.py,sha256=Q-4kAja4_P6xOuDNpgszkUfOU1x5ltrJs2xm8jMeBiI,5228
|
|
2
|
+
cli/arg.py,sha256=ogDNNdLAJsJcER6hga9HlV8uCYIBMa2iX1I_FadfQ4U,2441
|
|
3
|
+
cli/cmd.py,sha256=L1pPhMD3lq6pxwRx5IZ3wSpRV1Wbe_4vCox3GPgKtUA,16161
|
|
4
|
+
cli/context.py,sha256=hGNgWEpzkT0w4YBAt_dM9W9cwkLd7bBpF0lCj60jg5E,11375
|
|
5
|
+
cli/errors.py,sha256=VOo9F-nWj8rRLjKNx_jh_0gZgtjIWL7d6eKy98Jd-KE,6347
|
|
6
|
+
cli/flag.py,sha256=txx3W4ZQea6qIbERUAFYfGL0ZbyIMZTh6hV6AgQiLzk,4436
|
|
7
|
+
cli/help.py,sha256=sogGNDllFacXlvnmeycwM-CZR6D4J0DwG0c1S0_VNXs,7678
|
|
8
|
+
cli/parser.py,sha256=A2qkMpRBkv_iatWrVkwdkzNUCGZA8AXzv2oXkCXTRB4,15004
|
|
9
|
+
cli/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
10
|
+
cli/style.py,sha256=tJ3PoCdAs2gCoj1AeQSGSj3QY-R3W9au-YsFtYV-iPk,4909
|
|
11
|
+
cli/suggest.py,sha256=0z_4aal4RvkmTjESSLo8a3_1hYpZCINn6wYMAgx808o,8439
|
|
12
|
+
cli/utils.py,sha256=_BX34ApMYc4VJqWZ_O8853ilsTdmPieDpb4xqM886DA,11323
|
|
13
|
+
cli/validate.py,sha256=W40mpmfrxaZc0GZLj8OBBF41zqYQHM8-rLujdDewTW0,4572
|
|
14
|
+
cli/wraps.py,sha256=N-JSvTTLjhnDxl_28wRh4x2oOSheF_sd_K7VU8BubsI,11819
|
|
15
|
+
cli/tests/__init__.py,sha256=exBFnP5lJgjWz8da3YvJ7jraHzzi_5iqZelVEcrPwFg,273
|
|
16
|
+
cli/tests/parser.py,sha256=31_DAHzO0nvP-MR73jc5lKlW-1YzK-XzV1DZKWc0h1Y,10341
|
|
17
|
+
cli/tests/suggest.py,sha256=p-1jaOO-x7BcLNXwcU3-thPqmXPWYheuV4ZNcSQNr0A,2607
|
|
18
|
+
cli/tests/_apps/__init__.py,sha256=IQokgX1qDkKHwBYYhz5B4TC9Bs3zs7LoWohBqzcsIe8,401
|
|
19
|
+
cli/tests/_apps/v1.py,sha256=9mkezjPai3Mg5FEMAtoPZN6EB6Fi5kRO6UfpPDv60ys,1874
|
|
20
|
+
cli/tests/_apps/v2.py,sha256=nER9pqGCYgGEvs7TZbIcofDLTg-zcXo4VNhsN8Pwf2k,1783
|
|
21
|
+
cli3-2.0.0.dist-info/licenses/LICENSE,sha256=GOaXO8j5Ipy-HYz1pIbFTQgGVRT2bOh4Yu8N-b56svQ,1081
|
|
22
|
+
cli3-2.0.0.dist-info/METADATA,sha256=5LvOYgGADMpbLRKPWfZM4UP1xfwKNZractwGArsEX_Q,6036
|
|
23
|
+
cli3-2.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
24
|
+
cli3-2.0.0.dist-info/top_level.txt,sha256=2ImG917oaVHlm0nP9oJE-Qrgs-fq_fGWgba2H1f8fpE,4
|
|
25
|
+
cli3-2.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrew C Scott
|
|
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
|
+
cli
|