cli-def 0.1.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_def/__init__.py +16 -0
- cli_def/backend/__init__.py +2 -0
- cli_def/backend/argparse/__init__.py +10 -0
- cli_def/backend/argparse/argparse_builder.py +238 -0
- cli_def/backend/click/__init__.py +13 -0
- cli_def/backend/click/click_binder.py +46 -0
- cli_def/backend/click/click_builder.py +182 -0
- cli_def/backend/protocols.py +22 -0
- cli_def/demo/__init__.py +2 -0
- cli_def/demo/handlers.py +125 -0
- cli_def/demo/profiles/advanced.toml +39 -0
- cli_def/demo/profiles/beginner.toml +21 -0
- cli_def/models/__init__.py +17 -0
- cli_def/models/abstract_node.py +87 -0
- cli_def/models/argument_def.py +84 -0
- cli_def/models/cli_def.py +28 -0
- cli_def/models/command_def.py +49 -0
- cli_def/models/mult_def.py +81 -0
- cli_def/models/schema/cli_schema.py +24 -0
- cli_def/ops/__init__.py +16 -0
- cli_def/ops/dumper.py +216 -0
- cli_def/ops/loader.py +56 -0
- cli_def/ops/utils/__init__.py +3 -0
- cli_def/ops/utils/pretty_renderer.py +543 -0
- cli_def/ops/utils/renderer.py +199 -0
- cli_def/ops/utils/renderer_ops.py +169 -0
- cli_def/parsers/__init__.py +10 -0
- cli_def/parsers/parser.py +101 -0
- cli_def/resources/cli_def.toml +126 -0
- cli_def/runtime/__init__.py +22 -0
- cli_def/runtime/_internal/__init__.py +2 -0
- cli_def/runtime/_internal/scan_runner.py +28 -0
- cli_def/runtime/context.py +12 -0
- cli_def/runtime/dispatcher.py +197 -0
- cli_def/runtime/event.py +35 -0
- cli_def/runtime/handler_support.py +75 -0
- cli_def/runtime/result.py +117 -0
- cli_def/runtime/runner.py +178 -0
- cli_def/runtime/scanner.py +143 -0
- cli_def/runtime/session.py +286 -0
- cli_def/runtime/utils.py +54 -0
- cli_def/script/__init__.py +2 -0
- cli_def/script/common.py +29 -0
- cli_def/script/handlers/__init__.py +14 -0
- cli_def/script/handlers/dummy.py +43 -0
- cli_def/script/handlers/dummy2.py +22 -0
- cli_def/script/handlers/dump.py +50 -0
- cli_def/script/handlers/repl.py +70 -0
- cli_def/script/handlers/run.py +70 -0
- cli_def/script/handlers/scan.py +179 -0
- cli_def/script/handlers/search.py +116 -0
- cli_def/script/main.py +32 -0
- cli_def-0.1.0.dist-info/METADATA +400 -0
- cli_def-0.1.0.dist-info/RECORD +58 -0
- cli_def-0.1.0.dist-info/WHEEL +5 -0
- cli_def-0.1.0.dist-info/entry_points.txt +2 -0
- cli_def-0.1.0.dist-info/licenses/LICENSE +21 -0
- cli_def-0.1.0.dist-info/top_level.txt +1 -0
cli_def/__init__.py
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
# cli_def/backend/argparse/argparse_builder.py
|
|
2
|
+
from typing import Iterable, Sequence, Any, Mapping
|
|
3
|
+
import argparse
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
from ...models import (
|
|
7
|
+
CliDefNode,
|
|
8
|
+
CliDef,
|
|
9
|
+
CommandDef,
|
|
10
|
+
ExecutableNode,
|
|
11
|
+
ArgumentDef,
|
|
12
|
+
MultDef,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from typing import Union
|
|
16
|
+
|
|
17
|
+
ArgparseNode = Union[
|
|
18
|
+
argparse.ArgumentParser,
|
|
19
|
+
argparse._SubParsersAction,
|
|
20
|
+
argparse.Action,
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
from ..protocols import BuilderProtocol
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ArgparseBuilder(BuilderProtocol):
|
|
27
|
+
|
|
28
|
+
def __init__(self):
|
|
29
|
+
self._defpath_mapping: dict[str, ArgparseNode] = {}
|
|
30
|
+
self._early_parser = None
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def defpath_mapping(self) -> Mapping[str, ArgparseNode]:
|
|
34
|
+
return self._defpath_mapping
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def build(self, cliDef: CliDef) -> argparse.ArgumentParser:
|
|
38
|
+
return self.build_argparse(cliDef)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _register(self, node: CliDefNode, obj: Any):
|
|
42
|
+
self._defpath_mapping[node.defpath] = obj
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _register_subparsers(self, node: CliDefNode, obj: Any):
|
|
46
|
+
special_defpath = "/".join([node.defpath, "__subparsers__"])
|
|
47
|
+
self._defpath_mapping[special_defpath] = obj
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def build_early_argparse(self, cliDef: CliDef, prog: str|None = None) -> argparse.ArgumentParser|None:
|
|
51
|
+
early_node = cliDef.select_first(lambda n: n.key == CommandDef.EARLY)
|
|
52
|
+
if early_node is None:
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
parser = argparse.ArgumentParser(
|
|
56
|
+
prog=prog or cliDef.key,
|
|
57
|
+
add_help=False,
|
|
58
|
+
)
|
|
59
|
+
assert isinstance(early_node, CommandDef)
|
|
60
|
+
self.build_arguments(early_node.arguments, parser)
|
|
61
|
+
return parser
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def build_argparse(self, cliDef: CliDef, prog: str|None = None) -> argparse.ArgumentParser:
|
|
65
|
+
parser = argparse.ArgumentParser(
|
|
66
|
+
prog=prog or cliDef.key
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
self._register(cliDef, parser)
|
|
70
|
+
|
|
71
|
+
self.build_arguments(cliDef.arguments, parser)
|
|
72
|
+
if cliDef.commands:
|
|
73
|
+
self.build_commands(cliDef.commands, parser)
|
|
74
|
+
|
|
75
|
+
self._attach_metadata(parser, cliDef, cliDef.get_command_sequence())
|
|
76
|
+
|
|
77
|
+
return parser
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def build_arguments(
|
|
81
|
+
self,
|
|
82
|
+
argumentDefs: Iterable[ArgumentDef],
|
|
83
|
+
parser: argparse.ArgumentParser,
|
|
84
|
+
) -> Iterable[argparse.Action] | None:
|
|
85
|
+
if argumentDefs is None:
|
|
86
|
+
return None
|
|
87
|
+
actions = []
|
|
88
|
+
for argDef in argumentDefs:
|
|
89
|
+
logging.debug(f"{argDef.defpath} mult={argDef.mult} nargs={self.to_nargs(argDef.mult)}")
|
|
90
|
+
if argDef.is_positional:
|
|
91
|
+
# positional paremter
|
|
92
|
+
action = parser.add_argument(
|
|
93
|
+
argDef.key,
|
|
94
|
+
choices=argDef.choices,
|
|
95
|
+
default=argDef.default,
|
|
96
|
+
nargs=self.to_nargs(argDef.mult),
|
|
97
|
+
help=argDef.help,
|
|
98
|
+
)
|
|
99
|
+
elif argDef.is_flag:
|
|
100
|
+
name_or_flags = []
|
|
101
|
+
if argDef.aliases:
|
|
102
|
+
name_or_flags.extend(argDef.aliases)
|
|
103
|
+
name_or_flags.append(argDef.option or argDef.key)
|
|
104
|
+
action = parser.add_argument(
|
|
105
|
+
*name_or_flags,
|
|
106
|
+
dest=argDef.get_dest(),
|
|
107
|
+
help=argDef.help,
|
|
108
|
+
action=argDef.get_action() or "",
|
|
109
|
+
)
|
|
110
|
+
else: # option parameter
|
|
111
|
+
name_or_flags = []
|
|
112
|
+
if argDef.aliases:
|
|
113
|
+
name_or_flags.extend(argDef.aliases)
|
|
114
|
+
name_or_flags.append(argDef.option)
|
|
115
|
+
action = parser.add_argument(
|
|
116
|
+
*name_or_flags,
|
|
117
|
+
dest=argDef.get_dest(),
|
|
118
|
+
choices=argDef.choices,
|
|
119
|
+
default=argDef.default,
|
|
120
|
+
nargs=self.to_nargs(argDef.mult),
|
|
121
|
+
help=argDef.help,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
actions.append(action)
|
|
125
|
+
self._register(argDef, action)
|
|
126
|
+
|
|
127
|
+
return actions
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def build_commands(
|
|
131
|
+
self,
|
|
132
|
+
commandDefs: Sequence[CommandDef],
|
|
133
|
+
parser: argparse.ArgumentParser,
|
|
134
|
+
) -> Iterable[argparse.ArgumentParser] | None:
|
|
135
|
+
if commandDefs is None or len(commandDefs) == 0:
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
# build parent parsers from defs of templates
|
|
139
|
+
parent_parsers_map = {}
|
|
140
|
+
for cmdDef in commandDefs:
|
|
141
|
+
if not cmdDef.is_template or cmdDef.key == CommandDef.EARLY:
|
|
142
|
+
continue
|
|
143
|
+
cmd_templ_parser = self.build_single_command(cmdDef)
|
|
144
|
+
parent_parsers_map[cmdDef.key] = cmd_templ_parser
|
|
145
|
+
if cmdDef.key == CommandDef.EARLY:
|
|
146
|
+
#print(f"@@@ early_parser found :{cmdDef.key}")
|
|
147
|
+
self._early_parser = cmd_templ_parser
|
|
148
|
+
|
|
149
|
+
firstCmdDef: CommandDef = commandDefs[0]
|
|
150
|
+
assert isinstance(firstCmdDef.parent, ExecutableNode)
|
|
151
|
+
group = (firstCmdDef.parent.group or
|
|
152
|
+
("sub" * firstCmdDef.parent.deflevel) + "command"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
subparsers = parser.add_subparsers(dest=group)
|
|
156
|
+
self._register_subparsers(firstCmdDef.parent, subparsers)
|
|
157
|
+
cmd_parsers = []
|
|
158
|
+
for cmdDef in commandDefs:
|
|
159
|
+
if cmdDef.is_template or cmdDef.key == CommandDef.EARLY:
|
|
160
|
+
continue
|
|
161
|
+
# select parent_parsers
|
|
162
|
+
parent_parsers = []
|
|
163
|
+
for tmpl in cmdDef.get_templates():
|
|
164
|
+
if tmpl.key in parent_parsers_map:
|
|
165
|
+
#print(f"@@@ parent_parser :{tmpl.key}")
|
|
166
|
+
parent_parsers.append(parent_parsers_map[tmpl.key])
|
|
167
|
+
# # TODO consider forcibly add _early to parents
|
|
168
|
+
# #print(f"@@@ self_early_parser = {"None" if self._early_parser is None else "Not None"}")
|
|
169
|
+
# if self._early_parser and self._early_parser not in parent_parsers:
|
|
170
|
+
# #print(f"@@@ add _early parser to {cmdDef.defpath}")
|
|
171
|
+
# parent_parsers.append(self._early_parser)
|
|
172
|
+
|
|
173
|
+
cmd_parser = self.build_single_command(cmdDef, subparsers, parent_parsers)
|
|
174
|
+
cmd_parsers.append(cmd_parser)
|
|
175
|
+
|
|
176
|
+
return cmd_parsers
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def build_single_command(
|
|
180
|
+
self,
|
|
181
|
+
cmdDef: CommandDef,
|
|
182
|
+
subparsers: argparse._SubParsersAction|None = None,
|
|
183
|
+
parent_parsers: Sequence[argparse.ArgumentParser]|None = None
|
|
184
|
+
) -> argparse.ArgumentParser:
|
|
185
|
+
if parent_parsers is None:
|
|
186
|
+
parent_parsers = []
|
|
187
|
+
if subparsers is not None:
|
|
188
|
+
parser = subparsers.add_parser(
|
|
189
|
+
cmdDef.key,
|
|
190
|
+
help = cmdDef.help,
|
|
191
|
+
parents=parent_parsers,
|
|
192
|
+
)
|
|
193
|
+
else:
|
|
194
|
+
kwargs = ({"add_help":True, "help":cmdDef.help}
|
|
195
|
+
if cmdDef.help is not None else
|
|
196
|
+
{"add_help":False}
|
|
197
|
+
)
|
|
198
|
+
parser = argparse.ArgumentParser(
|
|
199
|
+
cmdDef.key,
|
|
200
|
+
parents=parent_parsers,
|
|
201
|
+
**kwargs
|
|
202
|
+
)
|
|
203
|
+
self.build_arguments(cmdDef.arguments, parser)
|
|
204
|
+
if cmdDef.subcommands:
|
|
205
|
+
self.build_commands(cmdDef.subcommands, parser)
|
|
206
|
+
|
|
207
|
+
self._register(cmdDef, parser)
|
|
208
|
+
self._attach_metadata(parser, cmdDef, cmdDef.get_command_sequence())
|
|
209
|
+
|
|
210
|
+
return parser
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _attach_metadata(self, parser, command: CommandDef|CliDef, path):
|
|
214
|
+
parser.set_defaults(
|
|
215
|
+
_path=path,
|
|
216
|
+
_command=command, # ← これも入れる(重要)
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def to_nargs(self, mult: MultDef) -> str | int | None:
|
|
221
|
+
if mult is None:
|
|
222
|
+
return None
|
|
223
|
+
|
|
224
|
+
if mult.is_fixed: # lower == upper
|
|
225
|
+
if mult.lower == 1:
|
|
226
|
+
return None # to fix problem list ['param'] passing
|
|
227
|
+
return mult.lower
|
|
228
|
+
|
|
229
|
+
if mult.is_optional:
|
|
230
|
+
return "?"
|
|
231
|
+
|
|
232
|
+
if mult.is_unbounded:
|
|
233
|
+
if mult.lower == 0:
|
|
234
|
+
return "*"
|
|
235
|
+
elif mult.lower == 1:
|
|
236
|
+
return "+"
|
|
237
|
+
|
|
238
|
+
return f"{mult.lower}..{mult.upper}"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# cli_def/backend/click/click_binder.py
|
|
2
|
+
from typing import Any, Mapping
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
import click
|
|
7
|
+
except ImportError:
|
|
8
|
+
raise ImportError(
|
|
9
|
+
"click is required for click builder. Install with `cli-def[click]`"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
from ...runtime import (
|
|
13
|
+
CliDispatcher,
|
|
14
|
+
CliRuntimeContext,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
from ..protocols import BinderProtocol
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ClickBinder(BinderProtocol):
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
dispatcher: CliDispatcher,
|
|
25
|
+
ctx: CliRuntimeContext|None = None
|
|
26
|
+
):
|
|
27
|
+
self.dispatcher = dispatcher
|
|
28
|
+
self.runtime_ctx = ctx
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def bind(self, defpath_mapping: Mapping[str, object]):
|
|
32
|
+
for defpath, obj in defpath_mapping.items():
|
|
33
|
+
if isinstance(obj, click.Command):
|
|
34
|
+
# subcommand を持つGroupは callback を設定しない
|
|
35
|
+
if isinstance(obj, click.Group):
|
|
36
|
+
continue
|
|
37
|
+
#print(f"@@@ try to override callback of {defpath}")
|
|
38
|
+
obj.callback = self._make_callback(defpath)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _make_callback(self, defpath: str):
|
|
42
|
+
@click.pass_context
|
|
43
|
+
def callback(ctx, **kwargs):
|
|
44
|
+
logging.info(f"@@@ internal callback called: {defpath} passthrough={ctx.args}")
|
|
45
|
+
return self.dispatcher._dispatch(defpath, kwargs, extra_args=list(ctx.args), ctx=self.runtime_ctx)
|
|
46
|
+
return callback
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# cli_def/backend/click/click_builder.py
|
|
2
|
+
from typing import Any, Mapping, Iterable
|
|
3
|
+
try:
|
|
4
|
+
import click
|
|
5
|
+
except ImportError:
|
|
6
|
+
raise ImportError(
|
|
7
|
+
"click is required for click builder. Install with `cli-def[click]`"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
from ...models import (
|
|
11
|
+
CliDefNode,
|
|
12
|
+
CliDef,
|
|
13
|
+
CommandDef,
|
|
14
|
+
ArgumentDef,
|
|
15
|
+
MultDef
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from typing import Union
|
|
19
|
+
|
|
20
|
+
ClickObject = Union[
|
|
21
|
+
click.Group,
|
|
22
|
+
click.Command,
|
|
23
|
+
click.Option,
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
GroupOrCommand = Union[
|
|
27
|
+
click.Group,
|
|
28
|
+
click.Command,
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
from ..protocols import BuilderProtocol
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ClickBuilder(BuilderProtocol):
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
self._defpath_mapping: dict[str, ClickObject] = {}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def defpath_mapping(self) -> Mapping[str, ClickObject]:
|
|
42
|
+
return self._defpath_mapping
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def build(self, cliDef: CliDef) -> GroupOrCommand:
|
|
46
|
+
return self.build_click(cliDef)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _register(self, node, obj):
|
|
50
|
+
self._defpath_mapping[node.defpath] = obj
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def build_click(self, cliDef: CliDef) -> GroupOrCommand:
|
|
54
|
+
if not cliDef.commands:
|
|
55
|
+
return self._build_root_command(cliDef)
|
|
56
|
+
|
|
57
|
+
return self._build_root_group(cliDef)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _build_root_command(self, cliDef: CliDef) -> click.Command:
|
|
61
|
+
params = [self._build_param(arg) for arg in cliDef.arguments]
|
|
62
|
+
|
|
63
|
+
def callback(**kwargs):
|
|
64
|
+
click.echo(f"{cliDef.defpath} {kwargs}")
|
|
65
|
+
|
|
66
|
+
cmd = click.Command(
|
|
67
|
+
name=cliDef.key,
|
|
68
|
+
help=cliDef.help,
|
|
69
|
+
params=params,
|
|
70
|
+
callback=callback,
|
|
71
|
+
context_settings={
|
|
72
|
+
"ignore_unknown_options": True,
|
|
73
|
+
"allow_extra_args": True,
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
self._register(cliDef, cmd)
|
|
78
|
+
|
|
79
|
+
return cmd
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _build_root_group(self, cliDef: CliDef) -> click.Group:
|
|
83
|
+
root = click.Group(name=cliDef.key, help=cliDef.help)
|
|
84
|
+
|
|
85
|
+
self._register(cliDef, root)
|
|
86
|
+
|
|
87
|
+
# 引数
|
|
88
|
+
self._attach_params(root, cliDef.arguments)
|
|
89
|
+
|
|
90
|
+
# コマンド
|
|
91
|
+
if cliDef.commands:
|
|
92
|
+
self._attach_commands(root, cliDef.commands)
|
|
93
|
+
|
|
94
|
+
return root
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _build_command(self, cmdDef: CommandDef) -> click.Command:
|
|
98
|
+
params = [self._build_param(arg) for arg in cmdDef.arguments]
|
|
99
|
+
# collect arguments from template
|
|
100
|
+
for tmpl_cmd in cmdDef.get_templates():
|
|
101
|
+
params.extend([self._build_param(arg) for arg in tmpl_cmd.arguments])
|
|
102
|
+
|
|
103
|
+
if cmdDef.subcommands:
|
|
104
|
+
grp = click.Group(name=cmdDef.key, help=cmdDef.help, params=params)
|
|
105
|
+
|
|
106
|
+
self._register(cmdDef, grp)
|
|
107
|
+
|
|
108
|
+
for sub in cmdDef.subcommands:
|
|
109
|
+
grp.add_command(self._build_command(sub))
|
|
110
|
+
|
|
111
|
+
return grp
|
|
112
|
+
|
|
113
|
+
# leaf command
|
|
114
|
+
def callback(**kwargs):
|
|
115
|
+
click.echo(f"{cmdDef.defpath} {kwargs}")
|
|
116
|
+
|
|
117
|
+
cmd = click.Command(
|
|
118
|
+
name=cmdDef.key,
|
|
119
|
+
help=cmdDef.help,
|
|
120
|
+
params=params,
|
|
121
|
+
callback=callback,
|
|
122
|
+
hidden=cmdDef.is_template,
|
|
123
|
+
context_settings={
|
|
124
|
+
"ignore_unknown_options": True,
|
|
125
|
+
"allow_extra_args": True,
|
|
126
|
+
}
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
self._register(cmdDef, cmd)
|
|
130
|
+
|
|
131
|
+
return cmd
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _build_param(self, arg: ArgumentDef) -> click.Parameter:
|
|
135
|
+
if arg.option:
|
|
136
|
+
# option
|
|
137
|
+
param = click.Option(
|
|
138
|
+
#[arg.option, arg.key], # works but ...
|
|
139
|
+
[arg.option] + (arg.aliases or []),
|
|
140
|
+
help=arg.help,
|
|
141
|
+
default=arg.default,
|
|
142
|
+
is_flag=arg.is_flag or False,
|
|
143
|
+
)
|
|
144
|
+
param.name = arg.key
|
|
145
|
+
else:
|
|
146
|
+
# positional
|
|
147
|
+
param = click.Argument(
|
|
148
|
+
param_decls=[arg.key],
|
|
149
|
+
#nargs=self.to_nargs(arg.mult),
|
|
150
|
+
)
|
|
151
|
+
self.apply_mult(param, arg.mult)
|
|
152
|
+
|
|
153
|
+
self._register(arg, param)
|
|
154
|
+
|
|
155
|
+
return param
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _attach_commands(
|
|
159
|
+
self,
|
|
160
|
+
parent: click.Group,
|
|
161
|
+
commands: Iterable[CommandDef]
|
|
162
|
+
):
|
|
163
|
+
for cmd in commands or []:
|
|
164
|
+
parent.add_command(self._build_command(cmd))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _attach_params(
|
|
168
|
+
self,
|
|
169
|
+
cmd: click.Command,
|
|
170
|
+
arguments: Iterable[ArgumentDef]
|
|
171
|
+
):
|
|
172
|
+
for arg in arguments or []:
|
|
173
|
+
cmd.params.append(self._build_param(arg))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def apply_mult(self, param: click.Parameter, mult: MultDef):
|
|
177
|
+
if mult.is_fixed:
|
|
178
|
+
param.nargs = mult.lower
|
|
179
|
+
elif mult.is_unbounded:
|
|
180
|
+
param.multiple = True
|
|
181
|
+
elif mult.is_optional:
|
|
182
|
+
param.nargs = 1
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# cli_def/backend/protocols.py
|
|
2
|
+
|
|
3
|
+
from typing import Protocol, Mapping
|
|
4
|
+
|
|
5
|
+
from ..models import CliDef
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class BuilderProtocol(Protocol):
|
|
9
|
+
|
|
10
|
+
@property
|
|
11
|
+
def defpath_mapping(self) -> Mapping[str, object]:
|
|
12
|
+
...
|
|
13
|
+
|
|
14
|
+
def build(self, cliDef: CliDef) -> object:
|
|
15
|
+
...
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BinderProtocol(Protocol):
|
|
20
|
+
|
|
21
|
+
def bind(self, defpath_mapping: Mapping[str, object]):
|
|
22
|
+
...
|
cli_def/demo/__init__.py
ADDED
cli_def/demo/handlers.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# cli_def/demo/handlers.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from cli_def.runtime import (
|
|
7
|
+
CliSession,
|
|
8
|
+
CliEvent,
|
|
9
|
+
CliHandlerResult,
|
|
10
|
+
cli_def_handler,
|
|
11
|
+
)
|
|
12
|
+
from cli_def.ops import (
|
|
13
|
+
load_cli_def_beside,
|
|
14
|
+
)
|
|
15
|
+
from cli_def.ops.dumper import CliDefDumper
|
|
16
|
+
|
|
17
|
+
# --------------------------------------------------------------------------------
|
|
18
|
+
# demo handler
|
|
19
|
+
# load profiles and go repl
|
|
20
|
+
# --------------------------------------------------------------------------------
|
|
21
|
+
@cli_def_handler("/cli-def/demo", late_binding=True)
|
|
22
|
+
def run_demo(event: CliEvent):
|
|
23
|
+
profile = event.params.get("profile") or "beginner"
|
|
24
|
+
|
|
25
|
+
cli_def = load_cli_def_beside(Path(__file__) , "profiles", profile + ".toml")
|
|
26
|
+
|
|
27
|
+
if cli_def is None:
|
|
28
|
+
msg = f"demo profile could not load: {profile}"
|
|
29
|
+
print(msg)
|
|
30
|
+
return CliHandlerResult.make_error(event, msg)
|
|
31
|
+
|
|
32
|
+
CliDefDumper.dump_pretty(cli_def, as_help=True)
|
|
33
|
+
|
|
34
|
+
print(f"=== demo: {profile} ===")
|
|
35
|
+
print("Type 'help' to list commands, 'exit' to exit")
|
|
36
|
+
|
|
37
|
+
# go repl
|
|
38
|
+
session = CliSession(
|
|
39
|
+
cli_def,
|
|
40
|
+
profile=profile,
|
|
41
|
+
ctx=event.ctx,
|
|
42
|
+
)
|
|
43
|
+
session.repl(prompt=f"demo[{profile}]> ")
|
|
44
|
+
|
|
45
|
+
return CliHandlerResult.make_result(
|
|
46
|
+
event,
|
|
47
|
+
"run_repl",
|
|
48
|
+
data=session.result_store.all_data()
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# --------------------------------------------------------------------------------
|
|
53
|
+
# handlers for commands in beginner profile
|
|
54
|
+
# --------------------------------------------------------------------------------
|
|
55
|
+
def echo(event: CliEvent) -> CliHandlerResult:
|
|
56
|
+
msg = event.params.get("message", [])
|
|
57
|
+
result = " ".join(msg)
|
|
58
|
+
print(result)
|
|
59
|
+
|
|
60
|
+
return CliHandlerResult.make_result(
|
|
61
|
+
event,
|
|
62
|
+
"message",
|
|
63
|
+
data=result,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def greet(event: CliEvent) -> CliHandlerResult:
|
|
68
|
+
name = event.params.get("name") or "world"
|
|
69
|
+
text = f"Hello, {name}!"
|
|
70
|
+
|
|
71
|
+
if event.params.get("upper"):
|
|
72
|
+
text = text.upper()
|
|
73
|
+
|
|
74
|
+
print(text)
|
|
75
|
+
|
|
76
|
+
return CliHandlerResult.make_result(
|
|
77
|
+
event,
|
|
78
|
+
"greet",
|
|
79
|
+
data=text,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# --------------------------------------------------------------------------------
|
|
84
|
+
# handlers for commands in advanced profile
|
|
85
|
+
# --------------------------------------------------------------------------------
|
|
86
|
+
def build(event: CliEvent) -> CliHandlerResult:
|
|
87
|
+
target = event.params["target"]
|
|
88
|
+
verbose = event.params.get("verbose", False)
|
|
89
|
+
|
|
90
|
+
text = f"[build] target={target} verbose={verbose}"
|
|
91
|
+
print(text)
|
|
92
|
+
|
|
93
|
+
return CliHandlerResult.make_result(
|
|
94
|
+
event,
|
|
95
|
+
"build",
|
|
96
|
+
data=text,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test(event: CliEvent) -> CliHandlerResult:
|
|
101
|
+
pattern = event.params.get("pattern") or "all"
|
|
102
|
+
verbose = event.params.get("verbose", False)
|
|
103
|
+
|
|
104
|
+
text = f"[test] pattern={pattern} verbose={verbose}"
|
|
105
|
+
print(text)
|
|
106
|
+
|
|
107
|
+
return CliHandlerResult.make_result(
|
|
108
|
+
event,
|
|
109
|
+
"test",
|
|
110
|
+
data=text,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def deploy(event: CliEvent) -> CliHandlerResult:
|
|
115
|
+
env = event.params.get("env") or "dev"
|
|
116
|
+
force = event.params.get("force", False)
|
|
117
|
+
|
|
118
|
+
text = f"[deploy] env={env} force={force}"
|
|
119
|
+
print(text)
|
|
120
|
+
|
|
121
|
+
return CliHandlerResult.make_result(
|
|
122
|
+
event,
|
|
123
|
+
"deploy",
|
|
124
|
+
data=text,
|
|
125
|
+
)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# cli_def/demo/profiles/advanced.toml
|
|
2
|
+
|
|
3
|
+
[cli]
|
|
4
|
+
key = "demo"
|
|
5
|
+
help = "Advanced demo CLI build-test-deploy"
|
|
6
|
+
prompt = "advanced> " # be used when repl mode
|
|
7
|
+
|
|
8
|
+
# --- template ----------------------------------------
|
|
9
|
+
|
|
10
|
+
[cli._common]
|
|
11
|
+
args = [
|
|
12
|
+
{key="verbose", option="--verbose", aliases=["-v"], is_flag=true}
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
# --- commands ----------------------------------------
|
|
16
|
+
|
|
17
|
+
[cli.build]
|
|
18
|
+
help = "Build target"
|
|
19
|
+
inherit_from = ["_common"]
|
|
20
|
+
args = [
|
|
21
|
+
{key="target", mult="1", type="str"}
|
|
22
|
+
]
|
|
23
|
+
entrypoint = "cli_def.demo.handlers:build"
|
|
24
|
+
|
|
25
|
+
[cli.test]
|
|
26
|
+
help = "Run tests"
|
|
27
|
+
inherit_from = ["_common"]
|
|
28
|
+
args = [
|
|
29
|
+
{key="pattern", mult="?", type="str"}
|
|
30
|
+
]
|
|
31
|
+
entrypoint = "cli_def.demo.handlers:test"
|
|
32
|
+
|
|
33
|
+
[cli.deploy]
|
|
34
|
+
help = "Deploy application"
|
|
35
|
+
args = [
|
|
36
|
+
{key="env", option="--env", mult="?", type="str"},
|
|
37
|
+
{key="force", option="--force", is_flag=true}
|
|
38
|
+
]
|
|
39
|
+
entrypoint = "cli_def.demo.handlers:deploy"
|