cliha 1.0.0rc1__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.
- cliha-1.0.0rc1/LICENSE +21 -0
- cliha-1.0.0rc1/MANIFEST.in +5 -0
- cliha-1.0.0rc1/PKG-INFO +225 -0
- cliha-1.0.0rc1/README.md +191 -0
- cliha-1.0.0rc1/cliha/__init__.py +57 -0
- cliha-1.0.0rc1/cliha/__main__.py +212 -0
- cliha-1.0.0rc1/cliha/_markers.py +51 -0
- cliha-1.0.0rc1/cliha/_sample_app.py +22 -0
- cliha-1.0.0rc1/cliha/api.py +163 -0
- cliha-1.0.0rc1/cliha/config.py +23 -0
- cliha-1.0.0rc1/cliha/emit/__init__.py +43 -0
- cliha-1.0.0rc1/cliha/emit/_render.py +91 -0
- cliha-1.0.0rc1/cliha/emit/_tables.py +213 -0
- cliha-1.0.0rc1/cliha/emit/bash.py +66 -0
- cliha-1.0.0rc1/cliha/emit/fish.py +57 -0
- cliha-1.0.0rc1/cliha/emit/zsh.py +76 -0
- cliha-1.0.0rc1/cliha/entry_points.py +88 -0
- cliha-1.0.0rc1/cliha/extract.py +236 -0
- cliha-1.0.0rc1/cliha/loader.py +36 -0
- cliha-1.0.0rc1/cliha/model.py +251 -0
- cliha-1.0.0rc1/cliha/paths.py +22 -0
- cliha-1.0.0rc1/cliha/py.typed +1 -0
- cliha-1.0.0rc1/cliha/resolve.py +329 -0
- cliha-1.0.0rc1/cliha/shell/__init__.py +1 -0
- cliha-1.0.0rc1/cliha/shell/runtime.bash +310 -0
- cliha-1.0.0rc1/cliha/shell/runtime.fish +347 -0
- cliha-1.0.0rc1/cliha/shell/runtime.zsh +344 -0
- cliha-1.0.0rc1/cliha.egg-info/PKG-INFO +225 -0
- cliha-1.0.0rc1/cliha.egg-info/SOURCES.txt +60 -0
- cliha-1.0.0rc1/cliha.egg-info/dependency_links.txt +1 -0
- cliha-1.0.0rc1/cliha.egg-info/entry_points.txt +2 -0
- cliha-1.0.0rc1/cliha.egg-info/requires.txt +12 -0
- cliha-1.0.0rc1/cliha.egg-info/top_level.txt +1 -0
- cliha-1.0.0rc1/pyproject.toml +168 -0
- cliha-1.0.0rc1/setup.cfg +4 -0
- cliha-1.0.0rc1/spec/behaviour.md +290 -0
- cliha-1.0.0rc1/spec/model.md +149 -0
- cliha-1.0.0rc1/tests/__init__.py +0 -0
- cliha-1.0.0rc1/tests/conformance/__init__.py +1 -0
- cliha-1.0.0rc1/tests/conformance/app.py +20 -0
- cliha-1.0.0rc1/tests/conformance/cases.py +239 -0
- cliha-1.0.0rc1/tests/conformance/drivers/complete.bash +35 -0
- cliha-1.0.0rc1/tests/conformance/drivers/complete.fish +14 -0
- cliha-1.0.0rc1/tests/conformance/drivers/complete.zsh +69 -0
- cliha-1.0.0rc1/tests/conformance/drivers/resolve.bash +18 -0
- cliha-1.0.0rc1/tests/conformance/drivers/resolve.fish +17 -0
- cliha-1.0.0rc1/tests/conformance/drivers/resolve.zsh +23 -0
- cliha-1.0.0rc1/tests/conformance/fixtures.py +414 -0
- cliha-1.0.0rc1/tests/conformance/shells.py +357 -0
- cliha-1.0.0rc1/tests/conformance/support.py +97 -0
- cliha-1.0.0rc1/tests/conformance/terminal.py +116 -0
- cliha-1.0.0rc1/tests/conformance/test_layer1_click.py +247 -0
- cliha-1.0.0rc1/tests/conformance/test_layer2_shells.py +124 -0
- cliha-1.0.0rc1/tests/conformance/test_layer3_entry.py +388 -0
- cliha-1.0.0rc1/tests/conformance/test_layer3_load.py +295 -0
- cliha-1.0.0rc1/tests/conftest.py +1 -0
- cliha-1.0.0rc1/tests/test_api.py +187 -0
- cliha-1.0.0rc1/tests/test_cli.py +92 -0
- cliha-1.0.0rc1/tests/test_emit.py +233 -0
- cliha-1.0.0rc1/tests/test_entry_points.py +106 -0
- cliha-1.0.0rc1/tests/test_extract.py +293 -0
- cliha-1.0.0rc1/tests/test_model.py +147 -0
cliha-1.0.0rc1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pavel Kulyov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
cliha-1.0.0rc1/PKG-INFO
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cliha
|
|
3
|
+
Version: 1.0.0rc1
|
|
4
|
+
Summary: CLIck Hybrid Autocompletion
|
|
5
|
+
Author-email: Pavel Kulyov <kulyov.pavel@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/pkulev/cliha
|
|
8
|
+
Project-URL: documentation, https://pkulev.github.io/cliha/
|
|
9
|
+
Project-URL: repository, https://github.com/pkulev/cliha
|
|
10
|
+
Keywords: cli,click,fast,autocompletion
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: click>=8.1.7
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: flask>=3.0.0; extra == "dev"
|
|
26
|
+
Requires-Dist: mypy>=1.10.0; extra == "dev"
|
|
27
|
+
Requires-Dist: poethepoet>=0.30.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest>=8.2.0; extra == "dev"
|
|
29
|
+
Requires-Dist: ruff>=0.5.0; extra == "dev"
|
|
30
|
+
Provides-Extra: docs
|
|
31
|
+
Requires-Dist: sphinx>=7.4.0; extra == "docs"
|
|
32
|
+
Requires-Dist: sphinx-autobuild>=2024.4.16; extra == "docs"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
<p align="center">
|
|
36
|
+
<img src="https://raw.githubusercontent.com/pkulev/cliha/main/sphinx/_static/cliha-logo.svg" alt="cliha" width="293">
|
|
37
|
+
</p>
|
|
38
|
+
|
|
39
|
+
## cliha - CLIck Hybrid Autocompletion
|
|
40
|
+
|
|
41
|
+
`cliha` is a Python library and CLI utility for building hybrid shell completion for Click
|
|
42
|
+
applications.
|
|
43
|
+
|
|
44
|
+
### Why click?
|
|
45
|
+
|
|
46
|
+
I just love its design, flexibility and overall look of CLI handlers.
|
|
47
|
+
|
|
48
|
+
### What is `hybrid`?
|
|
49
|
+
|
|
50
|
+
I think there are two kinds of values we can complete:
|
|
51
|
+
- **static** - subcommands, option names, pre-computed known option choices (e.g. `--shell bash`)
|
|
52
|
+
- **dynamic** - usually option values, that can be loaded from config, DB, Internet, but sometimes
|
|
53
|
+
even subcommands or option names could be taken from some storage.
|
|
54
|
+
|
|
55
|
+
### What problem this library solves?
|
|
56
|
+
|
|
57
|
+
Default click shell completion generation is nice and clean. There's no any specific data in that
|
|
58
|
+
script, because every time user hits TAB - shell asks completion list from your application.
|
|
59
|
+
It works on small scale, but when your project gets bigger, has many subcommands, plugins and
|
|
60
|
+
requires too much stuff to import: it gets *really* slow. Every TAB python interpreter initializes,
|
|
61
|
+
import a lot of code, builds the CLI handler tree, completes the incomplete and quits.
|
|
62
|
+
|
|
63
|
+
I think it's too much movement to just complete a subcommand, `--help` flag. The script handles
|
|
64
|
+
pathes though.
|
|
65
|
+
|
|
66
|
+
### Finally, what is the solution?
|
|
67
|
+
|
|
68
|
+
It's quite straightforward, if you think about it a little: just dump everything static, bake it
|
|
69
|
+
to the completion script, move the burden of updating the completions to your users and package
|
|
70
|
+
repository mantainers!
|
|
71
|
+
|
|
72
|
+
That's it, it just dumps everything known by default as a data into the completion scripts and
|
|
73
|
+
fires up the application only for specifically marked dynamic data.
|
|
74
|
+
|
|
75
|
+
## What does it promise?
|
|
76
|
+
|
|
77
|
+
One thing, and the whole project is built around it:
|
|
78
|
+
|
|
79
|
+
> For every command line, the generated script either gives **exactly the answer Click would
|
|
80
|
+
> give**, or it **asks your application** through Click's own completion protocol.
|
|
81
|
+
> It never gives a different answer.
|
|
82
|
+
|
|
83
|
+
So a `cliha` script is a drop-in replacement for the one Click generates. Whatever it knows for
|
|
84
|
+
sure, it answers itself. Whatever it cannot know, it hands over to the application, the same way
|
|
85
|
+
Click's script does on every TAB.
|
|
86
|
+
|
|
87
|
+
Answered by the script, without starting Python:
|
|
88
|
+
- subcommand names, at any depth, with their help;
|
|
89
|
+
- option names, with their help;
|
|
90
|
+
- values of `click.Choice` options and arguments (case-insensitive ones too);
|
|
91
|
+
- file and directory names for `click.Path` and `click.File`, with the shell's own completion;
|
|
92
|
+
- "nothing to complete here", for plain values like `--name <TAB>`.
|
|
93
|
+
|
|
94
|
+
Handed over to the application:
|
|
95
|
+
- parameters with a `shell_complete` callback or a custom type;
|
|
96
|
+
- groups marked with `cliha.dynamic_commands` (plugins, lazy loading, aliases);
|
|
97
|
+
- commands the script cannot follow safely: chained groups, custom `shell_complete` on a command,
|
|
98
|
+
`token_normalize_func`, `ignore_unknown_options`, and a few more;
|
|
99
|
+
- command lines with quotes or backslashes in them.
|
|
100
|
+
|
|
101
|
+
For scale: completing `flask run --de<TAB>` costs about 90 ms through Click on my laptop, nearly
|
|
102
|
+
all of it starting Python and importing Flask. The static script answers the same question in
|
|
103
|
+
well under a millisecond.
|
|
104
|
+
|
|
105
|
+
## How do I?
|
|
106
|
+
|
|
107
|
+
Generate a script and load it the way you load any completion script:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
cliha generate --factory mypkg.cli:cli --prog-name mytool --shell bash --output mytool.bash
|
|
111
|
+
source mytool.bash
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`--factory` is `module:attribute`, where the attribute is a Click command or group, or a function
|
|
115
|
+
without arguments that returns one. `--shell` is `bash`, `zsh` or `fish`.
|
|
116
|
+
|
|
117
|
+
Where to put the script:
|
|
118
|
+
- **bash** (4.4 or newer): `source` it from `~/.bashrc`. On an older bash, such as `/bin/bash` on
|
|
119
|
+
macOS, the script does nothing.
|
|
120
|
+
- **zsh**: `source` it after `compinit`, or save it as `_mytool` in a directory of your `fpath`.
|
|
121
|
+
- **fish**: save it as `~/.config/fish/completions/mytool.fish`.
|
|
122
|
+
|
|
123
|
+
`cliha install` writes the script to `cliha`'s own data directory and prints the path
|
|
124
|
+
(`cliha path` only prints it); sourcing that file is up to you.
|
|
125
|
+
|
|
126
|
+
**Regenerate the script when the command tree changes, and when Click is upgraded.** The script
|
|
127
|
+
is a snapshot. This is the price of not starting the application: ship the script with your
|
|
128
|
+
package, or generate it at install time.
|
|
129
|
+
|
|
130
|
+
### Telling cliha what is dynamic
|
|
131
|
+
|
|
132
|
+
Most of it is found automatically. A `shell_complete` callback or a custom parameter type is
|
|
133
|
+
always asked at run time, nothing to do there.
|
|
134
|
+
|
|
135
|
+
One thing cannot be seen from outside: a group whose list of commands is not fixed. Mark it,
|
|
136
|
+
otherwise the script will trust the list it was generated with:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
import click
|
|
140
|
+
import cliha
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
@cliha.dynamic_commands
|
|
144
|
+
@click.group(cls=PluginGroup)
|
|
145
|
+
def cli() -> None: ...
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
It works on a group you do not own as well: `cliha.dynamic_commands(flask.cli.cli)`.
|
|
149
|
+
|
|
150
|
+
The program name and the executable to ask can be set on the root command, so that
|
|
151
|
+
`cliha generate` does not need `--prog-name`:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
@cliha.hybrid_completion(prog_name="mytool", fallback_command="mytool")
|
|
155
|
+
@click.group()
|
|
156
|
+
def cli() -> None: ...
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### From Python
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
import cliha
|
|
163
|
+
|
|
164
|
+
model = cliha.extract_model(cli, prog_name="mytool")
|
|
165
|
+
script = cliha.emit_completion_script(model, "zsh")
|
|
166
|
+
|
|
167
|
+
cliha.resolve(model, ["deploy", "--env"], "pr")
|
|
168
|
+
# Candidates(items=(Candidate(value='prod', help=None),))
|
|
169
|
+
cliha.explain(model, ["deploy", "--env"], "pr").rules
|
|
170
|
+
# ('R7', 'R4', 'R13', 'R20'), the rules of the spec that decided the answer
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`cliha dump --factory mypkg.cli:cli` prints the model as JSON. It is everything the script knows
|
|
174
|
+
about your application, so it is the first thing to look at when a completion surprises you.
|
|
175
|
+
|
|
176
|
+
## How it is built
|
|
177
|
+
|
|
178
|
+
`cliha` is developed from a specification:
|
|
179
|
+
|
|
180
|
+
- [`spec/behaviour.md`](spec/behaviour.md) defines the answer for any command line as a list of
|
|
181
|
+
numbered rules. [`spec/model.md`](spec/model.md) defines the data a script is generated from.
|
|
182
|
+
- `cliha/resolve.py` is the specification in Python.
|
|
183
|
+
- `cliha/shell/runtime.{bash,zsh,fish}` are the same rules written by hand once per shell.
|
|
184
|
+
A generated script is that fixed runtime plus tables of data. No shell code is generated from
|
|
185
|
+
your command names, and every function and variable carries a per-program prefix, so scripts
|
|
186
|
+
of different programs do not collide.
|
|
187
|
+
|
|
188
|
+
The tests in `tests/conformance` hold the pieces together:
|
|
189
|
+
|
|
190
|
+
1. the Python resolver is compared with Click itself on thousands of generated command lines:
|
|
191
|
+
it must give the same answer or defer;
|
|
192
|
+
2. each shell runtime is compared with the Python resolver on the same command lines;
|
|
193
|
+
3. the scripts are loaded into real shells: no output on load, nothing defined outside the
|
|
194
|
+
prefix, the application is started only when the answer is "ask the application", and
|
|
195
|
+
pressing TAB in a real terminal does what Click's own script does.
|
|
196
|
+
|
|
197
|
+
CI runs this against the oldest supported Click (8.1.7) and the latest one.
|
|
198
|
+
|
|
199
|
+
## Known limits
|
|
200
|
+
|
|
201
|
+
- Everything Click's completion cannot do, `cliha` cannot do either, on purpose. For example in
|
|
202
|
+
bash `--opt=val<TAB>` completes nothing, with Click's script and with this one.
|
|
203
|
+
- A group is treated as static unless it is marked. An unmarked plugin group will not offer
|
|
204
|
+
commands that appeared after the script was generated.
|
|
205
|
+
- bash 4.4+, zsh and fish. No PowerShell.
|
|
206
|
+
|
|
207
|
+
## Development
|
|
208
|
+
|
|
209
|
+
Create and sync an environment with uv:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
uv venv .venv
|
|
213
|
+
uv sync --all-extras
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Run checks using poe (install to your system or call via `uv poe <task>`):
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
poe ci # (this is a combination of format, lint, lint-shell, typecheck and test)
|
|
220
|
+
poe docs-preview
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
The shell tests need `bash` 4.4+, `zsh`, `fish` and `shellcheck`. Locally a test is skipped when
|
|
224
|
+
its tool is missing; with `CI` set it fails instead. A shell outside `PATH` can be given with
|
|
225
|
+
`CLIHA_TEST_BASH`, `CLIHA_TEST_ZSH` or `CLIHA_TEST_FISH`.
|
cliha-1.0.0rc1/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/pkulev/cliha/main/sphinx/_static/cliha-logo.svg" alt="cliha" width="293">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
## cliha - CLIck Hybrid Autocompletion
|
|
6
|
+
|
|
7
|
+
`cliha` is a Python library and CLI utility for building hybrid shell completion for Click
|
|
8
|
+
applications.
|
|
9
|
+
|
|
10
|
+
### Why click?
|
|
11
|
+
|
|
12
|
+
I just love its design, flexibility and overall look of CLI handlers.
|
|
13
|
+
|
|
14
|
+
### What is `hybrid`?
|
|
15
|
+
|
|
16
|
+
I think there are two kinds of values we can complete:
|
|
17
|
+
- **static** - subcommands, option names, pre-computed known option choices (e.g. `--shell bash`)
|
|
18
|
+
- **dynamic** - usually option values, that can be loaded from config, DB, Internet, but sometimes
|
|
19
|
+
even subcommands or option names could be taken from some storage.
|
|
20
|
+
|
|
21
|
+
### What problem this library solves?
|
|
22
|
+
|
|
23
|
+
Default click shell completion generation is nice and clean. There's no any specific data in that
|
|
24
|
+
script, because every time user hits TAB - shell asks completion list from your application.
|
|
25
|
+
It works on small scale, but when your project gets bigger, has many subcommands, plugins and
|
|
26
|
+
requires too much stuff to import: it gets *really* slow. Every TAB python interpreter initializes,
|
|
27
|
+
import a lot of code, builds the CLI handler tree, completes the incomplete and quits.
|
|
28
|
+
|
|
29
|
+
I think it's too much movement to just complete a subcommand, `--help` flag. The script handles
|
|
30
|
+
pathes though.
|
|
31
|
+
|
|
32
|
+
### Finally, what is the solution?
|
|
33
|
+
|
|
34
|
+
It's quite straightforward, if you think about it a little: just dump everything static, bake it
|
|
35
|
+
to the completion script, move the burden of updating the completions to your users and package
|
|
36
|
+
repository mantainers!
|
|
37
|
+
|
|
38
|
+
That's it, it just dumps everything known by default as a data into the completion scripts and
|
|
39
|
+
fires up the application only for specifically marked dynamic data.
|
|
40
|
+
|
|
41
|
+
## What does it promise?
|
|
42
|
+
|
|
43
|
+
One thing, and the whole project is built around it:
|
|
44
|
+
|
|
45
|
+
> For every command line, the generated script either gives **exactly the answer Click would
|
|
46
|
+
> give**, or it **asks your application** through Click's own completion protocol.
|
|
47
|
+
> It never gives a different answer.
|
|
48
|
+
|
|
49
|
+
So a `cliha` script is a drop-in replacement for the one Click generates. Whatever it knows for
|
|
50
|
+
sure, it answers itself. Whatever it cannot know, it hands over to the application, the same way
|
|
51
|
+
Click's script does on every TAB.
|
|
52
|
+
|
|
53
|
+
Answered by the script, without starting Python:
|
|
54
|
+
- subcommand names, at any depth, with their help;
|
|
55
|
+
- option names, with their help;
|
|
56
|
+
- values of `click.Choice` options and arguments (case-insensitive ones too);
|
|
57
|
+
- file and directory names for `click.Path` and `click.File`, with the shell's own completion;
|
|
58
|
+
- "nothing to complete here", for plain values like `--name <TAB>`.
|
|
59
|
+
|
|
60
|
+
Handed over to the application:
|
|
61
|
+
- parameters with a `shell_complete` callback or a custom type;
|
|
62
|
+
- groups marked with `cliha.dynamic_commands` (plugins, lazy loading, aliases);
|
|
63
|
+
- commands the script cannot follow safely: chained groups, custom `shell_complete` on a command,
|
|
64
|
+
`token_normalize_func`, `ignore_unknown_options`, and a few more;
|
|
65
|
+
- command lines with quotes or backslashes in them.
|
|
66
|
+
|
|
67
|
+
For scale: completing `flask run --de<TAB>` costs about 90 ms through Click on my laptop, nearly
|
|
68
|
+
all of it starting Python and importing Flask. The static script answers the same question in
|
|
69
|
+
well under a millisecond.
|
|
70
|
+
|
|
71
|
+
## How do I?
|
|
72
|
+
|
|
73
|
+
Generate a script and load it the way you load any completion script:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cliha generate --factory mypkg.cli:cli --prog-name mytool --shell bash --output mytool.bash
|
|
77
|
+
source mytool.bash
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`--factory` is `module:attribute`, where the attribute is a Click command or group, or a function
|
|
81
|
+
without arguments that returns one. `--shell` is `bash`, `zsh` or `fish`.
|
|
82
|
+
|
|
83
|
+
Where to put the script:
|
|
84
|
+
- **bash** (4.4 or newer): `source` it from `~/.bashrc`. On an older bash, such as `/bin/bash` on
|
|
85
|
+
macOS, the script does nothing.
|
|
86
|
+
- **zsh**: `source` it after `compinit`, or save it as `_mytool` in a directory of your `fpath`.
|
|
87
|
+
- **fish**: save it as `~/.config/fish/completions/mytool.fish`.
|
|
88
|
+
|
|
89
|
+
`cliha install` writes the script to `cliha`'s own data directory and prints the path
|
|
90
|
+
(`cliha path` only prints it); sourcing that file is up to you.
|
|
91
|
+
|
|
92
|
+
**Regenerate the script when the command tree changes, and when Click is upgraded.** The script
|
|
93
|
+
is a snapshot. This is the price of not starting the application: ship the script with your
|
|
94
|
+
package, or generate it at install time.
|
|
95
|
+
|
|
96
|
+
### Telling cliha what is dynamic
|
|
97
|
+
|
|
98
|
+
Most of it is found automatically. A `shell_complete` callback or a custom parameter type is
|
|
99
|
+
always asked at run time, nothing to do there.
|
|
100
|
+
|
|
101
|
+
One thing cannot be seen from outside: a group whose list of commands is not fixed. Mark it,
|
|
102
|
+
otherwise the script will trust the list it was generated with:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
import click
|
|
106
|
+
import cliha
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@cliha.dynamic_commands
|
|
110
|
+
@click.group(cls=PluginGroup)
|
|
111
|
+
def cli() -> None: ...
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
It works on a group you do not own as well: `cliha.dynamic_commands(flask.cli.cli)`.
|
|
115
|
+
|
|
116
|
+
The program name and the executable to ask can be set on the root command, so that
|
|
117
|
+
`cliha generate` does not need `--prog-name`:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
@cliha.hybrid_completion(prog_name="mytool", fallback_command="mytool")
|
|
121
|
+
@click.group()
|
|
122
|
+
def cli() -> None: ...
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### From Python
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
import cliha
|
|
129
|
+
|
|
130
|
+
model = cliha.extract_model(cli, prog_name="mytool")
|
|
131
|
+
script = cliha.emit_completion_script(model, "zsh")
|
|
132
|
+
|
|
133
|
+
cliha.resolve(model, ["deploy", "--env"], "pr")
|
|
134
|
+
# Candidates(items=(Candidate(value='prod', help=None),))
|
|
135
|
+
cliha.explain(model, ["deploy", "--env"], "pr").rules
|
|
136
|
+
# ('R7', 'R4', 'R13', 'R20'), the rules of the spec that decided the answer
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`cliha dump --factory mypkg.cli:cli` prints the model as JSON. It is everything the script knows
|
|
140
|
+
about your application, so it is the first thing to look at when a completion surprises you.
|
|
141
|
+
|
|
142
|
+
## How it is built
|
|
143
|
+
|
|
144
|
+
`cliha` is developed from a specification:
|
|
145
|
+
|
|
146
|
+
- [`spec/behaviour.md`](spec/behaviour.md) defines the answer for any command line as a list of
|
|
147
|
+
numbered rules. [`spec/model.md`](spec/model.md) defines the data a script is generated from.
|
|
148
|
+
- `cliha/resolve.py` is the specification in Python.
|
|
149
|
+
- `cliha/shell/runtime.{bash,zsh,fish}` are the same rules written by hand once per shell.
|
|
150
|
+
A generated script is that fixed runtime plus tables of data. No shell code is generated from
|
|
151
|
+
your command names, and every function and variable carries a per-program prefix, so scripts
|
|
152
|
+
of different programs do not collide.
|
|
153
|
+
|
|
154
|
+
The tests in `tests/conformance` hold the pieces together:
|
|
155
|
+
|
|
156
|
+
1. the Python resolver is compared with Click itself on thousands of generated command lines:
|
|
157
|
+
it must give the same answer or defer;
|
|
158
|
+
2. each shell runtime is compared with the Python resolver on the same command lines;
|
|
159
|
+
3. the scripts are loaded into real shells: no output on load, nothing defined outside the
|
|
160
|
+
prefix, the application is started only when the answer is "ask the application", and
|
|
161
|
+
pressing TAB in a real terminal does what Click's own script does.
|
|
162
|
+
|
|
163
|
+
CI runs this against the oldest supported Click (8.1.7) and the latest one.
|
|
164
|
+
|
|
165
|
+
## Known limits
|
|
166
|
+
|
|
167
|
+
- Everything Click's completion cannot do, `cliha` cannot do either, on purpose. For example in
|
|
168
|
+
bash `--opt=val<TAB>` completes nothing, with Click's script and with this one.
|
|
169
|
+
- A group is treated as static unless it is marked. An unmarked plugin group will not offer
|
|
170
|
+
commands that appeared after the script was generated.
|
|
171
|
+
- bash 4.4+, zsh and fish. No PowerShell.
|
|
172
|
+
|
|
173
|
+
## Development
|
|
174
|
+
|
|
175
|
+
Create and sync an environment with uv:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
uv venv .venv
|
|
179
|
+
uv sync --all-extras
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Run checks using poe (install to your system or call via `uv poe <task>`):
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
poe ci # (this is a combination of format, lint, lint-shell, typecheck and test)
|
|
186
|
+
poe docs-preview
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
The shell tests need `bash` 4.4+, `zsh`, `fish` and `shellcheck`. Locally a test is skipped when
|
|
190
|
+
its tool is missing; with `CI` set it fails instead. A shell outside `PATH` can be given with
|
|
191
|
+
`CLIHA_TEST_BASH`, `CLIHA_TEST_ZSH` or `CLIHA_TEST_FISH`.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Public package interface for cliha."""
|
|
2
|
+
|
|
3
|
+
from cliha.api import (
|
|
4
|
+
dynamic_commands,
|
|
5
|
+
dynamic_completion,
|
|
6
|
+
get_parameter_shell_complete,
|
|
7
|
+
hybrid_completion,
|
|
8
|
+
is_dynamic_completion,
|
|
9
|
+
is_dynamic_parameter,
|
|
10
|
+
resolve_hybrid_config,
|
|
11
|
+
)
|
|
12
|
+
from cliha.config import DynamicCompletionConfig, HybridConfig
|
|
13
|
+
from cliha.emit import emit_completion_script
|
|
14
|
+
from cliha.extract import extract_model
|
|
15
|
+
from cliha.model import CommandNode, CompletionModel, ModelError, Param, ValueSource
|
|
16
|
+
from cliha.resolve import (
|
|
17
|
+
Answer,
|
|
18
|
+
Candidate,
|
|
19
|
+
Candidates,
|
|
20
|
+
Defer,
|
|
21
|
+
Dirs,
|
|
22
|
+
Files,
|
|
23
|
+
Nothing,
|
|
24
|
+
Resolution,
|
|
25
|
+
explain,
|
|
26
|
+
resolve,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"Answer",
|
|
32
|
+
"Candidate",
|
|
33
|
+
"Candidates",
|
|
34
|
+
"CommandNode",
|
|
35
|
+
"CompletionModel",
|
|
36
|
+
"Defer",
|
|
37
|
+
"Dirs",
|
|
38
|
+
"DynamicCompletionConfig",
|
|
39
|
+
"Files",
|
|
40
|
+
"HybridConfig",
|
|
41
|
+
"ModelError",
|
|
42
|
+
"Nothing",
|
|
43
|
+
"Param",
|
|
44
|
+
"Resolution",
|
|
45
|
+
"ValueSource",
|
|
46
|
+
"dynamic_commands",
|
|
47
|
+
"dynamic_completion",
|
|
48
|
+
"emit_completion_script",
|
|
49
|
+
"explain",
|
|
50
|
+
"extract_model",
|
|
51
|
+
"get_parameter_shell_complete",
|
|
52
|
+
"hybrid_completion",
|
|
53
|
+
"is_dynamic_completion",
|
|
54
|
+
"is_dynamic_parameter",
|
|
55
|
+
"resolve",
|
|
56
|
+
"resolve_hybrid_config",
|
|
57
|
+
]
|