cli3 1.1.3__tar.gz → 2.0.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.
- {cli3-1.1.3 → cli3-2.0.0}/LICENSE +2 -2
- cli3-2.0.0/PKG-INFO +218 -0
- cli3-2.0.0/README.md +192 -0
- cli3-2.0.0/cli/__init__.py +168 -0
- cli3-2.0.0/cli/arg.py +69 -0
- cli3-2.0.0/cli/cmd.py +473 -0
- cli3-2.0.0/cli/context.py +362 -0
- cli3-2.0.0/cli/errors.py +199 -0
- cli3-2.0.0/cli/flag.py +129 -0
- cli3-2.0.0/cli/help.py +228 -0
- cli3-2.0.0/cli/parser.py +440 -0
- cli3-2.0.0/cli/py.typed +0 -0
- cli3-2.0.0/cli/style.py +180 -0
- cli3-2.0.0/cli/suggest.py +256 -0
- cli3-2.0.0/cli/tests/__init__.py +15 -0
- cli3-2.0.0/cli/tests/_apps/__init__.py +19 -0
- cli3-2.0.0/cli/tests/_apps/v1.py +61 -0
- cli3-2.0.0/cli/tests/_apps/v2.py +76 -0
- cli3-2.0.0/cli/tests/parser.py +294 -0
- cli3-2.0.0/cli/tests/suggest.py +77 -0
- cli3-2.0.0/cli/utils.py +374 -0
- cli3-2.0.0/cli/validate.py +171 -0
- cli3-2.0.0/cli/wraps.py +371 -0
- cli3-2.0.0/cli3.egg-info/PKG-INFO +218 -0
- cli3-2.0.0/cli3.egg-info/SOURCES.txt +28 -0
- cli3-2.0.0/cli3.egg-info/requires.txt +1 -0
- cli3-2.0.0/pyproject.toml +39 -0
- cli3-1.1.3/PKG-INFO +0 -46
- cli3-1.1.3/README.md +0 -31
- cli3-1.1.3/cli/__init__.py +0 -84
- cli3-1.1.3/cli/abc.py +0 -460
- cli3-1.1.3/cli/app.py +0 -179
- cli3-1.1.3/cli/argument.py +0 -161
- cli3-1.1.3/cli/command.py +0 -198
- cli3-1.1.3/cli/flag.py +0 -159
- cli3-1.1.3/cli/help.py +0 -190
- cli3-1.1.3/cli/parser.py +0 -203
- cli3-1.1.3/cli/tests/__init__.py +0 -6
- cli3-1.1.3/cli/tests/app.py +0 -201
- cli3-1.1.3/cli/tests/content/__init__.py +0 -10
- cli3-1.1.3/cli/tests/content/v1.py +0 -91
- cli3-1.1.3/cli/tests/content/v2.py +0 -75
- cli3-1.1.3/cli/wraps.py +0 -381
- cli3-1.1.3/cli3.egg-info/PKG-INFO +0 -46
- cli3-1.1.3/cli3.egg-info/SOURCES.txt +0 -22
- cli3-1.1.3/cli3.egg-info/requires.txt +0 -3
- cli3-1.1.3/setup.py +0 -31
- {cli3-1.1.3 → cli3-2.0.0}/cli3.egg-info/dependency_links.txt +0 -0
- {cli3-1.1.3 → cli3-2.0.0}/cli3.egg-info/top_level.txt +0 -0
- {cli3-1.1.3 → cli3-2.0.0}/setup.cfg +0 -0
cli3-2.0.0/PKG-INFO
ADDED
|
@@ -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
|
+
```
|
cli3-2.0.0/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
## cli
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/cli3/)
|
|
4
|
+
[](https://pypi.org/project/cli3/)
|
|
5
|
+
[](https://github.com/imgurbot12/cli/blob/master/LICENSE)
|
|
6
|
+
[](https://github.com/imgurbot12/cli3)
|
|
7
|
+
|
|
8
|
+
A highly configurable, dynamic, fast, and easy solution to managing
|
|
9
|
+
a command-line application. The goal is to make cli development as simple
|
|
10
|
+
and effective as possible for any configuration enabling the developer to
|
|
11
|
+
focus on function over form.
|
|
12
|
+
|
|
13
|
+
Supports Python >= 3.8 and can run asynchronously or synchronously.
|
|
14
|
+
|
|
15
|
+
This library took inspiration from [urfave/cli](https://github.com/urfave/cli)
|
|
16
|
+
and [pallets/click](https://github.com/pallets/click).
|
|
17
|
+
|
|
18
|
+
### Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
$ pip install cli3
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Examples
|
|
25
|
+
|
|
26
|
+
#### Derivative vs declarative.
|
|
27
|
+
|
|
28
|
+
Derivative implementation.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import cli
|
|
32
|
+
from typing import List, Optional
|
|
33
|
+
|
|
34
|
+
#NOTE: Arguments and options are separated by `*`
|
|
35
|
+
# Anything that is a positional parameter is treated as an argument.
|
|
36
|
+
# Anything that is keyword only is treated as an option.
|
|
37
|
+
|
|
38
|
+
@cli.command
|
|
39
|
+
def example(
|
|
40
|
+
echo: List[str], *,
|
|
41
|
+
test: Optional[int] = None,
|
|
42
|
+
bool: Optional[bool] = None,
|
|
43
|
+
):
|
|
44
|
+
"""
|
|
45
|
+
this is an example
|
|
46
|
+
|
|
47
|
+
:param echo: arguments to echo to output
|
|
48
|
+
"""
|
|
49
|
+
cli.echo('example!', echo)
|
|
50
|
+
|
|
51
|
+
example()
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Declarative implementation.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import cli
|
|
58
|
+
from typing import List
|
|
59
|
+
|
|
60
|
+
#NOTE: Arguments do not need to be marked or divided by arg/kwarg here.
|
|
61
|
+
# They are already defined declaratively within the command definition.
|
|
62
|
+
# Everything is just passed according to name if present (except for cli.Context).
|
|
63
|
+
|
|
64
|
+
def action(echo: List[str]):
|
|
65
|
+
cli.echo('example!', echo)
|
|
66
|
+
|
|
67
|
+
command = cli.Command(
|
|
68
|
+
name='example',
|
|
69
|
+
about='this is an example',
|
|
70
|
+
version='0.0.1',
|
|
71
|
+
args=[cli.Arg('echo', about='arguments to echo to output', repeat=True)],
|
|
72
|
+
flags=[cli.Flag[int]('test'), cli.Flag[bool]('bool')],
|
|
73
|
+
action=action,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
command.run()
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### Command groups and extras.
|
|
80
|
+
|
|
81
|
+
Derivative implementation(s).
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
import cli
|
|
85
|
+
from typing import Annotated, List
|
|
86
|
+
|
|
87
|
+
#NOTE: Command groups (`@cli.group`) dont define their own args/options.
|
|
88
|
+
# They are intended to just be semantic helpers for actual commands.
|
|
89
|
+
# If you want functional commands within commands just use `@cli.command`.
|
|
90
|
+
|
|
91
|
+
#NOTE: pass extra values to children commands!
|
|
92
|
+
# They can be retrieved later in a variety of ways
|
|
93
|
+
|
|
94
|
+
@cli.group
|
|
95
|
+
def group(ctx: cli.Context):
|
|
96
|
+
ctx.extra['extra_arg'] = 'value!'
|
|
97
|
+
|
|
98
|
+
#NOTE: command1/command2/command3 are identical in function.
|
|
99
|
+
# 1. command1 denotes an argument as extra via a decorator.
|
|
100
|
+
# 2. command2 uses annotated type defintions to denote an extra.
|
|
101
|
+
# 3. command3 retrieves the extra directly from the context object.
|
|
102
|
+
# (cli.Context will get passed if a param with the type-annotation is present)
|
|
103
|
+
|
|
104
|
+
@group.command
|
|
105
|
+
@cli.extra('extra_arg')
|
|
106
|
+
def command1(args: List[int], extra_arg: str):
|
|
107
|
+
cli.echo('command1', args, extra_arg)
|
|
108
|
+
|
|
109
|
+
@group.command
|
|
110
|
+
def command2(args: List[int], extra_arg: Annotated[str, cli.Extra()]):
|
|
111
|
+
cli.echo('command2', args, extra_arg)
|
|
112
|
+
|
|
113
|
+
@group.command
|
|
114
|
+
def command3(ctx: cli.Context, args: List[int]):
|
|
115
|
+
extra_arg = ctx.get_extra('extra_arg', str)
|
|
116
|
+
cli.echo('command3', args, extra_arg)
|
|
117
|
+
|
|
118
|
+
group()
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Declarative implementation.
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import cli
|
|
125
|
+
from typing import List
|
|
126
|
+
|
|
127
|
+
def groupc(ctx: cli.Context):
|
|
128
|
+
ctx.extra['extra_arg'] = 'value!'
|
|
129
|
+
|
|
130
|
+
def command1(args: List[int], extra_arg: str):
|
|
131
|
+
cli.echo('command1', args, extra_arg)
|
|
132
|
+
|
|
133
|
+
def command2(ctx: cli.Context, args: List[int]):
|
|
134
|
+
extra_arg = ctx.get_extra('extra_arg', str)
|
|
135
|
+
cli.echo('command2', args, extra_arg)
|
|
136
|
+
|
|
137
|
+
def command3(ctx: cli.Context, extra_arg: str):
|
|
138
|
+
args = ctx.get_arg('args', List[int])
|
|
139
|
+
cli.echo('command3', args, extra_arg)
|
|
140
|
+
|
|
141
|
+
group = cli.Command(
|
|
142
|
+
name='group',
|
|
143
|
+
action=groupc,
|
|
144
|
+
commands=[
|
|
145
|
+
cli.Command(
|
|
146
|
+
name='command1',
|
|
147
|
+
args=[cli.Arg[int]('args', repeat=True)],
|
|
148
|
+
action=command1,
|
|
149
|
+
),
|
|
150
|
+
cli.Command(
|
|
151
|
+
name='command2',
|
|
152
|
+
args=[cli.Arg[List[int]]('args')],
|
|
153
|
+
action=command2,
|
|
154
|
+
),
|
|
155
|
+
cli.Command(
|
|
156
|
+
name='command3',
|
|
157
|
+
args=[cli.Arg[int]('args', repeat=True)],
|
|
158
|
+
action=command3,
|
|
159
|
+
),
|
|
160
|
+
]
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
group()
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### Async support.
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
import cli
|
|
170
|
+
import asyncio
|
|
171
|
+
|
|
172
|
+
#NOTE: commands support sync/async functions interchangably.
|
|
173
|
+
# You can call the cli asyncly with the `run_async()` function as well.
|
|
174
|
+
|
|
175
|
+
@cli.group
|
|
176
|
+
async def group():
|
|
177
|
+
cli.echo('group called!')
|
|
178
|
+
|
|
179
|
+
@group.command('foo-command')
|
|
180
|
+
async def foo():
|
|
181
|
+
cli.echo('foo called!')
|
|
182
|
+
|
|
183
|
+
@group.command('bar-command', about='this is a sync function')
|
|
184
|
+
def bar():
|
|
185
|
+
cli.echo('bar called!')
|
|
186
|
+
|
|
187
|
+
run_async = True
|
|
188
|
+
if run_async:
|
|
189
|
+
asyncio.run(group.run_async())
|
|
190
|
+
else:
|
|
191
|
+
group()
|
|
192
|
+
```
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Command-Line-Interface Parsing Library
|
|
3
|
+
"""
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from typing import (
|
|
6
|
+
Callable, Iterable, List, Literal, NamedTuple, Optional,
|
|
7
|
+
Tuple, Type, TypeVar, Union, cast)
|
|
8
|
+
from typing_extensions import Annotated, get_args, get_origin, get_type_hints
|
|
9
|
+
|
|
10
|
+
#DONE: handle ValueError exceptions on data-type issues
|
|
11
|
+
#DONE: functions: cli.group/cli.context/cli.echo
|
|
12
|
+
#DONE: decorators: cli.argument/cli.flag/cli.pass_context to override/enhance parsed details
|
|
13
|
+
|
|
14
|
+
#DONE: pass `Context` with annotation present or @cli.pass_context wrapper
|
|
15
|
+
#DONE: moar unit-tests for parser
|
|
16
|
+
# - extra arg / missing arg / invalid arg / arg repeat
|
|
17
|
+
# - extra flag / missing flag / invalid flag / missing flag value / invalid flag value / flag repeat
|
|
18
|
+
# - repeated command
|
|
19
|
+
# - group / command tree (subcmd-required difference)
|
|
20
|
+
|
|
21
|
+
#DONE: docs
|
|
22
|
+
# - simple hello world
|
|
23
|
+
# - `Context.extra` and pass via function args
|
|
24
|
+
|
|
25
|
+
#DONE: suggestor unit-tests
|
|
26
|
+
#DONE: controls on stripping/ignoring styling when writing to file instead of tty
|
|
27
|
+
#DONE: help-page implementation (with colors)
|
|
28
|
+
|
|
29
|
+
#DONE: docstrings for all functions
|
|
30
|
+
|
|
31
|
+
#DONE: command chaining rules
|
|
32
|
+
# enable/disable chaining with `chain: bool = False`
|
|
33
|
+
# command also has `repeat: bool = False`
|
|
34
|
+
# - repeat configures whether command can be chained on itself
|
|
35
|
+
# - chain configures if subcommands are allowed in combination.
|
|
36
|
+
|
|
37
|
+
#DONE: allow returning values from actions (when standalone is disabled)
|
|
38
|
+
|
|
39
|
+
#TODO: give indexing another shot (but count down rather than up)
|
|
40
|
+
#TODO: errors should be more precisce -> double flag, extra arg, etc...
|
|
41
|
+
|
|
42
|
+
#TODO: command categories does nothing
|
|
43
|
+
#TODO: authors will never be displayed ever in help
|
|
44
|
+
#TODO: version will never be displayed ever (maybe add version flag with support for more info?)
|
|
45
|
+
|
|
46
|
+
#** Variables **#
|
|
47
|
+
__all__ = [
|
|
48
|
+
'echo',
|
|
49
|
+
'style',
|
|
50
|
+
'secho',
|
|
51
|
+
'argument',
|
|
52
|
+
'extra',
|
|
53
|
+
'option',
|
|
54
|
+
'command',
|
|
55
|
+
'group',
|
|
56
|
+
'get_current_context',
|
|
57
|
+
|
|
58
|
+
'Arg',
|
|
59
|
+
'Command',
|
|
60
|
+
'Context',
|
|
61
|
+
'Flag',
|
|
62
|
+
|
|
63
|
+
'CliError',
|
|
64
|
+
'Extra',
|
|
65
|
+
'Validate',
|
|
66
|
+
'Suggest',
|
|
67
|
+
|
|
68
|
+
'Help',
|
|
69
|
+
'Parser',
|
|
70
|
+
'ParseCtx',
|
|
71
|
+
'ParsedCmd',
|
|
72
|
+
'Suggestor',
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
E = TypeVar('E', bound=Enum)
|
|
76
|
+
T = TypeVar('T')
|
|
77
|
+
|
|
78
|
+
SuggestFunc = Callable[[str], Iterable[str]]
|
|
79
|
+
OptSuggest = Union['SuggestFunc', Literal[False], None]
|
|
80
|
+
|
|
81
|
+
#** Classes **#
|
|
82
|
+
|
|
83
|
+
class _Meta(NamedTuple):
|
|
84
|
+
repeat: bool = False
|
|
85
|
+
|
|
86
|
+
#** Functions **#
|
|
87
|
+
|
|
88
|
+
def get_type(self, ftype: Optional[Type[T]]) -> Tuple[Type[T], _Meta]:
|
|
89
|
+
"""
|
|
90
|
+
retrieve real type associated with arg/flag and update the relevant attrs
|
|
91
|
+
|
|
92
|
+
:param ftype: type annotation assignment
|
|
93
|
+
:return: (true type, option metadata)
|
|
94
|
+
"""
|
|
95
|
+
ftype = ftype or getattr(self, 'type', None)
|
|
96
|
+
if ftype is not None:
|
|
97
|
+
origin = get_origin(ftype)
|
|
98
|
+
args = get_args(ftype)
|
|
99
|
+
if origin is None or origin is Annotated:
|
|
100
|
+
return ftype, _Meta()
|
|
101
|
+
if origin in (list, set):
|
|
102
|
+
return args[0], _Meta(repeat=True)
|
|
103
|
+
if origin is Union and len(args) == 2 and args[1] is type(None):
|
|
104
|
+
return get_type(self, args[0])
|
|
105
|
+
raise TypeError(ftype)
|
|
106
|
+
default = getattr(self, 'default', None)
|
|
107
|
+
if default is not None:
|
|
108
|
+
return type(self.default), _Meta()
|
|
109
|
+
validators = getattr(self, 'validators', None)
|
|
110
|
+
if validators:
|
|
111
|
+
for validator in validators[::-1]:
|
|
112
|
+
hints = get_type_hints(validator)
|
|
113
|
+
if 'return' in hints:
|
|
114
|
+
return hints['return']
|
|
115
|
+
return cast(Type, str), _Meta()
|
|
116
|
+
|
|
117
|
+
def get_suggestor(typedef: Type, suggest: OptSuggest) -> OptSuggest:
|
|
118
|
+
"""
|
|
119
|
+
get suggestor function or configuration based on type
|
|
120
|
+
|
|
121
|
+
:param type: arg/flag type
|
|
122
|
+
:param suggest: suggestion setting
|
|
123
|
+
:return: suggestion
|
|
124
|
+
"""
|
|
125
|
+
if suggest is not None:
|
|
126
|
+
return suggest
|
|
127
|
+
origin = get_origin(typedef)
|
|
128
|
+
if origin is Annotated:
|
|
129
|
+
for arg in get_args(typedef):
|
|
130
|
+
if isinstance(arg, Suggest):
|
|
131
|
+
return arg.suggestor
|
|
132
|
+
if origin is None and issubclass(typedef, Enum):
|
|
133
|
+
return suggest_static([opt.value for opt in typedef])
|
|
134
|
+
return None
|
|
135
|
+
|
|
136
|
+
def get_validator(type: Type,
|
|
137
|
+
validators: List['ValidatorFunc']) -> List['ValidatorFunc']:
|
|
138
|
+
"""
|
|
139
|
+
retrieve validators based on arg/flag configuration
|
|
140
|
+
|
|
141
|
+
:param type: type assignment for arg/flag
|
|
142
|
+
:param validators: list of established validators
|
|
143
|
+
:return: complete list of validators
|
|
144
|
+
"""
|
|
145
|
+
validator = DEFAULT_VALIDATORS.get(type)
|
|
146
|
+
if validator is not None:
|
|
147
|
+
validators.append(validator)
|
|
148
|
+
return validators
|
|
149
|
+
if get_origin(type) is Annotated:
|
|
150
|
+
for arg in get_args(type):
|
|
151
|
+
if isinstance(arg, Validate):
|
|
152
|
+
validators.append(arg.validator)
|
|
153
|
+
if not validators and type is not str:
|
|
154
|
+
return [type]
|
|
155
|
+
return validators
|
|
156
|
+
|
|
157
|
+
#** Imports **#
|
|
158
|
+
from .arg import Arg
|
|
159
|
+
from .cmd import Command
|
|
160
|
+
from .context import Context, get_current_context
|
|
161
|
+
from .errors import CliError
|
|
162
|
+
from .flag import Flag
|
|
163
|
+
from .help import Help
|
|
164
|
+
from .parser import Parser, ParsedCmd, ParseCtx
|
|
165
|
+
from .suggest import Suggest, Suggestor, suggest_static
|
|
166
|
+
from .utils import echo, style, secho, argument, extra, option, command, group
|
|
167
|
+
from .validate import DEFAULT_VALIDATORS, Validate, ValidatorFunc
|
|
168
|
+
from .wraps import Extra
|
cli3-2.0.0/cli/arg.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI Argument Implementation and DataType Parsers
|
|
3
|
+
"""
|
|
4
|
+
from typing import Any, Callable, Generic, List, Optional, Type
|
|
5
|
+
|
|
6
|
+
from . import T, OptSuggest, get_type, get_suggestor, get_validator
|
|
7
|
+
|
|
8
|
+
#** Variables **#
|
|
9
|
+
__all__ = ['Arg', 'Args']
|
|
10
|
+
|
|
11
|
+
Args = List['Arg']
|
|
12
|
+
|
|
13
|
+
#** Classes **#
|
|
14
|
+
|
|
15
|
+
class Arg(Generic[T]):
|
|
16
|
+
"""
|
|
17
|
+
Command Argument Configuration Setting
|
|
18
|
+
"""
|
|
19
|
+
type: Type[T]
|
|
20
|
+
suggestor: OptSuggest
|
|
21
|
+
|
|
22
|
+
def __init__(self,
|
|
23
|
+
name: str,
|
|
24
|
+
about: Optional[str] = None,
|
|
25
|
+
default: Optional[T] = None,
|
|
26
|
+
required: Optional[bool] = None,
|
|
27
|
+
repeat: bool = False,
|
|
28
|
+
validators: Optional[List[Callable[[Any], T]]] = None,
|
|
29
|
+
suggestor: OptSuggest = None,
|
|
30
|
+
type: Optional[Type[T]] = None,
|
|
31
|
+
):
|
|
32
|
+
"""
|
|
33
|
+
:param name: name of argument
|
|
34
|
+
:param about: description of argument
|
|
35
|
+
:param default: default value assigned to argument
|
|
36
|
+
:param required: label if argument is required during parsing
|
|
37
|
+
:param repeat: allow argument to be repeated
|
|
38
|
+
:param validators: validators used to process argument value
|
|
39
|
+
:param suggestor: argument auto-complete suggestion function
|
|
40
|
+
:param type: argument type assignment
|
|
41
|
+
"""
|
|
42
|
+
self.name = name
|
|
43
|
+
self.about = about
|
|
44
|
+
self.default = default
|
|
45
|
+
self.required = self.default is None if required is None else required
|
|
46
|
+
self.repeat = repeat
|
|
47
|
+
self.validators = validators or []
|
|
48
|
+
self.suggestor = suggestor
|
|
49
|
+
self._set_type(type)
|
|
50
|
+
|
|
51
|
+
def _set_type(self, typedef: Optional[Type]):
|
|
52
|
+
"""
|
|
53
|
+
type assignment and all related attributes that pull from type
|
|
54
|
+
"""
|
|
55
|
+
newtype, meta = get_type(self, typedef)
|
|
56
|
+
self.type = newtype
|
|
57
|
+
self.repeat = self.repeat or meta.repeat
|
|
58
|
+
self.suggestor = get_suggestor(self.type, self.suggestor)
|
|
59
|
+
self.validators = get_validator(self.type, self.validators)
|
|
60
|
+
|
|
61
|
+
def __repr__(self) -> str:
|
|
62
|
+
return f'Arg(name={self.name}, default={self.default})'
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def __class_getitem__(cls, value: Type):
|
|
66
|
+
func = getattr(super(), '__class_getitem__')
|
|
67
|
+
instance = func(value)
|
|
68
|
+
instance.type = value
|
|
69
|
+
return instance
|