cli-interview-kit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cli_interview_kit/__init__.py +61 -0
- cli_interview_kit/_help.py +102 -0
- cli_interview_kit/_model.py +15 -0
- cli_interview_kit/_prompts.py +84 -0
- cli_interview_kit/_suggest.py +199 -0
- cli_interview_kit/app.py +233 -0
- cli_interview_kit/command.py +163 -0
- cli_interview_kit/console.py +280 -0
- cli_interview_kit/errors.py +19 -0
- cli_interview_kit/interaction.py +104 -0
- cli_interview_kit/interview.py +862 -0
- cli_interview_kit/py.typed +0 -0
- cli_interview_kit/validators.py +53 -0
- cli_interview_kit-0.1.0.dist-info/METADATA +222 -0
- cli_interview_kit-0.1.0.dist-info/RECORD +17 -0
- cli_interview_kit-0.1.0.dist-info/WHEEL +4 -0
- cli_interview_kit-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,862 @@
|
|
|
1
|
+
"""Interviews declared as data: questions, consents, and the engine answering them.
|
|
2
|
+
|
|
3
|
+
A command declares an ``Interview``: its questions in order, the consents it
|
|
4
|
+
may ask for, and where dynamic choices, defaults and facts come from. Every
|
|
5
|
+
way of answering is derived from that one declaration:
|
|
6
|
+
|
|
7
|
+
- one flag per question, so the command runs from flags alone;
|
|
8
|
+
- a terminal interview that asks each applicable question its flag did not
|
|
9
|
+
answer, validating every answer at its prompt;
|
|
10
|
+
- defaults, or an error naming the flag for an answer with no safe default,
|
|
11
|
+
when nobody can be asked (see ``cli_interview_kit.interaction``);
|
|
12
|
+
- the ``--questions`` JSON document, from which an AI assistant interviews
|
|
13
|
+
the user in its own session and then runs the command with ``--yes`` and
|
|
14
|
+
the flags.
|
|
15
|
+
|
|
16
|
+
A question's flag is derived from its key (``max_workers`` is
|
|
17
|
+
``--max-workers``). Declarations are checked when they are built: a
|
|
18
|
+
question whose settings do not fit its kind, or an interview referring to a
|
|
19
|
+
source, kind or group it does not declare, raises pydantic's
|
|
20
|
+
``ValidationError`` at import time rather than failing a user's run.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import argparse
|
|
26
|
+
import string
|
|
27
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
28
|
+
from typing import Any, Literal
|
|
29
|
+
|
|
30
|
+
from pydantic import Field, field_serializer, model_validator
|
|
31
|
+
|
|
32
|
+
from cli_interview_kit._model import FrozenModel
|
|
33
|
+
from cli_interview_kit._prompts import parse_kv, prompt_list, prompt_pairs
|
|
34
|
+
from cli_interview_kit.console import Console
|
|
35
|
+
from cli_interview_kit.errors import CliError
|
|
36
|
+
from cli_interview_kit.interaction import Consent, Interaction
|
|
37
|
+
from cli_interview_kit.validators import Validator, all_of, non_empty, whole_number
|
|
38
|
+
|
|
39
|
+
QuestionKind = Literal[
|
|
40
|
+
"text", "select", "multiselect", "confirm", "int", "list", "pairs", "per_item", "custom"
|
|
41
|
+
]
|
|
42
|
+
"""How a question is answered; ``HOW_TO_ANSWER`` gives each kind's flag form."""
|
|
43
|
+
|
|
44
|
+
HOW_TO_ANSWER: dict[str, str] = {
|
|
45
|
+
"text": "--flag VALUE",
|
|
46
|
+
"select": "--flag CHOICE",
|
|
47
|
+
"multiselect": "repeat --flag CHOICE; omitted means every choice",
|
|
48
|
+
"confirm": "--flag or --no-flag",
|
|
49
|
+
"int": "--flag NUMBER",
|
|
50
|
+
"list": "repeat --flag VALUE",
|
|
51
|
+
"pairs": "repeat --flag KEY=VALUE",
|
|
52
|
+
"per_item": "repeat --flag ITEM=VALUE, one per item of items_from",
|
|
53
|
+
"custom": "see the question's custom kind under custom_kinds",
|
|
54
|
+
}
|
|
55
|
+
"""Each kind's flag form, printed with ``--questions``."""
|
|
56
|
+
|
|
57
|
+
PROCEDURE: tuple[str, ...] = (
|
|
58
|
+
"You are running this command for a user. Ask them the questions below, "
|
|
59
|
+
"then run the command with their answers; do not answer for them.",
|
|
60
|
+
"Go through the questions in order, a few at a time, grouped by section, "
|
|
61
|
+
"using whatever way of asking the user you have.",
|
|
62
|
+
"Skip a question whose when_all/when_any does not hold for the answers, "
|
|
63
|
+
"options and facts so far.",
|
|
64
|
+
"Offer a select or multiselect question's choices (choices, choices_now, "
|
|
65
|
+
"with their labels) as options and a confirm question as yes/no. For a "
|
|
66
|
+
"free-text question, offer its suggestions_now as options and accept any "
|
|
67
|
+
"other value the user gives.",
|
|
68
|
+
"Ask each applicable group once, as whether to keep its questions' "
|
|
69
|
+
"defaults; ask the group's questions one by one only if the user wants a "
|
|
70
|
+
"change. Do not bundle any other questions.",
|
|
71
|
+
"A required question must be answered. Do not ask a prompted: false "
|
|
72
|
+
"question unless the user brings it up.",
|
|
73
|
+
"Before running, describe each consent that can apply and pass --yes only "
|
|
74
|
+
"once the user has agreed to all of them. Consent does not carry over to "
|
|
75
|
+
"another run.",
|
|
76
|
+
"Run `command` with one flag per answer, as how_to_answer shows; leave "
|
|
77
|
+
"out the flag of an answer that kept its default.",
|
|
78
|
+
"If the run rejects a value, ask that question again with the error, then rerun.",
|
|
79
|
+
)
|
|
80
|
+
"""How an assistant runs a command for a user, printed with ``--questions``.
|
|
81
|
+
|
|
82
|
+
The procedure travels with the document, so any assistant that can run a
|
|
83
|
+
shell command can follow it, whichever tool it is.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
RULES: tuple[str, ...] = (
|
|
87
|
+
"Passing a flag for a question that does not apply fails the run.",
|
|
88
|
+
"Without its flag a question takes its default under --yes; a required "
|
|
89
|
+
"question has no safe default.",
|
|
90
|
+
"A default may reference earlier answers ({name}) and, for per_item, {item}.",
|
|
91
|
+
"choices_from/items_from/suggestions_from name a dynamic source; "
|
|
92
|
+
"choices_now, items_now and suggestions_now list its current values when "
|
|
93
|
+
"it is local, otherwise the CLI checks the answer when it runs.",
|
|
94
|
+
"prompted false: a setting the terminal interview never asks.",
|
|
95
|
+
"consents: confirmations the run may need, given by --yes.",
|
|
96
|
+
)
|
|
97
|
+
"""What the fields of the ``--questions`` document mean."""
|
|
98
|
+
|
|
99
|
+
QUESTIONS_FORMAT_VERSION = 1
|
|
100
|
+
"""The ``version`` of the ``--questions`` document.
|
|
101
|
+
|
|
102
|
+
Raised when a field changes meaning or is removed, so an assistant reading
|
|
103
|
+
the document can tell; adding a field does not raise it.
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
ChoiceSource = Callable[[Mapping[str, Any], str | None], Sequence[str]]
|
|
107
|
+
"""Dynamic choices: ``(answers so far, item or None) -> choices``."""
|
|
108
|
+
|
|
109
|
+
DefaultSource = Callable[[Mapping[str, Any], str | None], str | None]
|
|
110
|
+
"""A dynamic default: ``(answers so far, item or None) -> default``."""
|
|
111
|
+
|
|
112
|
+
Facts = Callable[[Mapping[str, Any]], Mapping[str, Any]]
|
|
113
|
+
"""Facts derived from the answers and options so far, for conditions."""
|
|
114
|
+
|
|
115
|
+
_COLLECTIONS = frozenset({"multiselect", "list", "pairs", "per_item", "custom"})
|
|
116
|
+
"""Kinds answered by a repeated flag."""
|
|
117
|
+
|
|
118
|
+
_TAKES_CHOICES = frozenset({"select", "multiselect", "per_item"})
|
|
119
|
+
_TAKES_SUGGESTIONS = frozenset({"text", "list", "per_item"})
|
|
120
|
+
_TAKES_VALIDATOR = frozenset({"text", "list", "pairs", "per_item"})
|
|
121
|
+
_TAKES_DEFAULT_FROM = frozenset({"text", "select", "per_item"})
|
|
122
|
+
_TEXT_ANSWERS = frozenset({"text", "select"})
|
|
123
|
+
"""Kinds whose answer is one string, so a default may reference it."""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class Condition(FrozenModel):
|
|
127
|
+
"""An answer, option or fact a question depends on.
|
|
128
|
+
|
|
129
|
+
Attributes
|
|
130
|
+
----------
|
|
131
|
+
key : str
|
|
132
|
+
An earlier question's key, an option's ``dest``, or a fact.
|
|
133
|
+
is_in : tuple[str | bool, ...]
|
|
134
|
+
The values that satisfy the condition.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
key: str
|
|
138
|
+
is_in: tuple[str | bool, ...] = Field(min_length=1)
|
|
139
|
+
|
|
140
|
+
def holds(self, context: Mapping[str, Any]) -> bool:
|
|
141
|
+
"""Return whether the context's value is one of ``is_in``."""
|
|
142
|
+
return self.key in context and context[self.key] in self.is_in
|
|
143
|
+
|
|
144
|
+
def describe(self) -> str:
|
|
145
|
+
"""Return the condition as text, e.g. ``source_type is files``."""
|
|
146
|
+
values = " or ".join(str(value).lower() for value in self.is_in)
|
|
147
|
+
return f"{self.key} is {values}"
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def when(key: str, *values: str | bool) -> Condition:
|
|
151
|
+
"""Return a condition: ``key`` is one of ``values``."""
|
|
152
|
+
return Condition(key=key, is_in=values)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class Question(FrozenModel):
|
|
156
|
+
"""One interview question.
|
|
157
|
+
|
|
158
|
+
Attributes
|
|
159
|
+
----------
|
|
160
|
+
key : str
|
|
161
|
+
Answer key, in snake_case; the flag is ``--`` plus the key with dashes.
|
|
162
|
+
section : str
|
|
163
|
+
Interview section and help group.
|
|
164
|
+
kind : QuestionKind
|
|
165
|
+
How the question is answered.
|
|
166
|
+
prompt : str
|
|
167
|
+
Terminal prompt. For ``list`` and ``pairs`` it names one item; for
|
|
168
|
+
``per_item`` it may use ``{item}``.
|
|
169
|
+
help : str
|
|
170
|
+
What the answer means; the flag's help.
|
|
171
|
+
choices : tuple[str, ...]
|
|
172
|
+
Fixed choices of a ``select``, ``multiselect`` or ``per_item`` question.
|
|
173
|
+
choices_from : str | None
|
|
174
|
+
Dynamic choices from the interview's ``choices`` source of that name,
|
|
175
|
+
after any fixed ones (per item for ``per_item``).
|
|
176
|
+
flag_choices_from : str | None
|
|
177
|
+
Further values the flag accepts that the prompt does not offer.
|
|
178
|
+
items_from : str | None
|
|
179
|
+
The ``choices`` source listing a ``per_item`` question's items.
|
|
180
|
+
labels : dict[str, str]
|
|
181
|
+
How choices are shown.
|
|
182
|
+
default : str | bool | int | None
|
|
183
|
+
The answer when nobody is asked, and the prompt's starting value. A
|
|
184
|
+
string may reference earlier text or select answers (``{name}``) and,
|
|
185
|
+
for ``per_item``, ``{item}``.
|
|
186
|
+
default_from : str | None
|
|
187
|
+
A dynamic default from the interview's ``defaults`` source instead.
|
|
188
|
+
required : bool
|
|
189
|
+
The answer has no safe default: a run that cannot prompt fails
|
|
190
|
+
without the flag.
|
|
191
|
+
validator : Validator | None
|
|
192
|
+
Checks every answer, at the prompt and from the flag.
|
|
193
|
+
metavar : str | None
|
|
194
|
+
The flag's value placeholder in help.
|
|
195
|
+
prompted : bool
|
|
196
|
+
Whether the terminal interview asks it; ``False`` for settings only
|
|
197
|
+
set by flag.
|
|
198
|
+
custom : str | None
|
|
199
|
+
The custom kind of a ``custom`` question.
|
|
200
|
+
suggestions_from : str | None
|
|
201
|
+
A ``choices`` source of suggested values for a free-text answer:
|
|
202
|
+
listed under the input in a terminal and by ``--questions``, never
|
|
203
|
+
enforced.
|
|
204
|
+
group : str | None
|
|
205
|
+
A group from ``Interview.groups`` an assistant may ask as one
|
|
206
|
+
bundled question ("keep these defaults?").
|
|
207
|
+
when_all : tuple[Condition, ...]
|
|
208
|
+
Asked only when every condition holds.
|
|
209
|
+
when_any : tuple[Condition, ...]
|
|
210
|
+
Asked only when at least one condition holds, if any are declared.
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
key: str = Field(pattern=r"^[a-z][a-z0-9_]*$")
|
|
214
|
+
section: str = Field(min_length=1)
|
|
215
|
+
kind: QuestionKind
|
|
216
|
+
prompt: str = Field(min_length=1)
|
|
217
|
+
help: str = Field(min_length=1)
|
|
218
|
+
choices: tuple[str, ...] = ()
|
|
219
|
+
choices_from: str | None = None
|
|
220
|
+
flag_choices_from: str | None = None
|
|
221
|
+
items_from: str | None = None
|
|
222
|
+
labels: dict[str, str] = Field(default_factory=dict)
|
|
223
|
+
default: str | bool | int | None = None
|
|
224
|
+
default_from: str | None = None
|
|
225
|
+
required: bool = False
|
|
226
|
+
validator: Validator | None = None
|
|
227
|
+
metavar: str | None = None
|
|
228
|
+
prompted: bool = True
|
|
229
|
+
custom: str | None = None
|
|
230
|
+
suggestions_from: str | None = None
|
|
231
|
+
group: str | None = None
|
|
232
|
+
when_all: tuple[Condition, ...] = ()
|
|
233
|
+
when_any: tuple[Condition, ...] = ()
|
|
234
|
+
|
|
235
|
+
@property
|
|
236
|
+
def flag(self) -> str:
|
|
237
|
+
"""The flag answering this question."""
|
|
238
|
+
return "--" + self.key.replace("_", "-")
|
|
239
|
+
|
|
240
|
+
@field_serializer("validator")
|
|
241
|
+
def _validator_name(self, validator: Validator | None) -> str | None:
|
|
242
|
+
return None if validator is None else getattr(validator, "__name__", "validator")
|
|
243
|
+
|
|
244
|
+
@model_validator(mode="after")
|
|
245
|
+
def _check_kind(self) -> Question:
|
|
246
|
+
"""Reject settings that do not fit the question's kind."""
|
|
247
|
+
kind = self.kind
|
|
248
|
+
rules: list[tuple[bool, str]] = [
|
|
249
|
+
(
|
|
250
|
+
bool(self.choices or self.choices_from) and kind not in _TAKES_CHOICES,
|
|
251
|
+
"only select, multiselect and per_item questions take choices",
|
|
252
|
+
),
|
|
253
|
+
(
|
|
254
|
+
kind in ("select", "multiselect") and not (self.choices or self.choices_from),
|
|
255
|
+
f"a {kind} question needs choices or choices_from",
|
|
256
|
+
),
|
|
257
|
+
(
|
|
258
|
+
self.flag_choices_from is not None and kind not in ("select", "multiselect"),
|
|
259
|
+
"only select and multiselect questions take flag_choices_from",
|
|
260
|
+
),
|
|
261
|
+
(
|
|
262
|
+
(kind == "per_item") != (self.items_from is not None),
|
|
263
|
+
"items_from is required on a per_item question and allowed only there",
|
|
264
|
+
),
|
|
265
|
+
(
|
|
266
|
+
(kind == "custom") != (self.custom is not None),
|
|
267
|
+
"custom is required on a custom question and allowed only there",
|
|
268
|
+
),
|
|
269
|
+
(
|
|
270
|
+
self.suggestions_from is not None and kind not in _TAKES_SUGGESTIONS,
|
|
271
|
+
"only text, list and per_item questions take suggestions_from",
|
|
272
|
+
),
|
|
273
|
+
(
|
|
274
|
+
self.validator is not None and kind not in _TAKES_VALIDATOR,
|
|
275
|
+
"only text, list, pairs and per_item questions take a validator",
|
|
276
|
+
),
|
|
277
|
+
(
|
|
278
|
+
self.default_from is not None and kind not in _TAKES_DEFAULT_FROM,
|
|
279
|
+
"only text, select and per_item questions take default_from",
|
|
280
|
+
),
|
|
281
|
+
(
|
|
282
|
+
self.default is not None and self.default_from is not None,
|
|
283
|
+
"set default or default_from, not both",
|
|
284
|
+
),
|
|
285
|
+
(
|
|
286
|
+
not _default_fits(kind, self.default),
|
|
287
|
+
f"a {kind} question cannot default to {self.default!r}",
|
|
288
|
+
),
|
|
289
|
+
(
|
|
290
|
+
self.required and (self.default is not None or self.default_from is not None),
|
|
291
|
+
"a required question has no safe default; drop required or the default",
|
|
292
|
+
),
|
|
293
|
+
(self.required and kind == "confirm", "a confirm question cannot be required"),
|
|
294
|
+
(self.required and self.group is not None, "a required question cannot be grouped"),
|
|
295
|
+
(self.required and not self.prompted, "a required question must be prompted"),
|
|
296
|
+
(
|
|
297
|
+
kind == "per_item" and any(name != "item" for name in _fields(self.prompt)),
|
|
298
|
+
"a per_item prompt may only use the {item} placeholder",
|
|
299
|
+
),
|
|
300
|
+
]
|
|
301
|
+
for broken, message in rules:
|
|
302
|
+
if broken:
|
|
303
|
+
raise ValueError(f"{self.flag}: {message}.")
|
|
304
|
+
return self
|
|
305
|
+
|
|
306
|
+
def applies(self, context: Mapping[str, Any]) -> bool:
|
|
307
|
+
"""Return whether the question is asked, given the context so far."""
|
|
308
|
+
return all(c.holds(context) for c in self.when_all) and (
|
|
309
|
+
not self.when_any or any(c.holds(context) for c in self.when_any)
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
def describe_conditions(self) -> str:
|
|
313
|
+
"""Return when the question applies, as text."""
|
|
314
|
+
parts = [c.describe() for c in self.when_all]
|
|
315
|
+
if self.when_any:
|
|
316
|
+
parts.append(" or ".join(c.describe() for c in self.when_any))
|
|
317
|
+
return " and ".join(parts)
|
|
318
|
+
|
|
319
|
+
def placeholders(self) -> list[str]:
|
|
320
|
+
"""Return the ``{name}`` placeholders of a string default."""
|
|
321
|
+
return _fields(self.default) if isinstance(self.default, str) else []
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _fields(template: str) -> list[str]:
|
|
325
|
+
"""Return the ``{name}`` placeholders of a format string."""
|
|
326
|
+
return [name for _, name, _, _ in string.Formatter().parse(template) if name is not None]
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _default_fits(kind: QuestionKind, default: object) -> bool:
|
|
330
|
+
"""Return whether ``default`` has the type a ``kind`` answer has."""
|
|
331
|
+
if default is None:
|
|
332
|
+
return True
|
|
333
|
+
if kind == "confirm":
|
|
334
|
+
return isinstance(default, bool)
|
|
335
|
+
if kind == "int":
|
|
336
|
+
return isinstance(default, int) and not isinstance(default, bool)
|
|
337
|
+
if kind in ("text", "select", "per_item"):
|
|
338
|
+
return isinstance(default, str)
|
|
339
|
+
return False
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
class CustomKind(FrozenModel):
|
|
343
|
+
"""A question kind an app defines, with its own flag form and prompt.
|
|
344
|
+
|
|
345
|
+
Registered in ``Interview.custom_kinds`` under the name a question's
|
|
346
|
+
``custom`` refers to. Its flag is repeatable.
|
|
347
|
+
|
|
348
|
+
Attributes
|
|
349
|
+
----------
|
|
350
|
+
flag_form : str
|
|
351
|
+
How one flag value is written, for help and ``--questions``.
|
|
352
|
+
parse : Callable[[list[str]], Any]
|
|
353
|
+
Turns the flag's values into the answer; raises ``CliError``.
|
|
354
|
+
prompt : Callable[[Console], Any]
|
|
355
|
+
Asks for the answer in a terminal, validating as it goes.
|
|
356
|
+
"""
|
|
357
|
+
|
|
358
|
+
flag_form: str = Field(min_length=1)
|
|
359
|
+
parse: Callable[[list[str]], Any]
|
|
360
|
+
prompt: Callable[[Console], Any]
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
class Interview(FrozenModel):
|
|
364
|
+
"""A command's questions and consents, and where their dynamic parts come from.
|
|
365
|
+
|
|
366
|
+
Attributes
|
|
367
|
+
----------
|
|
368
|
+
questions : tuple[Question, ...]
|
|
369
|
+
The questions, in the order they are asked.
|
|
370
|
+
consents : tuple[Consent, ...]
|
|
371
|
+
The confirmations the command may ask for while it runs.
|
|
372
|
+
choices : dict[str, ChoiceSource]
|
|
373
|
+
Dynamic choice, item and suggestion sources, by name.
|
|
374
|
+
defaults : dict[str, DefaultSource]
|
|
375
|
+
Dynamic default sources, by name.
|
|
376
|
+
offline : frozenset[str]
|
|
377
|
+
The choice sources that are local and cheap: ``--questions`` lists
|
|
378
|
+
their values and only names the others.
|
|
379
|
+
facts : Facts | None
|
|
380
|
+
Facts derived from the answers and options, for conditions.
|
|
381
|
+
custom_kinds : dict[str, CustomKind]
|
|
382
|
+
The custom kinds the questions use, by name.
|
|
383
|
+
groups : dict[str, str]
|
|
384
|
+
Bundled question groups, by name, each with what it covers in one
|
|
385
|
+
line.
|
|
386
|
+
"""
|
|
387
|
+
|
|
388
|
+
questions: tuple[Question, ...] = ()
|
|
389
|
+
consents: tuple[Consent, ...] = ()
|
|
390
|
+
choices: dict[str, ChoiceSource] = Field(default_factory=dict)
|
|
391
|
+
defaults: dict[str, DefaultSource] = Field(default_factory=dict)
|
|
392
|
+
offline: frozenset[str] = frozenset()
|
|
393
|
+
facts: Facts | None = None
|
|
394
|
+
custom_kinds: dict[str, CustomKind] = Field(default_factory=dict)
|
|
395
|
+
groups: dict[str, str] = Field(default_factory=dict)
|
|
396
|
+
|
|
397
|
+
@model_validator(mode="after")
|
|
398
|
+
def _check_references(self) -> Interview:
|
|
399
|
+
"""Reject duplicate keys and references to anything not declared."""
|
|
400
|
+
problems: list[str] = []
|
|
401
|
+
for name in sorted(self.offline - self.choices.keys()):
|
|
402
|
+
problems.append(f"offline names undeclared choices source '{name}'")
|
|
403
|
+
for keys, what in (
|
|
404
|
+
([q.key for q in self.questions], "question"),
|
|
405
|
+
([c.key for c in self.consents], "consent"),
|
|
406
|
+
):
|
|
407
|
+
for key in sorted({k for k in keys if keys.count(k) > 1}):
|
|
408
|
+
problems.append(f"{what} key '{key}' is declared twice")
|
|
409
|
+
grouped = {q.group for q in self.questions}
|
|
410
|
+
for name in sorted(self.groups.keys() - grouped):
|
|
411
|
+
problems.append(f"group '{name}' has no questions")
|
|
412
|
+
earlier: dict[str, Question] = {}
|
|
413
|
+
later = {q.key for q in self.questions}
|
|
414
|
+
for question in self.questions:
|
|
415
|
+
later.discard(question.key)
|
|
416
|
+
problems.extend(self._question_problems(question, earlier, later))
|
|
417
|
+
earlier[question.key] = question
|
|
418
|
+
if problems:
|
|
419
|
+
raise ValueError("; ".join(problems) + ".")
|
|
420
|
+
return self
|
|
421
|
+
|
|
422
|
+
def _question_problems(
|
|
423
|
+
self, question: Question, earlier: Mapping[str, Question], later: set[str]
|
|
424
|
+
) -> list[str]:
|
|
425
|
+
"""Return what is wrong with one question's references."""
|
|
426
|
+
flag = question.flag
|
|
427
|
+
problems = [
|
|
428
|
+
f"{flag} names undeclared {registry} '{name}'"
|
|
429
|
+
for name, names, registry in (
|
|
430
|
+
(question.choices_from, self.choices, "choices source"),
|
|
431
|
+
(question.flag_choices_from, self.choices, "choices source"),
|
|
432
|
+
(question.items_from, self.choices, "choices source"),
|
|
433
|
+
(question.suggestions_from, self.choices, "choices source"),
|
|
434
|
+
(question.default_from, self.defaults, "defaults source"),
|
|
435
|
+
(question.custom, self.custom_kinds, "custom kind"),
|
|
436
|
+
(question.group, self.groups, "group"),
|
|
437
|
+
)
|
|
438
|
+
if name is not None and name not in names
|
|
439
|
+
]
|
|
440
|
+
for name in question.placeholders():
|
|
441
|
+
if name == "item" and question.kind == "per_item":
|
|
442
|
+
continue
|
|
443
|
+
source = earlier.get(name)
|
|
444
|
+
if source is None or source.kind not in _TEXT_ANSWERS:
|
|
445
|
+
problems.append(
|
|
446
|
+
f"{flag}'s default references {{{name}}}, which is not an "
|
|
447
|
+
"earlier text or select question"
|
|
448
|
+
)
|
|
449
|
+
for condition in (*question.when_all, *question.when_any):
|
|
450
|
+
if condition.key == question.key or condition.key in later:
|
|
451
|
+
problems.append(
|
|
452
|
+
f"{flag} depends on '{condition.key}', which is not answered before it"
|
|
453
|
+
)
|
|
454
|
+
return problems
|
|
455
|
+
|
|
456
|
+
@property
|
|
457
|
+
def consent_keys(self) -> frozenset[str]:
|
|
458
|
+
"""The keys of the declared consents."""
|
|
459
|
+
return frozenset(consent.key for consent in self.consents)
|
|
460
|
+
|
|
461
|
+
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
|
|
462
|
+
"""Add one flag per question to ``parser``, grouped by section."""
|
|
463
|
+
sections: dict[str, argparse._ArgumentGroup] = {} # pyright: ignore[reportPrivateUsage]
|
|
464
|
+
for question in self.questions:
|
|
465
|
+
if question.section not in sections:
|
|
466
|
+
sections[question.section] = parser.add_argument_group(question.section)
|
|
467
|
+
kwargs: dict[str, Any] = {"dest": question.key, "help": question.help}
|
|
468
|
+
if question.metavar:
|
|
469
|
+
kwargs["metavar"] = question.metavar
|
|
470
|
+
elif question.custom is not None:
|
|
471
|
+
kwargs["metavar"] = self.custom_kinds[question.custom].flag_form
|
|
472
|
+
if question.kind == "confirm":
|
|
473
|
+
kwargs.update(action=argparse.BooleanOptionalAction, default=None)
|
|
474
|
+
elif question.kind in _COLLECTIONS:
|
|
475
|
+
kwargs["action"] = "append"
|
|
476
|
+
elif question.kind == "int":
|
|
477
|
+
kwargs["type"] = int
|
|
478
|
+
if (
|
|
479
|
+
question.kind in ("select", "multiselect")
|
|
480
|
+
and question.choices_from is None
|
|
481
|
+
and question.flag_choices_from is None
|
|
482
|
+
):
|
|
483
|
+
kwargs["choices"] = list(question.choices)
|
|
484
|
+
_ = sections[question.section].add_argument(question.flag, **kwargs)
|
|
485
|
+
|
|
486
|
+
def run(
|
|
487
|
+
self,
|
|
488
|
+
console: Console,
|
|
489
|
+
interaction: Interaction,
|
|
490
|
+
flag_values: Mapping[str, Any],
|
|
491
|
+
options: Mapping[str, Any],
|
|
492
|
+
) -> dict[str, Any]:
|
|
493
|
+
"""Answer every applicable question from its flag, a prompt, or its default.
|
|
494
|
+
|
|
495
|
+
Parameters
|
|
496
|
+
----------
|
|
497
|
+
console : Console
|
|
498
|
+
Where to ask.
|
|
499
|
+
interaction : Interaction
|
|
500
|
+
Whether the run may prompt.
|
|
501
|
+
flag_values : Mapping[str, Any]
|
|
502
|
+
The parsed flags, by question key; ``None`` or ``[]`` if not given.
|
|
503
|
+
options : Mapping[str, Any]
|
|
504
|
+
The command's option values, which conditions may test.
|
|
505
|
+
|
|
506
|
+
Returns
|
|
507
|
+
-------
|
|
508
|
+
dict[str, Any]
|
|
509
|
+
The answers of the questions that apply, by key.
|
|
510
|
+
|
|
511
|
+
Raises
|
|
512
|
+
------
|
|
513
|
+
CliError
|
|
514
|
+
If a flag answers a question that does not apply, a value fails
|
|
515
|
+
validation, or a run that cannot prompt misses a required answer.
|
|
516
|
+
"""
|
|
517
|
+
asker = _Asker(self, console, interaction.can_prompt)
|
|
518
|
+
answers: dict[str, Any] = {}
|
|
519
|
+
for question in self.questions:
|
|
520
|
+
flag_value = flag_values.get(question.key)
|
|
521
|
+
if not question.applies(self._context(answers, options)):
|
|
522
|
+
if _supplied(flag_value):
|
|
523
|
+
raise CliError(
|
|
524
|
+
f"{question.flag} does not apply here: it is only used "
|
|
525
|
+
f"when {question.describe_conditions()}."
|
|
526
|
+
)
|
|
527
|
+
continue
|
|
528
|
+
if not question.prompted and not _supplied(flag_value):
|
|
529
|
+
answers[question.key] = asker.unprompted(question, answers)
|
|
530
|
+
continue
|
|
531
|
+
answers[question.key] = asker.ask(question, flag_value, answers)
|
|
532
|
+
return answers
|
|
533
|
+
|
|
534
|
+
def _context(self, answers: Mapping[str, Any], options: Mapping[str, Any]) -> dict[str, Any]:
|
|
535
|
+
"""Return what conditions see: options, answers, then facts."""
|
|
536
|
+
context = {**options, **answers}
|
|
537
|
+
if self.facts is not None:
|
|
538
|
+
context.update(self.facts(context))
|
|
539
|
+
return context
|
|
540
|
+
|
|
541
|
+
def document(self, command_line: str, options: Sequence[Mapping[str, Any]]) -> dict[str, Any]:
|
|
542
|
+
"""Return the interview as the document ``--questions`` prints.
|
|
543
|
+
|
|
544
|
+
Parameters
|
|
545
|
+
----------
|
|
546
|
+
command_line : str
|
|
547
|
+
How the command is invoked, e.g. ``mytool deploy``.
|
|
548
|
+
options : Sequence[Mapping[str, Any]]
|
|
549
|
+
The command's plain options, described.
|
|
550
|
+
"""
|
|
551
|
+
return {
|
|
552
|
+
"version": QUESTIONS_FORMAT_VERSION,
|
|
553
|
+
"command": f"{command_line} --yes",
|
|
554
|
+
"how_to_answer": HOW_TO_ANSWER,
|
|
555
|
+
"procedure": list(PROCEDURE),
|
|
556
|
+
"rules": list(RULES),
|
|
557
|
+
"questions": [self._describe(question) for question in self.questions],
|
|
558
|
+
"custom_kinds": {name: kind.flag_form for name, kind in self.custom_kinds.items()},
|
|
559
|
+
"groups": {
|
|
560
|
+
name: {
|
|
561
|
+
"covers": covers,
|
|
562
|
+
"questions": [q.flag for q in self.questions if q.group == name],
|
|
563
|
+
}
|
|
564
|
+
for name, covers in self.groups.items()
|
|
565
|
+
},
|
|
566
|
+
"consents": [consent.model_dump(mode="json") for consent in self.consents],
|
|
567
|
+
"options": list(options),
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
def _describe(self, question: Question) -> dict[str, Any]:
|
|
571
|
+
"""Return one question for the document, with its local values."""
|
|
572
|
+
entry: dict[str, Any] = {"flag": question.flag, **question.model_dump(mode="json")}
|
|
573
|
+
for field, source in (("choices", question.choices_from), ("items", question.items_from)):
|
|
574
|
+
if source in self.offline:
|
|
575
|
+
entry[f"{field}_now"] = list(self.choices[source]({}, None))
|
|
576
|
+
suggestions = question.suggestions_from
|
|
577
|
+
if suggestions in self.offline:
|
|
578
|
+
source = self.choices[suggestions]
|
|
579
|
+
items = question.items_from
|
|
580
|
+
if question.kind == "per_item" and items in self.offline:
|
|
581
|
+
entry["suggestions_now"] = {
|
|
582
|
+
item: list(source({}, item)) for item in self.choices[items]({}, None)
|
|
583
|
+
}
|
|
584
|
+
else:
|
|
585
|
+
entry["suggestions_now"] = list(source({}, None))
|
|
586
|
+
return entry
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def _supplied(flag_value: object) -> bool:
|
|
590
|
+
"""Return whether a flag was given on the command line."""
|
|
591
|
+
return flag_value is not None and flag_value != []
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
class _Defaults(dict[str, str]):
|
|
595
|
+
"""Placeholder values for a default; an unanswered one is empty."""
|
|
596
|
+
|
|
597
|
+
def __missing__(self, key: str) -> str:
|
|
598
|
+
return ""
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
class _Asker:
|
|
602
|
+
"""Answers one question at a time for one run of an interview."""
|
|
603
|
+
|
|
604
|
+
def __init__(self, interview: Interview, console: Console, can_prompt: bool) -> None:
|
|
605
|
+
self._interview = interview
|
|
606
|
+
self._console = console
|
|
607
|
+
self._can_prompt = can_prompt
|
|
608
|
+
|
|
609
|
+
def ask(self, question: Question, flag_value: Any, answers: Mapping[str, Any]) -> Any:
|
|
610
|
+
"""Answer ``question`` by its kind."""
|
|
611
|
+
match question.kind:
|
|
612
|
+
case "text":
|
|
613
|
+
return self._text(question, flag_value, answers)
|
|
614
|
+
case "select":
|
|
615
|
+
return self._select(question, flag_value, answers)
|
|
616
|
+
case "multiselect":
|
|
617
|
+
return self._multiselect(question, flag_value, answers)
|
|
618
|
+
case "confirm":
|
|
619
|
+
return self._confirm(question, flag_value)
|
|
620
|
+
case "int":
|
|
621
|
+
return self._int(question, flag_value)
|
|
622
|
+
case "list":
|
|
623
|
+
return self._list(question, flag_value, answers)
|
|
624
|
+
case "pairs":
|
|
625
|
+
return self._pairs(question, flag_value)
|
|
626
|
+
case "per_item":
|
|
627
|
+
return self._per_item(question, flag_value, answers)
|
|
628
|
+
case "custom":
|
|
629
|
+
return self._custom(question, flag_value)
|
|
630
|
+
|
|
631
|
+
def unprompted(self, question: Question, answers: Mapping[str, Any]) -> Any:
|
|
632
|
+
"""Answer a question the interview never asks and no flag answered.
|
|
633
|
+
|
|
634
|
+
It takes its default, resolved like any other; a collection is empty.
|
|
635
|
+
"""
|
|
636
|
+
if question.kind in ("list", "multiselect"):
|
|
637
|
+
return []
|
|
638
|
+
if question.kind in ("pairs", "per_item"):
|
|
639
|
+
return {}
|
|
640
|
+
if question.kind in _TAKES_DEFAULT_FROM:
|
|
641
|
+
return self._default(question, answers)
|
|
642
|
+
return question.default
|
|
643
|
+
|
|
644
|
+
def _validator(self, question: Question) -> Validator:
|
|
645
|
+
"""Return the checks every answer to ``question`` must pass."""
|
|
646
|
+
return all_of(non_empty if question.required else None, question.validator)
|
|
647
|
+
|
|
648
|
+
def _checked(self, question: Question, value: str, label: str = "") -> str:
|
|
649
|
+
"""Validate a flag value, failing the run with the flag named."""
|
|
650
|
+
try:
|
|
651
|
+
return self._validator(question)(value)
|
|
652
|
+
except ValueError as error:
|
|
653
|
+
raise CliError(f"{label or question.flag} '{value}': {error}") from error
|
|
654
|
+
|
|
655
|
+
def _missing(self, question: Question, label: str = "") -> CliError:
|
|
656
|
+
"""Return the error for a required answer nobody can give."""
|
|
657
|
+
return CliError(
|
|
658
|
+
f"{label or question.flag} is required: it has no safe default, so "
|
|
659
|
+
"pass it when the run cannot prompt."
|
|
660
|
+
)
|
|
661
|
+
|
|
662
|
+
def _source(self, name: str | None, answers: Mapping[str, Any], item: str | None) -> list[str]:
|
|
663
|
+
"""Return the values of a choices source, or none if ``name`` is unset."""
|
|
664
|
+
return [] if name is None else list(self._interview.choices[name](answers, item))
|
|
665
|
+
|
|
666
|
+
def _default(
|
|
667
|
+
self, question: Question, answers: Mapping[str, Any], item: str | None = None
|
|
668
|
+
) -> str | None:
|
|
669
|
+
"""Return the question's default, resolving its source or placeholders."""
|
|
670
|
+
if question.default_from is not None:
|
|
671
|
+
return self._interview.defaults[question.default_from](answers, item)
|
|
672
|
+
if question.default is None:
|
|
673
|
+
return None
|
|
674
|
+
values = _Defaults({k: v for k, v in answers.items() if isinstance(v, str)})
|
|
675
|
+
if item is not None:
|
|
676
|
+
values["item"] = item
|
|
677
|
+
return str(question.default).format_map(values)
|
|
678
|
+
|
|
679
|
+
def _choices(
|
|
680
|
+
self, question: Question, answers: Mapping[str, Any], item: str | None = None
|
|
681
|
+
) -> list[str]:
|
|
682
|
+
"""Return the question's choices: fixed ones, then its source's."""
|
|
683
|
+
return [*question.choices, *self._source(question.choices_from, answers, item)]
|
|
684
|
+
|
|
685
|
+
def _flag_choices(self, question: Question, answers: Mapping[str, Any]) -> list[str]:
|
|
686
|
+
"""Return every value the flag accepts."""
|
|
687
|
+
return [
|
|
688
|
+
*self._choices(question, answers),
|
|
689
|
+
*self._source(question.flag_choices_from, answers, None),
|
|
690
|
+
]
|
|
691
|
+
|
|
692
|
+
def _label(self, question: Question) -> Callable[[str], str]:
|
|
693
|
+
return lambda choice: question.labels.get(choice, choice)
|
|
694
|
+
|
|
695
|
+
def _text(self, question: Question, flag_value: str | None, answers: Mapping[str, Any]) -> str:
|
|
696
|
+
if flag_value is not None:
|
|
697
|
+
return self._checked(question, flag_value)
|
|
698
|
+
default = self._default(question, answers) or ""
|
|
699
|
+
if self._can_prompt:
|
|
700
|
+
return self._console.text(
|
|
701
|
+
question.prompt,
|
|
702
|
+
default=default,
|
|
703
|
+
validate=self._validator(question),
|
|
704
|
+
suggestions=self._source(question.suggestions_from, answers, None),
|
|
705
|
+
)
|
|
706
|
+
if question.required:
|
|
707
|
+
raise self._missing(question)
|
|
708
|
+
return self._checked(question, default) if default else default
|
|
709
|
+
|
|
710
|
+
def _select(
|
|
711
|
+
self, question: Question, flag_value: str | None, answers: Mapping[str, Any]
|
|
712
|
+
) -> str:
|
|
713
|
+
if flag_value is not None:
|
|
714
|
+
allowed = self._flag_choices(question, answers)
|
|
715
|
+
if flag_value not in allowed:
|
|
716
|
+
raise CliError(
|
|
717
|
+
f"{question.flag} '{flag_value}' must be one of: {', '.join(allowed)}."
|
|
718
|
+
)
|
|
719
|
+
return flag_value
|
|
720
|
+
choices = self._choices(question, answers)
|
|
721
|
+
if not choices:
|
|
722
|
+
raise CliError(f"{question.flag}: there is nothing to choose from.")
|
|
723
|
+
if len(choices) == 1:
|
|
724
|
+
self._console.info(f"{question.prompt}: {self._label(question)(choices[0])}")
|
|
725
|
+
return choices[0]
|
|
726
|
+
if self._can_prompt:
|
|
727
|
+
return self._console.select(question.prompt, choices, label=self._label(question))
|
|
728
|
+
default = self._default(question, answers)
|
|
729
|
+
if default is None:
|
|
730
|
+
raise self._missing(question)
|
|
731
|
+
if default not in self._flag_choices(question, answers):
|
|
732
|
+
raise CliError(
|
|
733
|
+
f"{question.flag}: its default '{default}' is not one of "
|
|
734
|
+
f"{', '.join(choices)}; pass {question.flag}."
|
|
735
|
+
)
|
|
736
|
+
return default
|
|
737
|
+
|
|
738
|
+
def _multiselect(
|
|
739
|
+
self, question: Question, flag_value: list[str] | None, answers: Mapping[str, Any]
|
|
740
|
+
) -> list[str]:
|
|
741
|
+
if flag_value:
|
|
742
|
+
allowed = self._flag_choices(question, answers)
|
|
743
|
+
if unknown := sorted(set(flag_value) - set(allowed)):
|
|
744
|
+
raise CliError(
|
|
745
|
+
f"{question.flag} names {', '.join(unknown)}, which "
|
|
746
|
+
f"{'is' if len(unknown) == 1 else 'are'} not among: {', '.join(allowed)}."
|
|
747
|
+
)
|
|
748
|
+
return list(dict.fromkeys(flag_value))
|
|
749
|
+
choices = self._choices(question, answers)
|
|
750
|
+
if len(choices) > 1 and self._can_prompt:
|
|
751
|
+
selected = self._console.checkbox(question.prompt, choices, label=self._label(question))
|
|
752
|
+
else:
|
|
753
|
+
selected = choices
|
|
754
|
+
if question.required and not selected:
|
|
755
|
+
raise CliError(f"{question.flag}: choose at least one.")
|
|
756
|
+
return selected
|
|
757
|
+
|
|
758
|
+
def _confirm(self, question: Question, flag_value: bool | None) -> bool:
|
|
759
|
+
if flag_value is not None:
|
|
760
|
+
return flag_value
|
|
761
|
+
default = bool(question.default)
|
|
762
|
+
if self._can_prompt:
|
|
763
|
+
return self._console.confirm(question.prompt, default=default)
|
|
764
|
+
return default
|
|
765
|
+
|
|
766
|
+
def _int(self, question: Question, flag_value: int | None) -> int | None:
|
|
767
|
+
if flag_value is not None:
|
|
768
|
+
return flag_value
|
|
769
|
+
default = question.default if isinstance(question.default, int) else None
|
|
770
|
+
if not self._can_prompt:
|
|
771
|
+
if question.required:
|
|
772
|
+
raise self._missing(question)
|
|
773
|
+
return default
|
|
774
|
+
entered = self._console.text(
|
|
775
|
+
question.prompt,
|
|
776
|
+
default="" if default is None else str(default),
|
|
777
|
+
validate=all_of(non_empty if question.required else None, whole_number),
|
|
778
|
+
)
|
|
779
|
+
return int(entered) if entered.strip() else default
|
|
780
|
+
|
|
781
|
+
def _list(
|
|
782
|
+
self, question: Question, flag_value: list[str] | None, answers: Mapping[str, Any]
|
|
783
|
+
) -> list[str]:
|
|
784
|
+
if flag_value:
|
|
785
|
+
return [self._checked(question, value) for value in flag_value]
|
|
786
|
+
if self._can_prompt:
|
|
787
|
+
return prompt_list(
|
|
788
|
+
self._console,
|
|
789
|
+
question.prompt,
|
|
790
|
+
required=question.required,
|
|
791
|
+
validate=question.validator,
|
|
792
|
+
suggestions=self._source(question.suggestions_from, answers, None),
|
|
793
|
+
)
|
|
794
|
+
if question.required:
|
|
795
|
+
raise self._missing(question)
|
|
796
|
+
return []
|
|
797
|
+
|
|
798
|
+
def _pairs(self, question: Question, flag_value: list[str] | None) -> dict[str, str]:
|
|
799
|
+
if flag_value:
|
|
800
|
+
pairs = parse_kv(flag_value, question.flag)
|
|
801
|
+
return {
|
|
802
|
+
key: self._checked(question, value, f"{question.flag} {key}=...")
|
|
803
|
+
for key, value in pairs.items()
|
|
804
|
+
}
|
|
805
|
+
if self._can_prompt:
|
|
806
|
+
return prompt_pairs(
|
|
807
|
+
self._console,
|
|
808
|
+
question.prompt,
|
|
809
|
+
required=question.required,
|
|
810
|
+
validate=question.validator,
|
|
811
|
+
)
|
|
812
|
+
if question.required:
|
|
813
|
+
raise self._missing(question)
|
|
814
|
+
return {}
|
|
815
|
+
|
|
816
|
+
def _per_item(
|
|
817
|
+
self, question: Question, flag_value: list[str] | None, answers: Mapping[str, Any]
|
|
818
|
+
) -> dict[str, str]:
|
|
819
|
+
items = self._source(question.items_from, answers, None)
|
|
820
|
+
supplied = parse_kv(flag_value, question.flag)
|
|
821
|
+
if unknown := sorted(set(supplied) - set(items)):
|
|
822
|
+
raise CliError(
|
|
823
|
+
f"{question.flag} names {', '.join(unknown)}, not among: {', '.join(items)}."
|
|
824
|
+
)
|
|
825
|
+
return {
|
|
826
|
+
item: self._per_item_answer(question, item, supplied.get(item), answers)
|
|
827
|
+
for item in items
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
def _per_item_answer(
|
|
831
|
+
self, question: Question, item: str, flag_value: str | None, answers: Mapping[str, Any]
|
|
832
|
+
) -> str:
|
|
833
|
+
label = f"{question.flag} {item}=..."
|
|
834
|
+
if flag_value is not None:
|
|
835
|
+
return self._checked(question, flag_value, label)
|
|
836
|
+
default = self._default(question, answers, item)
|
|
837
|
+
if self._can_prompt:
|
|
838
|
+
prompt = question.prompt.format(item=item)
|
|
839
|
+
choices = self._choices(question, answers, item)
|
|
840
|
+
if len(choices) > 1:
|
|
841
|
+
return self._console.select(prompt, choices, label=self._label(question))
|
|
842
|
+
return self._console.text(
|
|
843
|
+
prompt,
|
|
844
|
+
default=default or "",
|
|
845
|
+
validate=self._validator(question),
|
|
846
|
+
suggestions=self._source(question.suggestions_from, answers, item),
|
|
847
|
+
)
|
|
848
|
+
if default is None:
|
|
849
|
+
if question.required:
|
|
850
|
+
raise self._missing(question, label)
|
|
851
|
+
return ""
|
|
852
|
+
return default
|
|
853
|
+
|
|
854
|
+
def _custom(self, question: Question, flag_value: list[str] | None) -> Any:
|
|
855
|
+
kind = self._interview.custom_kinds[str(question.custom)]
|
|
856
|
+
if flag_value:
|
|
857
|
+
return kind.parse(flag_value)
|
|
858
|
+
if self._can_prompt:
|
|
859
|
+
return kind.prompt(self._console)
|
|
860
|
+
if question.required:
|
|
861
|
+
raise self._missing(question)
|
|
862
|
+
return None
|