cli-def 0.3.0__tar.gz → 0.4.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/src/cli_def.egg-info → cli_def-0.4.0}/PKG-INFO +145 -7
- {cli_def-0.3.0 → cli_def-0.4.0}/README.md +144 -6
- {cli_def-0.3.0 → cli_def-0.4.0}/pyproject.toml +1 -1
- cli_def-0.4.0/src/cli_def/__init__.py +22 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/argparse/argparse_builder.py +20 -2
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/click/click_builder.py +8 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/common/spec_type_def.py +7 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/argument_def.py +2 -2
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/command_def.py +7 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/protocols.py +3 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/argument_def.py +2 -2
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/command_def.py +15 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/argument_def.py +2 -2
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/command_def.py +15 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/spec_type.py +40 -5
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/parser/parser.py +5 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/resolver/resolver.py +7 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/validator/validator.py +18 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/dumper.py +17 -3
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/resources/cli_def.toml +31 -4
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/__init__.py +9 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/dispatcher.py +1 -1
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/runner.py +65 -0
- cli_def-0.4.0/src/cli_def/runtime/session.py +736 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/spec_type_support.py +16 -3
- cli_def-0.4.0/src/cli_def/script/handlers/sketch.py +340 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/validate.py +4 -6
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/main.py +5 -0
- {cli_def-0.3.0 → cli_def-0.4.0/src/cli_def.egg-info}/PKG-INFO +145 -7
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def.egg-info/SOURCES.txt +1 -0
- cli_def-0.3.0/src/cli_def/__init__.py +0 -12
- cli_def-0.3.0/src/cli_def/runtime/session.py +0 -295
- {cli_def-0.3.0 → cli_def-0.4.0}/LICENSE +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/setup.cfg +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/argparse/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/click/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/click/click_binder.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/backend/protocols.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/basic/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/basic/basic_types.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/builtin_spec_types.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/common/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/common/mult_def.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/abstract_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/cli_def.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/generic/executable_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/cli_def.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/executable_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/raw_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/raw/raw_protocols.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/cli_def.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/executable_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/resolved_node.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/models/resolved/resolved_protocols.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/parser/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/resolver/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/core/validator/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/dummy.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/dummy2.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/handlers.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/profiles/advanced.toml +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/profiles/beginner.toml +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/demo/profiles/beginner_test.toml +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/loader.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/utils/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/utils/pretty_renderer.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/utils/renderer.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/ops/utils/renderer_ops.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/resources/spec_types.toml +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/resources/templates/basic/cli_def.toml +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/resources/templates/basic/handlers.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/resources/templates/basic/main.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/_internal/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/_internal/scan_runner.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/context.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/event.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/handler_support.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/result.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/scanner.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/runtime/utils.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/common.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/dump.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/init.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/repl.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/run.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/scan.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/search.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/script/handlers/test.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/test_support/__init__.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/test_support/test_generator.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def/test_support/test_runner.py +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def.egg-info/dependency_links.txt +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def.egg-info/entry_points.txt +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def.egg-info/requires.txt +0 -0
- {cli_def-0.3.0 → cli_def-0.4.0}/src/cli_def.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cli-def
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: A declarative DSL for composable CLI applications with runtime and REPL
|
|
5
5
|
Author: Tomesoft
|
|
6
6
|
License: MIT License
|
|
@@ -53,12 +53,12 @@ https://cli-def.tomesoft.net
|
|
|
53
53
|
source:
|
|
54
54
|
https://github.com/tomesoft/cli-def
|
|
55
55
|
|
|
56
|
-
## What's new in 0.
|
|
56
|
+
## What's new in 0.4.0
|
|
57
57
|
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
58
|
+
- Layered command help and ANSI-colored REPL prompts
|
|
59
|
+
- Tab completion for commands, options, aliases, subcommands, and paths
|
|
60
|
+
- File suffix filters and application-defined completion providers for `spec_type`
|
|
61
|
+
- `cli-def --version` / `cli-def -V`
|
|
62
62
|
|
|
63
63
|
## What's new in 0.2.0
|
|
64
64
|
|
|
@@ -125,6 +125,82 @@ cli-def run example.toml -- echo hello world
|
|
|
125
125
|
PARAMS: {'message': ['hello', 'world']}
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
+
## Sketching a Command Definition
|
|
129
|
+
|
|
130
|
+
Use a command-line example to generate a starting TOML snippet:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
cli-def sketch -- myapp deploy artifact.tar --env prod --force
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```toml
|
|
137
|
+
[cli]
|
|
138
|
+
key = "myapp"
|
|
139
|
+
|
|
140
|
+
[cli.deploy]
|
|
141
|
+
args = [
|
|
142
|
+
{key="arg1", mult=1, type="str"},
|
|
143
|
+
{key="env", option="--env", mult=1, type="str"},
|
|
144
|
+
{key="force", option="--force", is_flag=true, default=false}
|
|
145
|
+
]
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The sketcher does not execute the example. It treats the first token as the
|
|
149
|
+
CLI name and, when present, the second non-option token as the command name.
|
|
150
|
+
An option followed by a non-option token takes a value; otherwise it is
|
|
151
|
+
treated as a flag. Repeated value options use `action = "append"`. Review the
|
|
152
|
+
inferred argument names and types before using the snippet.
|
|
153
|
+
|
|
154
|
+
Brace notation in a value is translated to `choices`:
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
cli-def sketch -- myapp show --level '{high,mid,low}'
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```toml
|
|
161
|
+
{key="level", option="--level", mult=1, type="str", choices=["high", "mid", "low"]}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Quote the brace expression in a shell to prevent shell brace expansion. An
|
|
165
|
+
all-integer set produces integer choices.
|
|
166
|
+
|
|
167
|
+
Square brackets mark an argument as optional:
|
|
168
|
+
|
|
169
|
+
```text
|
|
170
|
+
sketch> tool show [--level {1,2,3}]
|
|
171
|
+
{key="level", option="--level", mult="?", type="int", choices=[1, 2, 3]}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Uppercase identifiers are placeholders for positional arguments:
|
|
175
|
+
|
|
176
|
+
```text
|
|
177
|
+
sketch> tool copy FILE...
|
|
178
|
+
{key="file", mult="+", type="str"}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The supported multiplicity suffixes are `FILE?`, `FILE*`, `FILE+`, and
|
|
182
|
+
`FILE...` (`...` is equivalent to `+`). Placeholders also describe option
|
|
183
|
+
values, for example `--output PATH` and `--tag TAG*`.
|
|
184
|
+
|
|
185
|
+
Use `cli-def sketch --help` for sketch command help. To sketch an example that
|
|
186
|
+
contains `--help`, place it after the `--` separator.
|
|
187
|
+
|
|
188
|
+
For repeated sketches, start interactive mode with `--interactive` or `-I`:
|
|
189
|
+
|
|
190
|
+
```text
|
|
191
|
+
$ cli-def sketch -I
|
|
192
|
+
sketch> myapp deploy artifact.tar --env prod --force
|
|
193
|
+
[cli]
|
|
194
|
+
key = "myapp"
|
|
195
|
+
...
|
|
196
|
+
sketch> myapp status --json
|
|
197
|
+
...
|
|
198
|
+
sketch> exit
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Each line is parsed, translated to TOML, and validated before it is printed.
|
|
202
|
+
An empty line is ignored; `exit`, `quit`, or EOF ends the session.
|
|
203
|
+
|
|
128
204
|
## 🚀 Quick Start (Demo)
|
|
129
205
|
|
|
130
206
|
```bash
|
|
@@ -290,24 +366,86 @@ An application can declare an existing file or directory type:
|
|
|
290
366
|
base_type = "posix_path"
|
|
291
367
|
exists = true
|
|
292
368
|
kind = "file" # omit for either a file or directory
|
|
369
|
+
suffixes = [".toml"] # optional; requires kind = "file"
|
|
293
370
|
```
|
|
294
371
|
|
|
295
372
|
`kind = "directory"` requires a directory. The standard pre-dispatch hook
|
|
296
373
|
checks existence, follows symbolic links, and resolves relative paths against
|
|
297
374
|
the process working directory. It does not open the path or guarantee it
|
|
298
|
-
remains available.
|
|
375
|
+
remains available. `suffixes` restricts files by case-sensitive suffix and is
|
|
376
|
+
also used by REPL path completion. Disabling the hook disables these checks.
|
|
299
377
|
|
|
300
378
|
## 🖥 Interactive Mode (REPL)
|
|
301
379
|
|
|
380
|
+
Install the `repl` extra to use the interactive experience with Tab completion
|
|
381
|
+
and colored prompts.
|
|
382
|
+
|
|
302
383
|
```bash
|
|
384
|
+
pip install "cli-def[repl]"
|
|
303
385
|
cli-def repl --file your_cli.toml
|
|
304
386
|
yourcli> help
|
|
305
387
|
yourcli> command arg1 --option value
|
|
306
388
|
```
|
|
307
389
|
|
|
390
|
+
### Embedding the REPL in an application
|
|
391
|
+
|
|
392
|
+
Load the TOML definition and call `CliSession.repl()` to launch the REPL from
|
|
393
|
+
your application. `fallback_handler` is used when no command-specific handler
|
|
394
|
+
is registered.
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
from cli_def.ops import load_cli_def_path
|
|
398
|
+
from cli_def.runtime import CliHandlerResult, CliSession
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
def dispatch(event):
|
|
402
|
+
value = event.params.get("value")
|
|
403
|
+
print(value)
|
|
404
|
+
return CliHandlerResult.make_result(event, "value", data=value)
|
|
405
|
+
|
|
406
|
+
|
|
407
|
+
cli_def = load_cli_def_path("your_cli.toml")
|
|
408
|
+
if cli_def is None:
|
|
409
|
+
raise SystemExit("Could not load your_cli.toml")
|
|
410
|
+
|
|
411
|
+
session = CliSession(
|
|
412
|
+
cli_def,
|
|
413
|
+
fallback_handler=dispatch,
|
|
414
|
+
cli_def_file="your_cli.toml",
|
|
415
|
+
)
|
|
416
|
+
session.repl(prompt="yourcli> ")
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
When `cli_def_file` is provided, the REPL's `reload` command reloads that file.
|
|
420
|
+
Results are kept in `session.result_store` and can be inspected from the safe
|
|
421
|
+
evaluation mode entered with `>`.
|
|
422
|
+
|
|
423
|
+
Applications can register completion providers for custom specification types,
|
|
424
|
+
either while creating the session or afterward:
|
|
425
|
+
|
|
426
|
+
```python
|
|
427
|
+
session.register_spec_type_completer(
|
|
428
|
+
"search_kind",
|
|
429
|
+
lambda context: [
|
|
430
|
+
("source", "Application source"),
|
|
431
|
+
("tests", "Test files"),
|
|
432
|
+
],
|
|
433
|
+
)
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Providers can also be passed while constructing the session with
|
|
437
|
+
`spec_type_completers={"search_kind": complete_search_kind}`.
|
|
438
|
+
|
|
439
|
+
A provider yields strings or `(value, description)` tuples. Its `context`
|
|
440
|
+
exposes `spec_type`, `argument`, `executable`, `prefix`, and
|
|
441
|
+
`completed_tokens`. Aliases created with `extends` are registered by their
|
|
442
|
+
resolved specification type name. Specification types based on `posix_path`
|
|
443
|
+
use the built-in file and directory completer.
|
|
444
|
+
|
|
308
445
|
## 🔁 Run Another CLI (CLI chaining)
|
|
309
446
|
|
|
310
447
|
```bash
|
|
448
|
+
cli-def --version
|
|
311
449
|
cli-def run example.toml -- command arg1 arg2
|
|
312
450
|
```
|
|
313
451
|
|
|
@@ -10,12 +10,12 @@ https://cli-def.tomesoft.net
|
|
|
10
10
|
source:
|
|
11
11
|
https://github.com/tomesoft/cli-def
|
|
12
12
|
|
|
13
|
-
## What's new in 0.
|
|
13
|
+
## What's new in 0.4.0
|
|
14
14
|
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
15
|
+
- Layered command help and ANSI-colored REPL prompts
|
|
16
|
+
- Tab completion for commands, options, aliases, subcommands, and paths
|
|
17
|
+
- File suffix filters and application-defined completion providers for `spec_type`
|
|
18
|
+
- `cli-def --version` / `cli-def -V`
|
|
19
19
|
|
|
20
20
|
## What's new in 0.2.0
|
|
21
21
|
|
|
@@ -82,6 +82,82 @@ cli-def run example.toml -- echo hello world
|
|
|
82
82
|
PARAMS: {'message': ['hello', 'world']}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
## Sketching a Command Definition
|
|
86
|
+
|
|
87
|
+
Use a command-line example to generate a starting TOML snippet:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
cli-def sketch -- myapp deploy artifact.tar --env prod --force
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```toml
|
|
94
|
+
[cli]
|
|
95
|
+
key = "myapp"
|
|
96
|
+
|
|
97
|
+
[cli.deploy]
|
|
98
|
+
args = [
|
|
99
|
+
{key="arg1", mult=1, type="str"},
|
|
100
|
+
{key="env", option="--env", mult=1, type="str"},
|
|
101
|
+
{key="force", option="--force", is_flag=true, default=false}
|
|
102
|
+
]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The sketcher does not execute the example. It treats the first token as the
|
|
106
|
+
CLI name and, when present, the second non-option token as the command name.
|
|
107
|
+
An option followed by a non-option token takes a value; otherwise it is
|
|
108
|
+
treated as a flag. Repeated value options use `action = "append"`. Review the
|
|
109
|
+
inferred argument names and types before using the snippet.
|
|
110
|
+
|
|
111
|
+
Brace notation in a value is translated to `choices`:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
cli-def sketch -- myapp show --level '{high,mid,low}'
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
```toml
|
|
118
|
+
{key="level", option="--level", mult=1, type="str", choices=["high", "mid", "low"]}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Quote the brace expression in a shell to prevent shell brace expansion. An
|
|
122
|
+
all-integer set produces integer choices.
|
|
123
|
+
|
|
124
|
+
Square brackets mark an argument as optional:
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
sketch> tool show [--level {1,2,3}]
|
|
128
|
+
{key="level", option="--level", mult="?", type="int", choices=[1, 2, 3]}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Uppercase identifiers are placeholders for positional arguments:
|
|
132
|
+
|
|
133
|
+
```text
|
|
134
|
+
sketch> tool copy FILE...
|
|
135
|
+
{key="file", mult="+", type="str"}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The supported multiplicity suffixes are `FILE?`, `FILE*`, `FILE+`, and
|
|
139
|
+
`FILE...` (`...` is equivalent to `+`). Placeholders also describe option
|
|
140
|
+
values, for example `--output PATH` and `--tag TAG*`.
|
|
141
|
+
|
|
142
|
+
Use `cli-def sketch --help` for sketch command help. To sketch an example that
|
|
143
|
+
contains `--help`, place it after the `--` separator.
|
|
144
|
+
|
|
145
|
+
For repeated sketches, start interactive mode with `--interactive` or `-I`:
|
|
146
|
+
|
|
147
|
+
```text
|
|
148
|
+
$ cli-def sketch -I
|
|
149
|
+
sketch> myapp deploy artifact.tar --env prod --force
|
|
150
|
+
[cli]
|
|
151
|
+
key = "myapp"
|
|
152
|
+
...
|
|
153
|
+
sketch> myapp status --json
|
|
154
|
+
...
|
|
155
|
+
sketch> exit
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Each line is parsed, translated to TOML, and validated before it is printed.
|
|
159
|
+
An empty line is ignored; `exit`, `quit`, or EOF ends the session.
|
|
160
|
+
|
|
85
161
|
## 🚀 Quick Start (Demo)
|
|
86
162
|
|
|
87
163
|
```bash
|
|
@@ -247,24 +323,86 @@ An application can declare an existing file or directory type:
|
|
|
247
323
|
base_type = "posix_path"
|
|
248
324
|
exists = true
|
|
249
325
|
kind = "file" # omit for either a file or directory
|
|
326
|
+
suffixes = [".toml"] # optional; requires kind = "file"
|
|
250
327
|
```
|
|
251
328
|
|
|
252
329
|
`kind = "directory"` requires a directory. The standard pre-dispatch hook
|
|
253
330
|
checks existence, follows symbolic links, and resolves relative paths against
|
|
254
331
|
the process working directory. It does not open the path or guarantee it
|
|
255
|
-
remains available.
|
|
332
|
+
remains available. `suffixes` restricts files by case-sensitive suffix and is
|
|
333
|
+
also used by REPL path completion. Disabling the hook disables these checks.
|
|
256
334
|
|
|
257
335
|
## 🖥 Interactive Mode (REPL)
|
|
258
336
|
|
|
337
|
+
Install the `repl` extra to use the interactive experience with Tab completion
|
|
338
|
+
and colored prompts.
|
|
339
|
+
|
|
259
340
|
```bash
|
|
341
|
+
pip install "cli-def[repl]"
|
|
260
342
|
cli-def repl --file your_cli.toml
|
|
261
343
|
yourcli> help
|
|
262
344
|
yourcli> command arg1 --option value
|
|
263
345
|
```
|
|
264
346
|
|
|
347
|
+
### Embedding the REPL in an application
|
|
348
|
+
|
|
349
|
+
Load the TOML definition and call `CliSession.repl()` to launch the REPL from
|
|
350
|
+
your application. `fallback_handler` is used when no command-specific handler
|
|
351
|
+
is registered.
|
|
352
|
+
|
|
353
|
+
```python
|
|
354
|
+
from cli_def.ops import load_cli_def_path
|
|
355
|
+
from cli_def.runtime import CliHandlerResult, CliSession
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def dispatch(event):
|
|
359
|
+
value = event.params.get("value")
|
|
360
|
+
print(value)
|
|
361
|
+
return CliHandlerResult.make_result(event, "value", data=value)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
cli_def = load_cli_def_path("your_cli.toml")
|
|
365
|
+
if cli_def is None:
|
|
366
|
+
raise SystemExit("Could not load your_cli.toml")
|
|
367
|
+
|
|
368
|
+
session = CliSession(
|
|
369
|
+
cli_def,
|
|
370
|
+
fallback_handler=dispatch,
|
|
371
|
+
cli_def_file="your_cli.toml",
|
|
372
|
+
)
|
|
373
|
+
session.repl(prompt="yourcli> ")
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
When `cli_def_file` is provided, the REPL's `reload` command reloads that file.
|
|
377
|
+
Results are kept in `session.result_store` and can be inspected from the safe
|
|
378
|
+
evaluation mode entered with `>`.
|
|
379
|
+
|
|
380
|
+
Applications can register completion providers for custom specification types,
|
|
381
|
+
either while creating the session or afterward:
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
session.register_spec_type_completer(
|
|
385
|
+
"search_kind",
|
|
386
|
+
lambda context: [
|
|
387
|
+
("source", "Application source"),
|
|
388
|
+
("tests", "Test files"),
|
|
389
|
+
],
|
|
390
|
+
)
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Providers can also be passed while constructing the session with
|
|
394
|
+
`spec_type_completers={"search_kind": complete_search_kind}`.
|
|
395
|
+
|
|
396
|
+
A provider yields strings or `(value, description)` tuples. Its `context`
|
|
397
|
+
exposes `spec_type`, `argument`, `executable`, `prefix`, and
|
|
398
|
+
`completed_tokens`. Aliases created with `extends` are registered by their
|
|
399
|
+
resolved specification type name. Specification types based on `posix_path`
|
|
400
|
+
use the built-in file and directory completer.
|
|
401
|
+
|
|
265
402
|
## 🔁 Run Another CLI (CLI chaining)
|
|
266
403
|
|
|
267
404
|
```bash
|
|
405
|
+
cli-def --version
|
|
268
406
|
cli-def run example.toml -- command arg1 arg2
|
|
269
407
|
```
|
|
270
408
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# cli_def/init.py
|
|
2
|
+
# package marker
|
|
3
|
+
|
|
4
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
5
|
+
|
|
6
|
+
from .core.models import (
|
|
7
|
+
CliDef,
|
|
8
|
+
)
|
|
9
|
+
from .core.parser import CliDefParser
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
__version__ = version("cli-def")
|
|
14
|
+
except PackageNotFoundError:
|
|
15
|
+
__version__ = "0+unknown"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"CliDef",
|
|
20
|
+
"CliDefParser",
|
|
21
|
+
"__version__",
|
|
22
|
+
]
|
|
@@ -124,6 +124,9 @@ class ArgparseBuilder(BuilderProtocol):
|
|
|
124
124
|
if argDef.aliases:
|
|
125
125
|
name_or_flags.extend(argDef.aliases)
|
|
126
126
|
name_or_flags.append(argDef.option)
|
|
127
|
+
option_kwargs = {}
|
|
128
|
+
if argDef.get_action():
|
|
129
|
+
option_kwargs["action"] = argDef.get_action()
|
|
127
130
|
action = parser.add_argument(
|
|
128
131
|
*name_or_flags,
|
|
129
132
|
dest=argDef.get_dest(),
|
|
@@ -131,6 +134,7 @@ class ArgparseBuilder(BuilderProtocol):
|
|
|
131
134
|
default=argDef.default,
|
|
132
135
|
nargs=self.to_nargs(argDef.mult),
|
|
133
136
|
help=argDef.help,
|
|
137
|
+
**option_kwargs,
|
|
134
138
|
)
|
|
135
139
|
|
|
136
140
|
actions.append(action)
|
|
@@ -154,11 +158,21 @@ class ArgparseBuilder(BuilderProtocol):
|
|
|
154
158
|
("sub" * firstCmdDef.parent.deflevel) + "command"
|
|
155
159
|
)
|
|
156
160
|
|
|
157
|
-
|
|
161
|
+
visible_commands = [
|
|
162
|
+
cmd.key for cmd in commandDefs if cmd.visibility == "public"
|
|
163
|
+
]
|
|
164
|
+
metavar = (
|
|
165
|
+
"{" + ",".join(visible_commands) + "}"
|
|
166
|
+
if visible_commands else "COMMAND"
|
|
167
|
+
)
|
|
168
|
+
subparsers = parser.add_subparsers(dest=group, metavar=metavar)
|
|
158
169
|
self._register_subparsers(firstCmdDef.parent, subparsers)
|
|
159
170
|
cmd_parsers = []
|
|
160
171
|
for cmdDef in commandDefs:
|
|
172
|
+
choice_action_count = len(subparsers._choices_actions)
|
|
161
173
|
cmd_parser = self.build_single_command(cmdDef, subparsers)
|
|
174
|
+
if cmdDef.visibility == "hidden":
|
|
175
|
+
del subparsers._choices_actions[choice_action_count:]
|
|
162
176
|
cmd_parsers.append(cmd_parser)
|
|
163
177
|
|
|
164
178
|
return cmd_parsers
|
|
@@ -175,7 +189,11 @@ class ArgparseBuilder(BuilderProtocol):
|
|
|
175
189
|
if subparsers is not None:
|
|
176
190
|
parser = subparsers.add_parser(
|
|
177
191
|
cmdDef.key,
|
|
178
|
-
help
|
|
192
|
+
help=(
|
|
193
|
+
cmdDef.help
|
|
194
|
+
if cmdDef.visibility == "public"
|
|
195
|
+
else argparse.SUPPRESS
|
|
196
|
+
),
|
|
179
197
|
add_help=cmdDef.definition.extra_defs.get("add_help", True),
|
|
180
198
|
parents=parent_parsers,
|
|
181
199
|
)
|
|
@@ -113,7 +113,12 @@ class ClickBuilder(BuilderProtocol):
|
|
|
113
113
|
]
|
|
114
114
|
|
|
115
115
|
if cmdDef.subcommands:
|
|
116
|
-
grp = click.Group(
|
|
116
|
+
grp = click.Group(
|
|
117
|
+
name=cmdDef.key,
|
|
118
|
+
help=cmdDef.help,
|
|
119
|
+
params=params,
|
|
120
|
+
hidden=cmdDef.visibility == "hidden",
|
|
121
|
+
)
|
|
117
122
|
|
|
118
123
|
self._register(cmdDef, grp)
|
|
119
124
|
|
|
@@ -130,6 +135,7 @@ class ClickBuilder(BuilderProtocol):
|
|
|
130
135
|
name=cmdDef.key,
|
|
131
136
|
help=cmdDef.help,
|
|
132
137
|
params=params,
|
|
138
|
+
hidden=cmdDef.visibility == "hidden",
|
|
133
139
|
callback=callback,
|
|
134
140
|
context_settings={
|
|
135
141
|
"ignore_unknown_options": True,
|
|
@@ -155,6 +161,7 @@ class ClickBuilder(BuilderProtocol):
|
|
|
155
161
|
default=arg.default,
|
|
156
162
|
is_flag=arg.is_flag or False,
|
|
157
163
|
type=choice_type,
|
|
164
|
+
multiple=arg.get_action() == "append",
|
|
158
165
|
)
|
|
159
166
|
param.name = arg.key
|
|
160
167
|
self.apply_mult(param, arg.mult)
|
|
@@ -16,6 +16,7 @@ class SpecTypeDef:
|
|
|
16
16
|
max: int | None = None
|
|
17
17
|
exists: bool | None = None
|
|
18
18
|
kind: Literal["file", "directory"] | None = None
|
|
19
|
+
suffixes: Sequence[str] | None = None
|
|
19
20
|
extends: str | None = None
|
|
20
21
|
|
|
21
22
|
def __post_init__(self) -> None:
|
|
@@ -23,6 +24,11 @@ class SpecTypeDef:
|
|
|
23
24
|
if isinstance(self.choices, (str, bytes)) or not isinstance(self.choices, Sequence):
|
|
24
25
|
raise ValueError("spec type choices must be a sequence")
|
|
25
26
|
object.__setattr__(self, "choices", tuple(self.choices))
|
|
27
|
+
if self.suffixes is not None:
|
|
28
|
+
if (isinstance(self.suffixes, (str, bytes))
|
|
29
|
+
or not isinstance(self.suffixes, Sequence)):
|
|
30
|
+
raise ValueError("spec type suffixes must be a sequence")
|
|
31
|
+
object.__setattr__(self, "suffixes", tuple(self.suffixes))
|
|
26
32
|
|
|
27
33
|
@classmethod
|
|
28
34
|
def from_reference(cls, value: str | SpecTypeDef) -> SpecTypeDef:
|
|
@@ -32,6 +38,7 @@ class SpecTypeDef:
|
|
|
32
38
|
if (value.regex_pattern is not None or value.choices is not None
|
|
33
39
|
or value.min is not None or value.max is not None
|
|
34
40
|
or value.exists is not None or value.kind is not None
|
|
41
|
+
or value.suffixes is not None
|
|
35
42
|
or value.extends is not None
|
|
36
43
|
or value.base_type not in (None, "any")):
|
|
37
44
|
raise ValueError("Argument spec_type must be a name-only reference")
|
|
@@ -141,9 +141,9 @@ class AbstractArgumentDef(
|
|
|
141
141
|
|
|
142
142
|
|
|
143
143
|
def get_action(self) -> str | None:
|
|
144
|
+
if "action" in self.extra_defs:
|
|
145
|
+
return self.extra_defs["action"]
|
|
144
146
|
if self.is_flag:
|
|
145
|
-
if "action" in self.extra_defs:
|
|
146
|
-
return self.extra_defs["action"]
|
|
147
147
|
if self.default is not None and self.default == True:
|
|
148
148
|
return "store_false"
|
|
149
149
|
return "store_true"
|
|
@@ -23,6 +23,7 @@ class AbstractCommandDef(
|
|
|
23
23
|
_KNOWN_FIELDS = frozenset(
|
|
24
24
|
AbstractExecutableNode._KNOWN_FIELDS | {
|
|
25
25
|
"aliases",
|
|
26
|
+
"visibility",
|
|
26
27
|
})
|
|
27
28
|
|
|
28
29
|
def __init__(
|
|
@@ -37,6 +38,7 @@ class AbstractCommandDef(
|
|
|
37
38
|
|
|
38
39
|
subcommands: Iterable[Self]|None = None,
|
|
39
40
|
aliases: Iterable[str]|None = None,
|
|
41
|
+
visibility: str|None = None,
|
|
40
42
|
|
|
41
43
|
extra_defs: Mapping[str, Any]|None = None,
|
|
42
44
|
):
|
|
@@ -51,11 +53,16 @@ class AbstractCommandDef(
|
|
|
51
53
|
)
|
|
52
54
|
self._subcommands: list[Self]|None = list(subcommands) if subcommands is not None else None
|
|
53
55
|
self._aliases: list[str]|None = list(aliases) if aliases is not None else None
|
|
56
|
+
self._visibility = visibility or "public"
|
|
54
57
|
|
|
55
58
|
|
|
56
59
|
@property
|
|
57
60
|
def aliases(self) -> Sequence[str]:
|
|
58
61
|
return self._aliases or []
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def visibility(self) -> str:
|
|
65
|
+
return self._visibility
|
|
59
66
|
|
|
60
67
|
@property
|
|
61
68
|
def subcommands(self) -> Sequence[Self]:
|
|
@@ -136,6 +136,9 @@ class CommandDefProtocol(ExecutableNodeProtocol[TArgDef], Protocol[TArgDef]):
|
|
|
136
136
|
def aliases(self) -> Sequence[str]:
|
|
137
137
|
...
|
|
138
138
|
@property
|
|
139
|
+
def visibility(self) -> str:
|
|
140
|
+
...
|
|
141
|
+
@property
|
|
139
142
|
def subcommands(self) -> Sequence[Self]:
|
|
140
143
|
...
|
|
141
144
|
|
|
@@ -140,9 +140,9 @@ class ArgumentDef(
|
|
|
140
140
|
|
|
141
141
|
|
|
142
142
|
def get_action(self) -> str | None:
|
|
143
|
+
if "action" in self.extra_defs:
|
|
144
|
+
return self.extra_defs["action"]
|
|
143
145
|
if self.is_flag:
|
|
144
|
-
if "action" in self.extra_defs:
|
|
145
|
-
return self.extra_defs["action"]
|
|
146
146
|
if self.default is not None and self.default == True:
|
|
147
147
|
return "store_false"
|
|
148
148
|
return "store_true"
|
|
@@ -27,6 +27,7 @@ class CommandDef(
|
|
|
27
27
|
"aliases",
|
|
28
28
|
"inherit_from",
|
|
29
29
|
"is_template",
|
|
30
|
+
"visibility",
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
|
|
@@ -45,6 +46,7 @@ class CommandDef(
|
|
|
45
46
|
is_template: bool|None = None,
|
|
46
47
|
aliases: Iterable[str]|None = None,
|
|
47
48
|
inherit_from: Iterable[str]|None = None,
|
|
49
|
+
visibility: str|None = None,
|
|
48
50
|
|
|
49
51
|
extra_defs: Mapping[str, Any]|None = None,
|
|
50
52
|
):
|
|
@@ -62,6 +64,11 @@ class CommandDef(
|
|
|
62
64
|
self._inherit_from: list[str]|None = list(inherit_from) if inherit_from is not None else None
|
|
63
65
|
self._subcommands: list[CommandDef]|None = list(subcommands) if subcommands is not None else None
|
|
64
66
|
self._aliases: list[str]|None = list(aliases) if aliases is not None else None
|
|
67
|
+
if visibility not in (None, "public", "hidden"):
|
|
68
|
+
raise ValueError(
|
|
69
|
+
f"Invalid command visibility {visibility!r}; expected 'public' or 'hidden'"
|
|
70
|
+
)
|
|
71
|
+
self._visibility = visibility
|
|
65
72
|
|
|
66
73
|
# fix is_template:
|
|
67
74
|
self._is_template: bool = (
|
|
@@ -81,6 +88,10 @@ class CommandDef(
|
|
|
81
88
|
@property
|
|
82
89
|
def aliases(self) -> Sequence[str]:
|
|
83
90
|
return self._aliases or []
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def visibility(self) -> str:
|
|
94
|
+
return self._visibility or "public"
|
|
84
95
|
|
|
85
96
|
@property
|
|
86
97
|
def subcommands(self) -> Sequence[CommandDef]:
|
|
@@ -126,6 +137,8 @@ class CommandDef(
|
|
|
126
137
|
if self.aliases is None:
|
|
127
138
|
if other.aliases is not None:
|
|
128
139
|
self._aliases = list(other.aliases)
|
|
140
|
+
if self._visibility is None:
|
|
141
|
+
self._visibility = other._visibility
|
|
129
142
|
|
|
130
143
|
|
|
131
144
|
def override_with(self, other: CommandDef):
|
|
@@ -145,6 +158,8 @@ class CommandDef(
|
|
|
145
158
|
|
|
146
159
|
if other.aliases:
|
|
147
160
|
self._aliases = list(other.aliases)
|
|
161
|
+
if other._visibility is not None:
|
|
162
|
+
self._visibility = other._visibility
|
|
148
163
|
|
|
149
164
|
|
|
150
165
|
|
|
@@ -159,9 +159,9 @@ class ResolvedArgumentDef(
|
|
|
159
159
|
|
|
160
160
|
|
|
161
161
|
def get_action(self) -> str | None:
|
|
162
|
+
if "action" in self.extra_defs:
|
|
163
|
+
return self.extra_defs["action"]
|
|
162
164
|
if self.is_flag:
|
|
163
|
-
if "action" in self.extra_defs:
|
|
164
|
-
return self.extra_defs["action"]
|
|
165
165
|
if self.default is not None and self.default == True:
|
|
166
166
|
return "store_false"
|
|
167
167
|
return "store_true"
|