Design#
How it works#
App.main(argv)
-> build_parser(): one argparse subparser per Command
Option -> a plain flag or positional
Question -> a flag derived from its key, grouped by section
interactive -> --yes and --questions
-> parse_args
-> --questions? Interview.document() -> JSON on stdout, exit 0
-> Interaction.resolve(--yes, stdin is a terminal, declared consents)
-> Interview.run(): for each question in order
does it apply? conditions over options, earlier answers and facts
flag given? validate it, or fail naming the flag
can prompt? ask, validating at the prompt until it passes
otherwise its default, or fail if it has no safe default
-> Command.run(Invocation(console, interaction, answers, options))
Consent.ask(...) --yes, a person, the safe default, or fail
-> one error policy -> exit code
Modules#
Module |
Responsibility |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
The |
|
|
|
Prompt loops for answers with several items, and |
|
The free-text prompt listing suggestions under the input. |
|
Rich-rendered help and usage. |
|
The frozen pydantic base of every declaration. |
Everything public is exported from cli_interview_kit; modules starting with an underscore are
internal and may change in any release.
Decisions#
Declarations are data. A command is a frozen pydantic model, not decorated functions. The same declaration then yields the parser, the interview, the unattended behaviour and the
--questionsdocument, and pydantic validates it when it is built. Rejected: decorators in the style of Click or Typer, which spread a command’s prompts through its code where neither the framework nor an assistant can list them.argparse, not Click or Typer. The parser is generated, so argparse’s standard-library stability matters more than a nicer authoring API, and it adds no dependency.
questionary for prompts, plus one custom prompt. questionary covers select, checkbox, confirm and text. Free text with suggestions is a small prompt_toolkit application (
_suggest.py) that lists matches as plain lines under the input; prompt_toolkit’s own completion menu floats in a framed box at the cursor and was rejected as hard to read.Answers and consents are different things. An answer has a default and is safe to take unattended; a consent allows something hard to undo and is never given by a default unless the author declares a safe one. A piped stdin never consents, so
yes | tool deletedoes not delete.The assistant procedure travels with the document.
--questionscarries theprocedureandrules, so the behaviour is the same in every assistant and nothing has to be installed per assistant or per repository.Validators are plain functions of the form
(str) -> strraisingValueError. The same function checks a prompt answer (shown under the input) and a flag value (fails the run).Status on stderr, plain text. Results stay pipeable, and a user’s input cannot be read as rich markup. Status lines are not hard-wrapped, so a log or an assistant sees one line per message.
Trade-offs#
Answers are a
dict[str, Any]keyed by question key, not a typed object. Typed answers would need a model per command; a key typo fails at run time with aKeyError.The help renderer reads argparse’s underscored attributes (
_actions,_action_groups). They have been stable for many releases, and CI runs the help tests on the oldest and newest supported Python to catch a change.Dynamic sources run when needed.
choices_fromand similar sources are called during the interview, and by--questionsonly when listed inoffline, so a slow or remote source never delays the document.Conditions on facts are checked at run time. Facts are computed by a function, so a condition naming a fact that does not exist is not caught when the declaration is built; it simply never holds.
Extending#
A new question kind that most apps need belongs in
QuestionKind,HOW_TO_ANSWER, the kind rules inQuestion._check_kind,Interview.add_argumentsand_Asker. One app’s own kind should be aCustomKindinstead.A new field in
--questionsis additive and needs no version change; changing or removing one raisesQUESTIONS_FORMAT_VERSION, and the snapshot test shows the difference.
Anything that adds a runtime dependency, an option kept “just in case”, or a second way to do an existing thing should be discussed in an issue first (see CONTRIBUTING.md).