cliha 1.0.0rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cliha/__init__.py +57 -0
- cliha/__main__.py +212 -0
- cliha/_markers.py +51 -0
- cliha/_sample_app.py +22 -0
- cliha/api.py +163 -0
- cliha/config.py +23 -0
- cliha/emit/__init__.py +43 -0
- cliha/emit/_render.py +91 -0
- cliha/emit/_tables.py +213 -0
- cliha/emit/bash.py +66 -0
- cliha/emit/fish.py +57 -0
- cliha/emit/zsh.py +76 -0
- cliha/entry_points.py +88 -0
- cliha/extract.py +236 -0
- cliha/loader.py +36 -0
- cliha/model.py +251 -0
- cliha/paths.py +22 -0
- cliha/py.typed +1 -0
- cliha/resolve.py +329 -0
- cliha/shell/__init__.py +1 -0
- cliha/shell/runtime.bash +310 -0
- cliha/shell/runtime.fish +347 -0
- cliha/shell/runtime.zsh +344 -0
- cliha-1.0.0rc1.dist-info/METADATA +225 -0
- cliha-1.0.0rc1.dist-info/RECORD +29 -0
- cliha-1.0.0rc1.dist-info/WHEEL +5 -0
- cliha-1.0.0rc1.dist-info/entry_points.txt +2 -0
- cliha-1.0.0rc1.dist-info/licenses/LICENSE +21 -0
- cliha-1.0.0rc1.dist-info/top_level.txt +1 -0
cliha/emit/_tables.py
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"""Flatten a completion model into the lookup tables the shell runtimes read.
|
|
2
|
+
|
|
3
|
+
The tables are the same for every shell; each emitter only spells them in its
|
|
4
|
+
own syntax. All indexes are 1-based, and ``*_start`` tables carry one extra
|
|
5
|
+
entry so that the slice of row ``n`` is ``start[n] .. start[n + 1] - 1``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from cliha.model import CompletionModel
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
PLACEHOLDER = "_cliha_PROG"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class Tables:
|
|
20
|
+
"""Everything a runtime needs to know about one program."""
|
|
21
|
+
|
|
22
|
+
prefix: str
|
|
23
|
+
prog_name: str
|
|
24
|
+
complete_var: str
|
|
25
|
+
fallback_command: str
|
|
26
|
+
|
|
27
|
+
#: Per node: ``g`` group, ``d`` dynamic commands, ``x`` opaque.
|
|
28
|
+
node_flags: tuple[str, ...]
|
|
29
|
+
#: Per node: the largest ``nargs`` among its options that take values.
|
|
30
|
+
node_maxn: tuple[int, ...]
|
|
31
|
+
|
|
32
|
+
#: ``"<node> <name>"`` to the id of that subcommand (hidden ones too).
|
|
33
|
+
sub_keys: tuple[str, ...]
|
|
34
|
+
sub_ids: tuple[int, ...]
|
|
35
|
+
|
|
36
|
+
#: ``"<node> <option name>"`` to arity (``f``, ``v``, ``i``), param id, nargs.
|
|
37
|
+
opt_keys: tuple[str, ...]
|
|
38
|
+
opt_arity: tuple[str, ...]
|
|
39
|
+
opt_param: tuple[int, ...]
|
|
40
|
+
opt_nargs: tuple[int, ...]
|
|
41
|
+
|
|
42
|
+
#: Visible subcommands of each node, in listing order.
|
|
43
|
+
cmd_start: tuple[int, ...]
|
|
44
|
+
cmd_names: tuple[str, ...]
|
|
45
|
+
cmd_helps: tuple[str, ...]
|
|
46
|
+
|
|
47
|
+
#: Visible option names of each node, in listing order. The param id is 0
|
|
48
|
+
#: for repeatable options, which are offered even after they were seen.
|
|
49
|
+
optl_start: tuple[int, ...]
|
|
50
|
+
optl_names: tuple[str, ...]
|
|
51
|
+
optl_helps: tuple[str, ...]
|
|
52
|
+
optl_params: tuple[int, ...]
|
|
53
|
+
|
|
54
|
+
#: Arguments of each node, in declaration order.
|
|
55
|
+
arg_start: tuple[int, ...]
|
|
56
|
+
arg_params: tuple[int, ...]
|
|
57
|
+
arg_nargs: tuple[int, ...]
|
|
58
|
+
|
|
59
|
+
#: Per param: ``n`` nothing, ``f`` files, ``d`` dirs, ``x`` defer,
|
|
60
|
+
#: ``c`` choices, ``ci`` case-insensitive choices.
|
|
61
|
+
param_flags: tuple[str, ...]
|
|
62
|
+
choice_start: tuple[int, ...]
|
|
63
|
+
choice_values: tuple[str, ...]
|
|
64
|
+
#: What a choice is matched by: itself, or its lowercase form.
|
|
65
|
+
choice_keys: tuple[str, ...]
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def opt_codes(self) -> tuple[str, ...]:
|
|
69
|
+
"""Option entries packed into one word: ``f<p>``, ``v<p>:<nargs>``, ``i``."""
|
|
70
|
+
|
|
71
|
+
codes: list[str] = []
|
|
72
|
+
for arity, param, nargs in zip(self.opt_arity, self.opt_param, self.opt_nargs, strict=True):
|
|
73
|
+
if arity == "f":
|
|
74
|
+
codes.append(f"f{param}")
|
|
75
|
+
elif arity == "v":
|
|
76
|
+
codes.append(f"v{param}:{nargs}")
|
|
77
|
+
else:
|
|
78
|
+
codes.append("i")
|
|
79
|
+
return tuple(codes)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
_VALUE_FLAGS = {"none": "n", "files": "f", "dirs": "d", "defer": "x"}
|
|
83
|
+
_ARITY_CODES = {"flag": "f", "value": "v", "irregular": "i"}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def function_prefix(prog_name: str) -> str:
|
|
87
|
+
"""Return the prefix of every shell name that belongs to *prog_name*.
|
|
88
|
+
|
|
89
|
+
The mapping is injective, so two programs never share a name.
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
parts: list[str] = []
|
|
93
|
+
for char in prog_name:
|
|
94
|
+
if char.isascii() and char.isalnum():
|
|
95
|
+
parts.append(char)
|
|
96
|
+
elif char == "_":
|
|
97
|
+
parts.append("__")
|
|
98
|
+
elif ord(char) < 0x100:
|
|
99
|
+
parts.append(f"_{ord(char):02x}")
|
|
100
|
+
else:
|
|
101
|
+
parts.append(f"_u{ord(char):06x}")
|
|
102
|
+
return "_cliha_" + "".join(parts)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def one_line(text: str | None) -> str:
|
|
106
|
+
"""Collapse help text to a single line."""
|
|
107
|
+
|
|
108
|
+
return " ".join((text or "").split())
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def build_tables(model: CompletionModel) -> Tables:
|
|
112
|
+
node_flags: list[str] = []
|
|
113
|
+
node_maxn: list[int] = []
|
|
114
|
+
sub: dict[str, int] = {}
|
|
115
|
+
opt: dict[str, tuple[str, int, int]] = {}
|
|
116
|
+
cmd_start: list[int] = []
|
|
117
|
+
cmd_names: list[str] = []
|
|
118
|
+
cmd_helps: list[str] = []
|
|
119
|
+
optl_start: list[int] = []
|
|
120
|
+
optl_names: list[str] = []
|
|
121
|
+
optl_helps: list[str] = []
|
|
122
|
+
optl_params: list[int] = []
|
|
123
|
+
arg_start: list[int] = []
|
|
124
|
+
arg_params: list[int] = []
|
|
125
|
+
arg_nargs: list[int] = []
|
|
126
|
+
param_flags: list[str] = []
|
|
127
|
+
choice_start: list[int] = []
|
|
128
|
+
choice_values: list[str] = []
|
|
129
|
+
choice_keys: list[str] = []
|
|
130
|
+
|
|
131
|
+
for expected_id, node in enumerate(model.nodes(), start=1):
|
|
132
|
+
if node.id != expected_id:
|
|
133
|
+
raise ValueError(f"Node ids must be consecutive, got {node.id} at {expected_id}")
|
|
134
|
+
|
|
135
|
+
flags = ""
|
|
136
|
+
if node.group:
|
|
137
|
+
flags += "g"
|
|
138
|
+
if node.dynamic_commands:
|
|
139
|
+
flags += "d"
|
|
140
|
+
if node.opaque is not None:
|
|
141
|
+
flags += "x"
|
|
142
|
+
node_flags.append(flags)
|
|
143
|
+
node_maxn.append(
|
|
144
|
+
max((param.nargs for param in node.options if param.arity != "flag"), default=0)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
cmd_start.append(len(cmd_names) + 1)
|
|
148
|
+
for child in node.commands:
|
|
149
|
+
sub[f"{node.id} {child.name}"] = child.id
|
|
150
|
+
if not child.hidden:
|
|
151
|
+
cmd_names.append(child.name)
|
|
152
|
+
cmd_helps.append(one_line(child.help))
|
|
153
|
+
|
|
154
|
+
optl_start.append(len(optl_names) + 1)
|
|
155
|
+
arg_start.append(len(arg_params) + 1)
|
|
156
|
+
for param in node.params:
|
|
157
|
+
param_id = len(param_flags) + 1
|
|
158
|
+
source = param.value
|
|
159
|
+
if source.kind == "choices":
|
|
160
|
+
param_flags.append("c" if source.case_sensitive else "ci")
|
|
161
|
+
else:
|
|
162
|
+
param_flags.append(_VALUE_FLAGS[source.kind])
|
|
163
|
+
choice_start.append(len(choice_values) + 1)
|
|
164
|
+
for choice in source.choices:
|
|
165
|
+
choice_values.append(choice)
|
|
166
|
+
choice_keys.append(choice if source.case_sensitive else choice.lower())
|
|
167
|
+
|
|
168
|
+
if param.kind == "argument":
|
|
169
|
+
arg_params.append(param_id)
|
|
170
|
+
arg_nargs.append(param.nargs)
|
|
171
|
+
continue
|
|
172
|
+
|
|
173
|
+
for name in param.names:
|
|
174
|
+
# Click keeps the last option registered under a name.
|
|
175
|
+
opt[f"{node.id} {name}"] = (_ARITY_CODES[param.arity], param_id, param.nargs)
|
|
176
|
+
if not param.hidden:
|
|
177
|
+
optl_names.append(name)
|
|
178
|
+
optl_helps.append(one_line(param.help))
|
|
179
|
+
optl_params.append(0 if param.multiple else param_id)
|
|
180
|
+
|
|
181
|
+
cmd_start.append(len(cmd_names) + 1)
|
|
182
|
+
optl_start.append(len(optl_names) + 1)
|
|
183
|
+
arg_start.append(len(arg_params) + 1)
|
|
184
|
+
choice_start.append(len(choice_values) + 1)
|
|
185
|
+
|
|
186
|
+
return Tables(
|
|
187
|
+
prefix=function_prefix(model.prog_name),
|
|
188
|
+
prog_name=model.prog_name,
|
|
189
|
+
complete_var=model.complete_var,
|
|
190
|
+
fallback_command=model.fallback_command or "",
|
|
191
|
+
node_flags=tuple(node_flags),
|
|
192
|
+
node_maxn=tuple(node_maxn),
|
|
193
|
+
sub_keys=tuple(sub),
|
|
194
|
+
sub_ids=tuple(sub.values()),
|
|
195
|
+
opt_keys=tuple(opt),
|
|
196
|
+
opt_arity=tuple(entry[0] for entry in opt.values()),
|
|
197
|
+
opt_param=tuple(entry[1] for entry in opt.values()),
|
|
198
|
+
opt_nargs=tuple(entry[2] for entry in opt.values()),
|
|
199
|
+
cmd_start=tuple(cmd_start),
|
|
200
|
+
cmd_names=tuple(cmd_names),
|
|
201
|
+
cmd_helps=tuple(cmd_helps),
|
|
202
|
+
optl_start=tuple(optl_start),
|
|
203
|
+
optl_names=tuple(optl_names),
|
|
204
|
+
optl_helps=tuple(optl_helps),
|
|
205
|
+
optl_params=tuple(optl_params),
|
|
206
|
+
arg_start=tuple(arg_start),
|
|
207
|
+
arg_params=tuple(arg_params),
|
|
208
|
+
arg_nargs=tuple(arg_nargs),
|
|
209
|
+
param_flags=tuple(param_flags),
|
|
210
|
+
choice_start=tuple(choice_start),
|
|
211
|
+
choice_values=tuple(choice_values),
|
|
212
|
+
choice_keys=tuple(choice_keys),
|
|
213
|
+
)
|
cliha/emit/bash.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Write the bash completion script: tables, runtime, registration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
from cliha.emit._render import header, load_runtime, numbers, sh_quote, wrap
|
|
8
|
+
from cliha.emit._tables import Tables
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
_VERSION_GUARD = """\
|
|
12
|
+
if [[ -z ${BASH_VERSINFO-} ]] ||
|
|
13
|
+
((BASH_VERSINFO[0] < 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 4))); then
|
|
14
|
+
return 0
|
|
15
|
+
fi"""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def emit_bash(tables: Tables) -> str:
|
|
19
|
+
prefix = tables.prefix
|
|
20
|
+
|
|
21
|
+
def scalar(name: str, value: str) -> list[str]:
|
|
22
|
+
return [f"{prefix}_{name}={sh_quote(value)}"]
|
|
23
|
+
|
|
24
|
+
def array(name: str, values: Sequence[str]) -> list[str]:
|
|
25
|
+
# Index 0 is padding: every table is 1-based.
|
|
26
|
+
return wrap(f"declare -ga {prefix}_{name}=(", ["''", *values], ")", tight=True)
|
|
27
|
+
|
|
28
|
+
def mapping(name: str, keys: Sequence[str], values: Sequence[str]) -> list[str]:
|
|
29
|
+
pairs = [f"[{sh_quote(key)}]={value}" for key, value in zip(keys, values, strict=True)]
|
|
30
|
+
return wrap(f"declare -gA {prefix}_{name}=(", pairs, ")", tight=True)
|
|
31
|
+
|
|
32
|
+
def strings(values: Sequence[str]) -> list[str]:
|
|
33
|
+
return [sh_quote(value) for value in values]
|
|
34
|
+
|
|
35
|
+
lines = [
|
|
36
|
+
*header(tables.prog_name, "bash"),
|
|
37
|
+
"# shellcheck shell=bash",
|
|
38
|
+
"",
|
|
39
|
+
"# The script needs bash 4.4; older shells keep their default completion.",
|
|
40
|
+
_VERSION_GUARD,
|
|
41
|
+
"",
|
|
42
|
+
*scalar("name", tables.prog_name),
|
|
43
|
+
*scalar("var", tables.complete_var),
|
|
44
|
+
*scalar("cmd", tables.fallback_command),
|
|
45
|
+
*array("nf", strings(tables.node_flags)),
|
|
46
|
+
*array("nm", numbers(tables.node_maxn)),
|
|
47
|
+
*mapping("sub", tables.sub_keys, numbers(tables.sub_ids)),
|
|
48
|
+
*mapping("opt", tables.opt_keys, tables.opt_codes),
|
|
49
|
+
*array("cs", numbers(tables.cmd_start)),
|
|
50
|
+
*array("cn", strings(tables.cmd_names)),
|
|
51
|
+
*array("os", numbers(tables.optl_start)),
|
|
52
|
+
*array("on", strings(tables.optl_names)),
|
|
53
|
+
*array("op", numbers(tables.optl_params)),
|
|
54
|
+
*array("as", numbers(tables.arg_start)),
|
|
55
|
+
*array("ap", numbers(tables.arg_params)),
|
|
56
|
+
*array("an", numbers(tables.arg_nargs)),
|
|
57
|
+
*array("pf", strings(tables.param_flags)),
|
|
58
|
+
*array("vs", numbers(tables.choice_start)),
|
|
59
|
+
*array("vv", strings(tables.choice_values)),
|
|
60
|
+
*array("vk", strings(tables.choice_keys)),
|
|
61
|
+
"",
|
|
62
|
+
load_runtime("bash", prefix).rstrip("\n"),
|
|
63
|
+
"",
|
|
64
|
+
f"complete -o nosort -F {prefix}_complete {sh_quote(tables.prog_name)}",
|
|
65
|
+
]
|
|
66
|
+
return "\n".join(lines) + "\n"
|
cliha/emit/fish.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Write the fish completion script: tables, runtime, registration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
from cliha.emit._render import fish_quote, header, load_runtime, numbers, wrap
|
|
8
|
+
from cliha.emit._tables import Tables
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def emit_fish(tables: Tables) -> str:
|
|
12
|
+
prefix = tables.prefix
|
|
13
|
+
|
|
14
|
+
def table(name: str, values: Sequence[str]) -> list[str]:
|
|
15
|
+
return wrap(f"set -g {prefix}_{name}", values, continuation=" \\")
|
|
16
|
+
|
|
17
|
+
def strings(values: Sequence[str]) -> list[str]:
|
|
18
|
+
return [fish_quote(value) for value in values]
|
|
19
|
+
|
|
20
|
+
def listing(names: Sequence[str], helps: Sequence[str]) -> list[str]:
|
|
21
|
+
# What fish shows: the value, a tab, and its description.
|
|
22
|
+
return strings(
|
|
23
|
+
[f"{name}\t{help}" if help else name for name, help in zip(names, helps, strict=True)]
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
lines = [
|
|
27
|
+
*header(tables.prog_name, "fish"),
|
|
28
|
+
"",
|
|
29
|
+
*table("name", strings([tables.prog_name])),
|
|
30
|
+
*table("var", strings([tables.complete_var])),
|
|
31
|
+
*table("cmd", strings([tables.fallback_command])),
|
|
32
|
+
*table("nf", strings(tables.node_flags)),
|
|
33
|
+
*table("nm", numbers(tables.node_maxn)),
|
|
34
|
+
*table("sk", strings(tables.sub_keys)),
|
|
35
|
+
*table("sv", numbers(tables.sub_ids)),
|
|
36
|
+
*table("ok", strings(tables.opt_keys)),
|
|
37
|
+
*table("oa", list(tables.opt_arity)),
|
|
38
|
+
*table("oq", numbers(tables.opt_param)),
|
|
39
|
+
*table("ow", numbers(tables.opt_nargs)),
|
|
40
|
+
*table("cs", numbers(tables.cmd_start)),
|
|
41
|
+
*table("cl", listing(tables.cmd_names, tables.cmd_helps)),
|
|
42
|
+
*table("os", numbers(tables.optl_start)),
|
|
43
|
+
*table("ol", listing(tables.optl_names, tables.optl_helps)),
|
|
44
|
+
*table("op", numbers(tables.optl_params)),
|
|
45
|
+
*table("as", numbers(tables.arg_start)),
|
|
46
|
+
*table("ap", numbers(tables.arg_params)),
|
|
47
|
+
*table("an", numbers(tables.arg_nargs)),
|
|
48
|
+
*table("pf", strings(tables.param_flags)),
|
|
49
|
+
*table("vs", numbers(tables.choice_start)),
|
|
50
|
+
*table("vv", strings(tables.choice_values)),
|
|
51
|
+
"",
|
|
52
|
+
load_runtime("fish", prefix).rstrip("\n"),
|
|
53
|
+
"",
|
|
54
|
+
f"complete --no-files --command {fish_quote(tables.prog_name)}"
|
|
55
|
+
f" --arguments '({prefix}_complete)'",
|
|
56
|
+
]
|
|
57
|
+
return "\n".join(lines) + "\n"
|
cliha/emit/zsh.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Write the zsh completion script: tables, runtime, registration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
from cliha.emit._render import header, load_runtime, numbers, sh_quote, wrap
|
|
8
|
+
from cliha.emit._tables import Tables
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
_REGISTRATION = """\
|
|
12
|
+
if [[ ${zsh_eval_context[-1]} == loadautofunc ]]; then
|
|
13
|
+
# Autoloaded from fpath: this file is the body of the completion function.
|
|
14
|
+
# Replace that body, so that the tables above are loaded only once.
|
|
15
|
+
functions[${funcstack[1]}]='%(prefix)s_complete "$@"'
|
|
16
|
+
%(prefix)s_complete "$@"
|
|
17
|
+
elif (( ${+functions[compdef]} )); then
|
|
18
|
+
compdef %(prefix)s_complete %(prog_name)s
|
|
19
|
+
else
|
|
20
|
+
print -u2 -r -- "cliha: completion for "%(prog_name)s" needs compinit to be run first"
|
|
21
|
+
fi"""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def emit_zsh(tables: Tables) -> str:
|
|
25
|
+
prefix = tables.prefix
|
|
26
|
+
|
|
27
|
+
def scalar(name: str, value: str) -> list[str]:
|
|
28
|
+
return [f"typeset -g {prefix}_{name}={sh_quote(value)}"]
|
|
29
|
+
|
|
30
|
+
def array(name: str, values: Sequence[str]) -> list[str]:
|
|
31
|
+
return [
|
|
32
|
+
f"typeset -ga {prefix}_{name}",
|
|
33
|
+
*wrap(f"{prefix}_{name}=(", values, ")", tight=True),
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
def mapping(name: str, keys: Sequence[str], values: Sequence[str]) -> list[str]:
|
|
37
|
+
pairs = [f"{sh_quote(key)} {value}" for key, value in zip(keys, values, strict=True)]
|
|
38
|
+
return [
|
|
39
|
+
f"typeset -gA {prefix}_{name}",
|
|
40
|
+
*wrap(f"{prefix}_{name}=(", pairs, ")", tight=True),
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
def strings(values: Sequence[str]) -> list[str]:
|
|
44
|
+
return [sh_quote(value) for value in values]
|
|
45
|
+
|
|
46
|
+
lines = [
|
|
47
|
+
f"#compdef {tables.prog_name}",
|
|
48
|
+
*header(tables.prog_name, "zsh"),
|
|
49
|
+
"",
|
|
50
|
+
*scalar("name", tables.prog_name),
|
|
51
|
+
*scalar("var", tables.complete_var),
|
|
52
|
+
*scalar("cmd", tables.fallback_command),
|
|
53
|
+
*array("nf", strings(tables.node_flags)),
|
|
54
|
+
*array("nm", numbers(tables.node_maxn)),
|
|
55
|
+
*mapping("sub", tables.sub_keys, numbers(tables.sub_ids)),
|
|
56
|
+
*mapping("opt", tables.opt_keys, tables.opt_codes),
|
|
57
|
+
*array("cs", numbers(tables.cmd_start)),
|
|
58
|
+
*array("cn", strings(tables.cmd_names)),
|
|
59
|
+
*array("ch", strings(tables.cmd_helps)),
|
|
60
|
+
*array("os", numbers(tables.optl_start)),
|
|
61
|
+
*array("on", strings(tables.optl_names)),
|
|
62
|
+
*array("oh", strings(tables.optl_helps)),
|
|
63
|
+
*array("op", numbers(tables.optl_params)),
|
|
64
|
+
*array("as", numbers(tables.arg_start)),
|
|
65
|
+
*array("ap", numbers(tables.arg_params)),
|
|
66
|
+
*array("an", numbers(tables.arg_nargs)),
|
|
67
|
+
*array("pf", strings(tables.param_flags)),
|
|
68
|
+
*array("vs", numbers(tables.choice_start)),
|
|
69
|
+
*array("vv", strings(tables.choice_values)),
|
|
70
|
+
*array("vk", strings(tables.choice_keys)),
|
|
71
|
+
"",
|
|
72
|
+
load_runtime("zsh", prefix).rstrip("\n"),
|
|
73
|
+
"",
|
|
74
|
+
_REGISTRATION % {"prefix": prefix, "prog_name": sh_quote(tables.prog_name)},
|
|
75
|
+
]
|
|
76
|
+
return "\n".join(lines) + "\n"
|
cliha/entry_points.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Resolve installed console script entry points for Click factories."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib.metadata
|
|
6
|
+
import shutil
|
|
7
|
+
import sysconfig
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class ConsoleScript:
|
|
13
|
+
"""An installed ``[project.scripts]`` console entry point."""
|
|
14
|
+
|
|
15
|
+
name: str
|
|
16
|
+
target: str
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def iter_console_scripts() -> tuple[ConsoleScript, ...]:
|
|
20
|
+
"""Return all installed console script entry points."""
|
|
21
|
+
|
|
22
|
+
selected = importlib.metadata.entry_points().select(group="console_scripts")
|
|
23
|
+
return tuple(ConsoleScript(name=ep.name, target=ep.value) for ep in selected)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def find_console_scripts_for_factory(factory: str) -> tuple[ConsoleScript, ...]:
|
|
27
|
+
"""Return console scripts whose target matches *factory* (``module:attr``)."""
|
|
28
|
+
|
|
29
|
+
normalized = _normalize_factory(factory)
|
|
30
|
+
return tuple(
|
|
31
|
+
script
|
|
32
|
+
for script in iter_console_scripts()
|
|
33
|
+
if _normalize_factory(script.target) == normalized
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def resolve_prog_name(
|
|
38
|
+
factory: str,
|
|
39
|
+
command_name: str | None,
|
|
40
|
+
*,
|
|
41
|
+
explicit: str | None = None,
|
|
42
|
+
from_config: str | None = None,
|
|
43
|
+
) -> str:
|
|
44
|
+
"""Pick the executable name used in generated completion scripts.
|
|
45
|
+
|
|
46
|
+
Resolution order:
|
|
47
|
+
1. explicit CLI ``--prog-name``
|
|
48
|
+
2. ``HybridConfig.prog_name`` from the decorated command
|
|
49
|
+
3. a unique matching ``[project.scripts]`` console entry point
|
|
50
|
+
4. the Click command name, or ``"cli"``
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
if explicit is not None:
|
|
54
|
+
return explicit
|
|
55
|
+
if from_config is not None:
|
|
56
|
+
return from_config
|
|
57
|
+
|
|
58
|
+
scripts = find_console_scripts_for_factory(factory)
|
|
59
|
+
if len(scripts) == 1:
|
|
60
|
+
return scripts[0].name
|
|
61
|
+
if len(scripts) > 1:
|
|
62
|
+
for script in scripts:
|
|
63
|
+
if script.name == command_name:
|
|
64
|
+
return script.name
|
|
65
|
+
|
|
66
|
+
if command_name:
|
|
67
|
+
return command_name
|
|
68
|
+
return "cli"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def resolve_console_script_path(script_name: str) -> str | None:
|
|
72
|
+
"""Return the filesystem path of an installed console script, if any.
|
|
73
|
+
|
|
74
|
+
The scripts directory of the running environment is searched first, so a
|
|
75
|
+
script installed in a virtual environment is found whether or not the
|
|
76
|
+
environment is activated; ``PATH`` comes second.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
scripts_dir = sysconfig.get_path("scripts")
|
|
80
|
+
if scripts_dir:
|
|
81
|
+
found = shutil.which(script_name, path=scripts_dir)
|
|
82
|
+
if found is not None:
|
|
83
|
+
return found
|
|
84
|
+
return shutil.which(script_name)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _normalize_factory(factory: str) -> str:
|
|
88
|
+
return factory.strip()
|