cli-def 0.2.0__tar.gz → 0.3.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.
- cli_def-0.3.0/PKG-INFO +340 -0
- cli_def-0.3.0/README.md +297 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/pyproject.toml +3 -4
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/argparse/argparse_builder.py +12 -4
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/click/click_builder.py +18 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/__init__.py +26 -0
- cli_def-0.3.0/src/cli_def/core/models/builtin_spec_types.py +162 -0
- cli_def-0.3.0/src/cli_def/core/models/common/spec_type_def.py +39 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/argument_def.py +9 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/protocols.py +4 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/argument_def.py +13 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/cli_def.py +7 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/argument_def.py +10 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/cli_def.py +18 -0
- cli_def-0.3.0/src/cli_def/core/models/spec_type.py +377 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/parser/parser.py +50 -6
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/resolver/resolver.py +130 -23
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/validator/validator.py +34 -0
- cli_def-0.3.0/src/cli_def/demo/profiles/beginner_test.toml +30 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/dumper.py +0 -8
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/resources/cli_def.toml +39 -5
- cli_def-0.3.0/src/cli_def/resources/spec_types.toml +14 -0
- cli_def-0.3.0/src/cli_def/resources/templates/basic/cli_def.toml +12 -0
- cli_def-0.3.0/src/cli_def/resources/templates/basic/handlers.py +6 -0
- cli_def-0.3.0/src/cli_def/resources/templates/basic/main.py +10 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/__init__.py +12 -1
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/dispatcher.py +38 -8
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/runner.py +23 -1
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/session.py +11 -1
- cli_def-0.3.0/src/cli_def/runtime/spec_type_support.py +175 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/dump.py +14 -3
- cli_def-0.3.0/src/cli_def/script/handlers/init.py +48 -0
- cli_def-0.3.0/src/cli_def/script/handlers/test.py +65 -0
- cli_def-0.3.0/src/cli_def/test_support/__init__.py +8 -0
- cli_def-0.3.0/src/cli_def/test_support/test_generator.py +85 -0
- cli_def-0.3.0/src/cli_def/test_support/test_runner.py +240 -0
- cli_def-0.3.0/src/cli_def.egg-info/PKG-INFO +340 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def.egg-info/SOURCES.txt +15 -2
- cli_def-0.2.0/PKG-INFO +0 -187
- cli_def-0.2.0/README.md +0 -143
- cli_def-0.2.0/src/cli_def/ops/utils/simple_pager.py +0 -30
- cli_def-0.2.0/src/cli_def.egg-info/PKG-INFO +0 -187
- {cli_def-0.2.0 → cli_def-0.3.0}/LICENSE +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/setup.cfg +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/argparse/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/click/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/click/click_binder.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/backend/protocols.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/basic/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/basic/basic_types.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/common/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/common/mult_def.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/abstract_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/cli_def.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/command_def.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/generic/executable_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/command_def.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/executable_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/raw_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/raw/raw_protocols.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/command_def.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/executable_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/resolved_node.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/models/resolved/resolved_protocols.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/parser/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/resolver/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/core/validator/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/dummy.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/dummy2.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/handlers.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/profiles/advanced.toml +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/demo/profiles/beginner.toml +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/loader.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/utils/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/utils/pretty_renderer.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/utils/renderer.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/ops/utils/renderer_ops.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/_internal/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/_internal/scan_runner.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/context.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/event.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/handler_support.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/result.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/scanner.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/runtime/utils.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/common.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/__init__.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/repl.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/run.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/scan.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/search.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/handlers/validate.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def/script/main.py +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def.egg-info/dependency_links.txt +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def.egg-info/entry_points.txt +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def.egg-info/requires.txt +0 -0
- {cli_def-0.2.0 → cli_def-0.3.0}/src/cli_def.egg-info/top_level.txt +0 -0
cli_def-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cli-def
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A declarative DSL for composable CLI applications with runtime and REPL
|
|
5
|
+
Author: Tomesoft
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2026 Tomesoft
|
|
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 do so, subject to the
|
|
15
|
+
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
|
+
Project-URL: Homepage, https://cli-def.tomesoft.net
|
|
28
|
+
Project-URL: Repository, https://github.com/tomesoft/cli-def
|
|
29
|
+
Classifier: Programming Language :: Python :: 3
|
|
30
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
31
|
+
Classifier: Operating System :: OS Independent
|
|
32
|
+
Requires-Python: >=3.12
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
License-File: LICENSE
|
|
35
|
+
Requires-Dist: wcwidth
|
|
36
|
+
Provides-Extra: click
|
|
37
|
+
Requires-Dist: click>=8.0; extra == "click"
|
|
38
|
+
Provides-Extra: repl
|
|
39
|
+
Requires-Dist: prompt_toolkit; extra == "repl"
|
|
40
|
+
Provides-Extra: dev
|
|
41
|
+
Requires-Dist: pytest; extra == "dev"
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
# cli-def
|
|
45
|
+
|
|
46
|
+
[日本語](https://github.com/tomesoft/cli-def/blob/main/README.ja.md)
|
|
47
|
+
|
|
48
|
+
A declarative DSL for defining CLI structures with a unified runtime and REPL for argparse/click backends.
|
|
49
|
+
|
|
50
|
+
project page:
|
|
51
|
+
https://cli-def.tomesoft.net
|
|
52
|
+
|
|
53
|
+
source:
|
|
54
|
+
https://github.com/tomesoft/cli-def
|
|
55
|
+
|
|
56
|
+
## What's new in 0.3.0
|
|
57
|
+
|
|
58
|
+
- Reusable argument specification types defined in TOML or registered in Python
|
|
59
|
+
- Regex, choices, integer ranges, path existence checks, and aliases
|
|
60
|
+
- Pre-dispatch validation with strict or permissive handling of unknown types
|
|
61
|
+
- Custom pre-dispatch hooks and built-in specification types
|
|
62
|
+
|
|
63
|
+
## What's new in 0.2.0
|
|
64
|
+
|
|
65
|
+
- Introduced Resolved model layer
|
|
66
|
+
- Added parameter binding (`bind`)
|
|
67
|
+
- Added `validate` command
|
|
68
|
+
- Improved inheritance (`inherit_from`)
|
|
69
|
+
- Added support for relative paths (`../`)
|
|
70
|
+
- Improved help and dump output
|
|
71
|
+
- Introduced `_early` parsing phase
|
|
72
|
+
|
|
73
|
+
## ✨ Features
|
|
74
|
+
|
|
75
|
+
- Define CLI structure declaratively (TOML / Python models)
|
|
76
|
+
- Generate CLI implementations for:
|
|
77
|
+
- `argparse` (standard library, no dependencies)
|
|
78
|
+
- `click` (optional dependency)
|
|
79
|
+
- Separation of:
|
|
80
|
+
- CLI definition (DSL)
|
|
81
|
+
- Runtime implementation (builders)
|
|
82
|
+
- Built-in runtime system:
|
|
83
|
+
- Event model (`CliEvent`)
|
|
84
|
+
- Runner
|
|
85
|
+
- Dispatcher
|
|
86
|
+
- Entrypoint resolution (`module:function`)
|
|
87
|
+
- Result propagation
|
|
88
|
+
- Interactive REPL support
|
|
89
|
+
- CLI chaining (execute one CLI from another)
|
|
90
|
+
|
|
91
|
+
## 📦 Installation
|
|
92
|
+
|
|
93
|
+
Requires Python 3.12 or later.
|
|
94
|
+
|
|
95
|
+
Core (argparse only)
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pip install cli-def
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
With click support
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
pip install cli-def[click]
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 🚀 Quick Example
|
|
108
|
+
|
|
109
|
+
```toml
|
|
110
|
+
[cli]
|
|
111
|
+
key = "Your CLI"
|
|
112
|
+
help = "Help of your CLI"
|
|
113
|
+
prompt = "yourcli> "
|
|
114
|
+
|
|
115
|
+
[cli.echo]
|
|
116
|
+
args = [{key="message", mult="+"}]
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
cli-def run example.toml -- echo hello world
|
|
121
|
+
|
|
122
|
+
[run] forwarding args: ['echo', 'hello', 'world'], no_ctx_propagate: False
|
|
123
|
+
=== fallback handler ===
|
|
124
|
+
PATH: ['Your CLI', 'echo']
|
|
125
|
+
PARAMS: {'message': ['hello', 'world']}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 🚀 Quick Start (Demo)
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
cli-def demo beginner
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
beginner profile reads in beginner.toml (see GitHub repository for details)
|
|
135
|
+
|
|
136
|
+
```text
|
|
137
|
+
Type 'help' to list commands, 'exit' to exit
|
|
138
|
+
demo[beginner]> echo a b c d
|
|
139
|
+
a b c d
|
|
140
|
+
|
|
141
|
+
-> out[0]
|
|
142
|
+
demo[beginner]> greet John --upper
|
|
143
|
+
HELLO, JOHN!
|
|
144
|
+
|
|
145
|
+
-> out[1]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Advanced demo:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
cli-def demo advanced
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
See profiles:
|
|
155
|
+
https://github.com/tomesoft/cli-def/tree/main/src/cli_def/demo/profiles
|
|
156
|
+
|
|
157
|
+
## Pre-dispatch hook
|
|
158
|
+
|
|
159
|
+
An application can provide a single hook to inspect the normalized event before
|
|
160
|
+
the handler runs, with either backend:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
from cli_def.runtime import CliRunner, PreDispatchError
|
|
164
|
+
|
|
165
|
+
def check_event(event):
|
|
166
|
+
if event.params.get("where") == "invalid":
|
|
167
|
+
raise PreDispatchError("Invalid path")
|
|
168
|
+
|
|
169
|
+
runner = CliRunner(cli_def, pre_dispatch_hook=check_event)
|
|
170
|
+
result = runner.run(["search", "--where", "invalid"])
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Treat the event as read-only. The hook also runs before a dry-run handler. A
|
|
174
|
+
`PreDispatchError` is logged and produces a failed `CliHandlerResult` and exit
|
|
175
|
+
code 1 without calling the handler; other exceptions propagate. When omitted or
|
|
176
|
+
set to `None`, the standard hook validates `spec_type`. A custom hook replaces
|
|
177
|
+
the standard hook. `CliSession` also accepts the hook and mode, retaining them
|
|
178
|
+
across reloads:
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from cli_def.core.models import get_spec_type_registry, register_builtin_spec_types
|
|
182
|
+
from cli_def.runtime import CliRunner, empty_pre_dispatch_hook
|
|
183
|
+
|
|
184
|
+
register_builtin_spec_types(get_spec_type_registry())
|
|
185
|
+
runner = CliRunner(cli_def) # permissive
|
|
186
|
+
runner = CliRunner(cli_def, spec_type_mode="strict")
|
|
187
|
+
runner = CliRunner(cli_def, pre_dispatch_hook=empty_pre_dispatch_hook)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`permissive` (the default) warns and skips unknown spec types; `strict` treats
|
|
191
|
+
them as errors. Both modes reject invalid values for known spec types.
|
|
192
|
+
Built-in types must be registered explicitly. `spec_type_mode` applies only
|
|
193
|
+
to the standard hook, not to a custom hook.
|
|
194
|
+
|
|
195
|
+
Register spec types beforehand or define them in TOML:
|
|
196
|
+
|
|
197
|
+
The built-in TOML definitions can be included alongside local files:
|
|
198
|
+
|
|
199
|
+
```toml
|
|
200
|
+
[cli]
|
|
201
|
+
key = "MyApp"
|
|
202
|
+
include = ["builtin:spec_types.toml"]
|
|
203
|
+
|
|
204
|
+
[cli.search]
|
|
205
|
+
args = [{key = "where", option = "--where", spec_type = "posix_path"}]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The included types are `ascii_identifier` (ASCII letter followed by letters
|
|
209
|
+
or digits), `slug` (lowercase alphanumeric segments separated by hyphens),
|
|
210
|
+
`uuid` (UUID textual form), `hex` (hexadecimal characters), and `posix_path`
|
|
211
|
+
(a non-empty path string without NUL). `posix_path` permits Unicode, whitespace,
|
|
212
|
+
newlines, and backslashes; it does not check existence, permissions, lengths,
|
|
213
|
+
encoding, or whether a particular filesystem accepts the path.
|
|
214
|
+
|
|
215
|
+
Custom definitions can be declared as follows:
|
|
216
|
+
|
|
217
|
+
```toml
|
|
218
|
+
[spec_types.identifier]
|
|
219
|
+
regex_pattern = '[a-zA-Z][a-zA-Z0-9]*'
|
|
220
|
+
|
|
221
|
+
[cli]
|
|
222
|
+
key = "MyApp"
|
|
223
|
+
|
|
224
|
+
[cli.search]
|
|
225
|
+
args = [
|
|
226
|
+
{key = "where", option = "--where", spec_type = "identifier"},
|
|
227
|
+
{key = "ignore", option = "--ignore", spec_type = "identifier"},
|
|
228
|
+
]
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Patterns use Python `re.fullmatch`. An omitted optional value is skipped and
|
|
232
|
+
multiple values are checked individually. Invalid patterns and duplicate names
|
|
233
|
+
across includes fail during loading/resolution; duplicates with the registry
|
|
234
|
+
fail during validation. The hook does not transform values; registered
|
|
235
|
+
validators receive the complete value when it is present.
|
|
236
|
+
|
|
237
|
+
An alias inherits an existing TOML specification while retaining its own name:
|
|
238
|
+
|
|
239
|
+
```toml
|
|
240
|
+
[spec_types.my_identifier]
|
|
241
|
+
extends = "identifier"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
This allows a custom hook to add checks for `my_identifier` after calling
|
|
245
|
+
`default_pre_dispatch_hook`. Targets may come from includes; missing targets,
|
|
246
|
+
cycles, and combining `extends` with other fields are errors. Registry-only
|
|
247
|
+
types cannot be alias targets.
|
|
248
|
+
|
|
249
|
+
Specification types can instead declare non-empty string choices:
|
|
250
|
+
|
|
251
|
+
```toml
|
|
252
|
+
[spec_types.color]
|
|
253
|
+
choices = ["red", "green", "blue"]
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Arguments inherit these choices and may narrow them using their own `choices`.
|
|
257
|
+
`choices` and `regex_pattern` cannot be combined. Both CLI backends enforce
|
|
258
|
+
the resulting choices for explicit input even when the pre-dispatch hook is
|
|
259
|
+
disabled; the standard hook also checks present default and bound values.
|
|
260
|
+
|
|
261
|
+
Integer choices are supported with `base_type = "num"`:
|
|
262
|
+
|
|
263
|
+
```toml
|
|
264
|
+
[spec_types.one_to_three]
|
|
265
|
+
base_type = "num"
|
|
266
|
+
choices = [1, 2, 3]
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
CLI values are compared using Python's `int()` without changing the value passed
|
|
270
|
+
to the handler. Floating-point choices are not yet supported.
|
|
271
|
+
|
|
272
|
+
An integer range can be specified instead:
|
|
273
|
+
|
|
274
|
+
```toml
|
|
275
|
+
[spec_types.port_number]
|
|
276
|
+
base_type = "num"
|
|
277
|
+
min = 1
|
|
278
|
+
max = 65535
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Bounds are inclusive; either can be omitted. Ranges cannot be combined with
|
|
282
|
+
specification-level `choices` or `regex_pattern`. The pre-dispatch hook checks
|
|
283
|
+
the range, so disabling the hook also disables range validation. Floating-point
|
|
284
|
+
ranges are not yet supported.
|
|
285
|
+
|
|
286
|
+
An application can declare an existing file or directory type:
|
|
287
|
+
|
|
288
|
+
```toml
|
|
289
|
+
[spec_types.existing_file_path]
|
|
290
|
+
base_type = "posix_path"
|
|
291
|
+
exists = true
|
|
292
|
+
kind = "file" # omit for either a file or directory
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
`kind = "directory"` requires a directory. The standard pre-dispatch hook
|
|
296
|
+
checks existence, follows symbolic links, and resolves relative paths against
|
|
297
|
+
the process working directory. It does not open the path or guarantee it
|
|
298
|
+
remains available. Disabling the hook disables this check.
|
|
299
|
+
|
|
300
|
+
## 🖥 Interactive Mode (REPL)
|
|
301
|
+
|
|
302
|
+
```bash
|
|
303
|
+
cli-def repl --file your_cli.toml
|
|
304
|
+
yourcli> help
|
|
305
|
+
yourcli> command arg1 --option value
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## 🔁 Run Another CLI (CLI chaining)
|
|
309
|
+
|
|
310
|
+
```bash
|
|
311
|
+
cli-def run example.toml -- command arg1 arg2
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
- `--` separates cli-def arguments from the target CLI arguments
|
|
315
|
+
- Remaining arguments are forwarded to the next CLI
|
|
316
|
+
|
|
317
|
+
## 📚 Documentation
|
|
318
|
+
|
|
319
|
+
Full documentation and design details are available on GitHub:
|
|
320
|
+
|
|
321
|
+
https://github.com/tomesoft/cli-def
|
|
322
|
+
|
|
323
|
+
- DSL specification
|
|
324
|
+
- Architecture
|
|
325
|
+
- Examples
|
|
326
|
+
- Development notes
|
|
327
|
+
|
|
328
|
+
## 📌 Roadmap
|
|
329
|
+
|
|
330
|
+
See TODO.md on GitHub:
|
|
331
|
+
https://github.com/tomesoft/cli-def/blob/main/TODO.md
|
|
332
|
+
|
|
333
|
+
## 🤝 Contributing
|
|
334
|
+
|
|
335
|
+
Contributions are welcome!
|
|
336
|
+
|
|
337
|
+
## 📄 License
|
|
338
|
+
|
|
339
|
+
MIT License
|
|
340
|
+
|
cli_def-0.3.0/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# cli-def
|
|
2
|
+
|
|
3
|
+
[日本語](https://github.com/tomesoft/cli-def/blob/main/README.ja.md)
|
|
4
|
+
|
|
5
|
+
A declarative DSL for defining CLI structures with a unified runtime and REPL for argparse/click backends.
|
|
6
|
+
|
|
7
|
+
project page:
|
|
8
|
+
https://cli-def.tomesoft.net
|
|
9
|
+
|
|
10
|
+
source:
|
|
11
|
+
https://github.com/tomesoft/cli-def
|
|
12
|
+
|
|
13
|
+
## What's new in 0.3.0
|
|
14
|
+
|
|
15
|
+
- Reusable argument specification types defined in TOML or registered in Python
|
|
16
|
+
- Regex, choices, integer ranges, path existence checks, and aliases
|
|
17
|
+
- Pre-dispatch validation with strict or permissive handling of unknown types
|
|
18
|
+
- Custom pre-dispatch hooks and built-in specification types
|
|
19
|
+
|
|
20
|
+
## What's new in 0.2.0
|
|
21
|
+
|
|
22
|
+
- Introduced Resolved model layer
|
|
23
|
+
- Added parameter binding (`bind`)
|
|
24
|
+
- Added `validate` command
|
|
25
|
+
- Improved inheritance (`inherit_from`)
|
|
26
|
+
- Added support for relative paths (`../`)
|
|
27
|
+
- Improved help and dump output
|
|
28
|
+
- Introduced `_early` parsing phase
|
|
29
|
+
|
|
30
|
+
## ✨ Features
|
|
31
|
+
|
|
32
|
+
- Define CLI structure declaratively (TOML / Python models)
|
|
33
|
+
- Generate CLI implementations for:
|
|
34
|
+
- `argparse` (standard library, no dependencies)
|
|
35
|
+
- `click` (optional dependency)
|
|
36
|
+
- Separation of:
|
|
37
|
+
- CLI definition (DSL)
|
|
38
|
+
- Runtime implementation (builders)
|
|
39
|
+
- Built-in runtime system:
|
|
40
|
+
- Event model (`CliEvent`)
|
|
41
|
+
- Runner
|
|
42
|
+
- Dispatcher
|
|
43
|
+
- Entrypoint resolution (`module:function`)
|
|
44
|
+
- Result propagation
|
|
45
|
+
- Interactive REPL support
|
|
46
|
+
- CLI chaining (execute one CLI from another)
|
|
47
|
+
|
|
48
|
+
## 📦 Installation
|
|
49
|
+
|
|
50
|
+
Requires Python 3.12 or later.
|
|
51
|
+
|
|
52
|
+
Core (argparse only)
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install cli-def
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
With click support
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install cli-def[click]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 🚀 Quick Example
|
|
65
|
+
|
|
66
|
+
```toml
|
|
67
|
+
[cli]
|
|
68
|
+
key = "Your CLI"
|
|
69
|
+
help = "Help of your CLI"
|
|
70
|
+
prompt = "yourcli> "
|
|
71
|
+
|
|
72
|
+
[cli.echo]
|
|
73
|
+
args = [{key="message", mult="+"}]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
cli-def run example.toml -- echo hello world
|
|
78
|
+
|
|
79
|
+
[run] forwarding args: ['echo', 'hello', 'world'], no_ctx_propagate: False
|
|
80
|
+
=== fallback handler ===
|
|
81
|
+
PATH: ['Your CLI', 'echo']
|
|
82
|
+
PARAMS: {'message': ['hello', 'world']}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 🚀 Quick Start (Demo)
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
cli-def demo beginner
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
beginner profile reads in beginner.toml (see GitHub repository for details)
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
Type 'help' to list commands, 'exit' to exit
|
|
95
|
+
demo[beginner]> echo a b c d
|
|
96
|
+
a b c d
|
|
97
|
+
|
|
98
|
+
-> out[0]
|
|
99
|
+
demo[beginner]> greet John --upper
|
|
100
|
+
HELLO, JOHN!
|
|
101
|
+
|
|
102
|
+
-> out[1]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Advanced demo:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
cli-def demo advanced
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
See profiles:
|
|
112
|
+
https://github.com/tomesoft/cli-def/tree/main/src/cli_def/demo/profiles
|
|
113
|
+
|
|
114
|
+
## Pre-dispatch hook
|
|
115
|
+
|
|
116
|
+
An application can provide a single hook to inspect the normalized event before
|
|
117
|
+
the handler runs, with either backend:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from cli_def.runtime import CliRunner, PreDispatchError
|
|
121
|
+
|
|
122
|
+
def check_event(event):
|
|
123
|
+
if event.params.get("where") == "invalid":
|
|
124
|
+
raise PreDispatchError("Invalid path")
|
|
125
|
+
|
|
126
|
+
runner = CliRunner(cli_def, pre_dispatch_hook=check_event)
|
|
127
|
+
result = runner.run(["search", "--where", "invalid"])
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Treat the event as read-only. The hook also runs before a dry-run handler. A
|
|
131
|
+
`PreDispatchError` is logged and produces a failed `CliHandlerResult` and exit
|
|
132
|
+
code 1 without calling the handler; other exceptions propagate. When omitted or
|
|
133
|
+
set to `None`, the standard hook validates `spec_type`. A custom hook replaces
|
|
134
|
+
the standard hook. `CliSession` also accepts the hook and mode, retaining them
|
|
135
|
+
across reloads:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from cli_def.core.models import get_spec_type_registry, register_builtin_spec_types
|
|
139
|
+
from cli_def.runtime import CliRunner, empty_pre_dispatch_hook
|
|
140
|
+
|
|
141
|
+
register_builtin_spec_types(get_spec_type_registry())
|
|
142
|
+
runner = CliRunner(cli_def) # permissive
|
|
143
|
+
runner = CliRunner(cli_def, spec_type_mode="strict")
|
|
144
|
+
runner = CliRunner(cli_def, pre_dispatch_hook=empty_pre_dispatch_hook)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
`permissive` (the default) warns and skips unknown spec types; `strict` treats
|
|
148
|
+
them as errors. Both modes reject invalid values for known spec types.
|
|
149
|
+
Built-in types must be registered explicitly. `spec_type_mode` applies only
|
|
150
|
+
to the standard hook, not to a custom hook.
|
|
151
|
+
|
|
152
|
+
Register spec types beforehand or define them in TOML:
|
|
153
|
+
|
|
154
|
+
The built-in TOML definitions can be included alongside local files:
|
|
155
|
+
|
|
156
|
+
```toml
|
|
157
|
+
[cli]
|
|
158
|
+
key = "MyApp"
|
|
159
|
+
include = ["builtin:spec_types.toml"]
|
|
160
|
+
|
|
161
|
+
[cli.search]
|
|
162
|
+
args = [{key = "where", option = "--where", spec_type = "posix_path"}]
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The included types are `ascii_identifier` (ASCII letter followed by letters
|
|
166
|
+
or digits), `slug` (lowercase alphanumeric segments separated by hyphens),
|
|
167
|
+
`uuid` (UUID textual form), `hex` (hexadecimal characters), and `posix_path`
|
|
168
|
+
(a non-empty path string without NUL). `posix_path` permits Unicode, whitespace,
|
|
169
|
+
newlines, and backslashes; it does not check existence, permissions, lengths,
|
|
170
|
+
encoding, or whether a particular filesystem accepts the path.
|
|
171
|
+
|
|
172
|
+
Custom definitions can be declared as follows:
|
|
173
|
+
|
|
174
|
+
```toml
|
|
175
|
+
[spec_types.identifier]
|
|
176
|
+
regex_pattern = '[a-zA-Z][a-zA-Z0-9]*'
|
|
177
|
+
|
|
178
|
+
[cli]
|
|
179
|
+
key = "MyApp"
|
|
180
|
+
|
|
181
|
+
[cli.search]
|
|
182
|
+
args = [
|
|
183
|
+
{key = "where", option = "--where", spec_type = "identifier"},
|
|
184
|
+
{key = "ignore", option = "--ignore", spec_type = "identifier"},
|
|
185
|
+
]
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Patterns use Python `re.fullmatch`. An omitted optional value is skipped and
|
|
189
|
+
multiple values are checked individually. Invalid patterns and duplicate names
|
|
190
|
+
across includes fail during loading/resolution; duplicates with the registry
|
|
191
|
+
fail during validation. The hook does not transform values; registered
|
|
192
|
+
validators receive the complete value when it is present.
|
|
193
|
+
|
|
194
|
+
An alias inherits an existing TOML specification while retaining its own name:
|
|
195
|
+
|
|
196
|
+
```toml
|
|
197
|
+
[spec_types.my_identifier]
|
|
198
|
+
extends = "identifier"
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
This allows a custom hook to add checks for `my_identifier` after calling
|
|
202
|
+
`default_pre_dispatch_hook`. Targets may come from includes; missing targets,
|
|
203
|
+
cycles, and combining `extends` with other fields are errors. Registry-only
|
|
204
|
+
types cannot be alias targets.
|
|
205
|
+
|
|
206
|
+
Specification types can instead declare non-empty string choices:
|
|
207
|
+
|
|
208
|
+
```toml
|
|
209
|
+
[spec_types.color]
|
|
210
|
+
choices = ["red", "green", "blue"]
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Arguments inherit these choices and may narrow them using their own `choices`.
|
|
214
|
+
`choices` and `regex_pattern` cannot be combined. Both CLI backends enforce
|
|
215
|
+
the resulting choices for explicit input even when the pre-dispatch hook is
|
|
216
|
+
disabled; the standard hook also checks present default and bound values.
|
|
217
|
+
|
|
218
|
+
Integer choices are supported with `base_type = "num"`:
|
|
219
|
+
|
|
220
|
+
```toml
|
|
221
|
+
[spec_types.one_to_three]
|
|
222
|
+
base_type = "num"
|
|
223
|
+
choices = [1, 2, 3]
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
CLI values are compared using Python's `int()` without changing the value passed
|
|
227
|
+
to the handler. Floating-point choices are not yet supported.
|
|
228
|
+
|
|
229
|
+
An integer range can be specified instead:
|
|
230
|
+
|
|
231
|
+
```toml
|
|
232
|
+
[spec_types.port_number]
|
|
233
|
+
base_type = "num"
|
|
234
|
+
min = 1
|
|
235
|
+
max = 65535
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Bounds are inclusive; either can be omitted. Ranges cannot be combined with
|
|
239
|
+
specification-level `choices` or `regex_pattern`. The pre-dispatch hook checks
|
|
240
|
+
the range, so disabling the hook also disables range validation. Floating-point
|
|
241
|
+
ranges are not yet supported.
|
|
242
|
+
|
|
243
|
+
An application can declare an existing file or directory type:
|
|
244
|
+
|
|
245
|
+
```toml
|
|
246
|
+
[spec_types.existing_file_path]
|
|
247
|
+
base_type = "posix_path"
|
|
248
|
+
exists = true
|
|
249
|
+
kind = "file" # omit for either a file or directory
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
`kind = "directory"` requires a directory. The standard pre-dispatch hook
|
|
253
|
+
checks existence, follows symbolic links, and resolves relative paths against
|
|
254
|
+
the process working directory. It does not open the path or guarantee it
|
|
255
|
+
remains available. Disabling the hook disables this check.
|
|
256
|
+
|
|
257
|
+
## 🖥 Interactive Mode (REPL)
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
cli-def repl --file your_cli.toml
|
|
261
|
+
yourcli> help
|
|
262
|
+
yourcli> command arg1 --option value
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## 🔁 Run Another CLI (CLI chaining)
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
cli-def run example.toml -- command arg1 arg2
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
- `--` separates cli-def arguments from the target CLI arguments
|
|
272
|
+
- Remaining arguments are forwarded to the next CLI
|
|
273
|
+
|
|
274
|
+
## 📚 Documentation
|
|
275
|
+
|
|
276
|
+
Full documentation and design details are available on GitHub:
|
|
277
|
+
|
|
278
|
+
https://github.com/tomesoft/cli-def
|
|
279
|
+
|
|
280
|
+
- DSL specification
|
|
281
|
+
- Architecture
|
|
282
|
+
- Examples
|
|
283
|
+
- Development notes
|
|
284
|
+
|
|
285
|
+
## 📌 Roadmap
|
|
286
|
+
|
|
287
|
+
See TODO.md on GitHub:
|
|
288
|
+
https://github.com/tomesoft/cli-def/blob/main/TODO.md
|
|
289
|
+
|
|
290
|
+
## 🤝 Contributing
|
|
291
|
+
|
|
292
|
+
Contributions are welcome!
|
|
293
|
+
|
|
294
|
+
## 📄 License
|
|
295
|
+
|
|
296
|
+
MIT License
|
|
297
|
+
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "cli-def"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.0"
|
|
4
4
|
description = "A declarative DSL for composable CLI applications with runtime and REPL"
|
|
5
5
|
readme = {file = "README.md", content-type = "text/markdown"}
|
|
6
|
-
requires-python = ">=3.
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
7
|
authors = [{name="Tomesoft"}]
|
|
8
8
|
license = {file = "LICENSE"}
|
|
9
9
|
classifiers = [
|
|
10
10
|
"Programming Language :: Python :: 3",
|
|
11
|
-
"Programming Language :: Python :: 3.10",
|
|
12
11
|
"License :: OSI Approved :: MIT License",
|
|
13
12
|
"Operating System :: OS Independent",
|
|
14
13
|
]
|
|
@@ -28,7 +27,7 @@ build-backend = "setuptools.build_meta"
|
|
|
28
27
|
where = ["src"]
|
|
29
28
|
|
|
30
29
|
[tool.setuptools.package-data]
|
|
31
|
-
"cli_def" = ["resources/*.toml", "demo/profiles/*.toml"]
|
|
30
|
+
"cli_def" = ["resources/*.toml", "resources/templates/basic/*.toml", "demo/profiles/*.toml"]
|
|
32
31
|
|
|
33
32
|
[project.optional-dependencies]
|
|
34
33
|
click = ["click>=8.0"]
|