cli-interview-kit 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. cli_interview_kit-0.1.0/.gitignore +11 -0
  2. cli_interview_kit-0.1.0/CHANGELOG.md +31 -0
  3. cli_interview_kit-0.1.0/LICENSE +21 -0
  4. cli_interview_kit-0.1.0/PKG-INFO +222 -0
  5. cli_interview_kit-0.1.0/README.md +190 -0
  6. cli_interview_kit-0.1.0/docs/design.md +92 -0
  7. cli_interview_kit-0.1.0/docs/requirements.md +58 -0
  8. cli_interview_kit-0.1.0/examples/projects.py +212 -0
  9. cli_interview_kit-0.1.0/pyproject.toml +82 -0
  10. cli_interview_kit-0.1.0/src/cli_interview_kit/__init__.py +61 -0
  11. cli_interview_kit-0.1.0/src/cli_interview_kit/_help.py +102 -0
  12. cli_interview_kit-0.1.0/src/cli_interview_kit/_model.py +15 -0
  13. cli_interview_kit-0.1.0/src/cli_interview_kit/_prompts.py +84 -0
  14. cli_interview_kit-0.1.0/src/cli_interview_kit/_suggest.py +199 -0
  15. cli_interview_kit-0.1.0/src/cli_interview_kit/app.py +233 -0
  16. cli_interview_kit-0.1.0/src/cli_interview_kit/command.py +163 -0
  17. cli_interview_kit-0.1.0/src/cli_interview_kit/console.py +280 -0
  18. cli_interview_kit-0.1.0/src/cli_interview_kit/errors.py +19 -0
  19. cli_interview_kit-0.1.0/src/cli_interview_kit/interaction.py +104 -0
  20. cli_interview_kit-0.1.0/src/cli_interview_kit/interview.py +862 -0
  21. cli_interview_kit-0.1.0/src/cli_interview_kit/py.typed +0 -0
  22. cli_interview_kit-0.1.0/src/cli_interview_kit/validators.py +53 -0
  23. cli_interview_kit-0.1.0/tests/conftest.py +53 -0
  24. cli_interview_kit-0.1.0/tests/helpers.py +48 -0
  25. cli_interview_kit-0.1.0/tests/snapshots/projects_new_questions.json +329 -0
  26. cli_interview_kit-0.1.0/tests/test_app.py +226 -0
  27. cli_interview_kit-0.1.0/tests/test_console.py +56 -0
  28. cli_interview_kit-0.1.0/tests/test_declarations.py +193 -0
  29. cli_interview_kit-0.1.0/tests/test_help.py +67 -0
  30. cli_interview_kit-0.1.0/tests/test_questions_document.py +73 -0
  31. cli_interview_kit-0.1.0/tests/test_readme.py +35 -0
  32. cli_interview_kit-0.1.0/tests/test_suggest.py +74 -0
  33. cli_interview_kit-0.1.0/tests/test_terminal.py +227 -0
  34. cli_interview_kit-0.1.0/tests/test_unattended.py +347 -0
  35. cli_interview_kit-0.1.0/tests/test_validators.py +40 -0
@@ -0,0 +1,11 @@
1
+ __pycache__/
2
+ *.egg-info/
3
+ .venv/
4
+ dist/
5
+ build/
6
+ .pytest_cache/
7
+ .ruff_cache/
8
+ .coverage
9
+ coverage.xml
10
+ .DS_Store
11
+ data/
@@ -0,0 +1,31 @@
1
+ # Changelog
2
+
3
+ All notable changes are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project follows
5
+ [Semantic Versioning](https://semver.org/) (while `0.x`, a breaking change bumps the minor
6
+ version).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-11-27
11
+
12
+ First release.
13
+
14
+ ### Added
15
+
16
+ - Declare commands as data: `App`, `Command`, `Option`, `Interview`, `Question`, `Consent`,
17
+ `CustomKind`, and `when` conditions.
18
+ - Nine question kinds: `text`, `select`, `multiselect`, `confirm`, `int`, `list`, `pairs`,
19
+ `per_item` and `custom`, each with a flag derived from its key.
20
+ - A terminal interview that validates every answer and asks again until it passes, with
21
+ suggestions listed under free-text inputs as you type.
22
+ - Unattended runs: `--yes` and non-terminal stdin answer from flags and defaults, and fail naming
23
+ the flag of an answer with no safe default. Consents are given only by `--yes` or a person.
24
+ - `--questions`: the interview as a versioned JSON document with a procedure, so any AI assistant
25
+ can interview a user and run the command.
26
+ - Declarations are validated when they are built; asking for an undeclared consent raises
27
+ `DeclarationError`.
28
+ - One error policy with documented exit codes, `--debug` for tracebacks, and `--version`.
29
+ - `Console`: results on stdout, plain-text status lines and progress on stderr.
30
+ - Built-in validators `non_empty`, `whole_number` and `byte_size`, and `parse_kv` for custom
31
+ kinds.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Northbridge Capital
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.
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.5
2
+ Name: cli-interview-kit
3
+ Version: 0.1.0
4
+ Summary: Python CLIs whose prompts are declared once and answered at the terminal, by flags, or by an AI assistant.
5
+ Project-URL: Homepage, https://github.com/Northbridge-Capital/cli-interview-kit
6
+ Project-URL: Source, https://github.com/Northbridge-Capital/cli-interview-kit
7
+ Project-URL: Issues, https://github.com/Northbridge-Capital/cli-interview-kit/issues
8
+ Project-URL: Changelog, https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/CHANGELOG.md
9
+ Author: Northbridge Capital
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: ai-agents,argparse,cli,interactive,prompts,questionary,rich
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Environment :: Console
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3 :: Only
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
24
+ Classifier: Topic :: Software Development :: User Interfaces
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.11
27
+ Requires-Dist: prompt-toolkit>=3.0.36
28
+ Requires-Dist: pydantic>=2.7
29
+ Requires-Dist: questionary>=2.0.1
30
+ Requires-Dist: rich>=13.7
31
+ Description-Content-Type: text/markdown
32
+
33
+ # cli-interview-kit
34
+
35
+ [![CI](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml)
36
+ [![PyPI](https://img.shields.io/pypi/v/cli-interview-kit)](https://pypi.org/project/cli-interview-kit/)
37
+ [![Python](https://img.shields.io/pypi/pyversions/cli-interview-kit)](https://pypi.org/project/cli-interview-kit/)
38
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE)
39
+
40
+ Build Python command-line tools whose prompts are declared once and answered in three ways:
41
+
42
+ - **at the terminal**, as an interview that validates every answer and asks again until it is
43
+ right;
44
+ - **by flags**, because every question has one, so the same command runs in CI and scripts;
45
+ - **by an AI assistant**: `--questions` prints the interview as JSON, the assistant asks the user
46
+ in its own chat and runs the command with their answers.
47
+
48
+ ```python
49
+ from cli_interview_kit import App, Command, Interview, Question
50
+
51
+ NEW = Command(
52
+ name="new",
53
+ help="Create a project.",
54
+ interview=Interview(
55
+ questions=(
56
+ Question(
57
+ key="name",
58
+ section="project",
59
+ kind="text",
60
+ prompt="Project name",
61
+ help="The project's name.",
62
+ required=True,
63
+ ),
64
+ Question(
65
+ key="template",
66
+ section="project",
67
+ kind="select",
68
+ prompt="Template",
69
+ help="What the project starts from.",
70
+ choices=("library", "service"),
71
+ default="library",
72
+ ),
73
+ ),
74
+ ),
75
+ run=lambda invocation: print(invocation.answers),
76
+ )
77
+
78
+ APP = App(name="projects", description="Create projects.", version="1.0.0", commands=(NEW,))
79
+
80
+ if __name__ == "__main__":
81
+ raise SystemExit(APP.main())
82
+ ```
83
+
84
+ ```console
85
+ $ projects new # a person at a terminal is interviewed
86
+ ? Project name demo
87
+ ? Template service
88
+ {'name': 'demo', 'template': 'service'}
89
+
90
+ $ projects new --yes --name demo # nobody to ask: flags and defaults
91
+ {'name': 'demo', 'template': 'library'}
92
+
93
+ $ projects new --yes # an answer with no safe default fails
94
+ ERROR --name is required: it has no safe default, so pass it when the run cannot prompt.
95
+
96
+ $ projects new --questions # what an AI assistant reads
97
+ { "version": 1, "command": "projects new --yes", "procedure": [...], "questions": [...] }
98
+ ```
99
+
100
+ **Status: alpha (`0.x`).** The API may change between minor versions; see the
101
+ [changelog](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/CHANGELOG.md).
102
+
103
+ ## Install
104
+
105
+ ```bash
106
+ uv add cli-interview-kit
107
+ # or: pip install cli-interview-kit
108
+ ```
109
+
110
+ Requires Python 3.11 or newer, on Linux, macOS or Windows. It builds on
111
+ [argparse](https://docs.python.org/3/library/argparse.html),
112
+ [questionary](https://github.com/tmbo/questionary) and [rich](https://github.com/Textualize/rich).
113
+
114
+ A complete example using every feature is in
115
+ [`examples/projects.py`](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/examples/projects.py).
116
+ From a clone, run `just example new` or `just example new --questions`.
117
+
118
+ ## Concepts
119
+
120
+ | You declare | What it is |
121
+ |---|---|
122
+ | `App` | The program: its name, version and commands. `App.main()` parses, runs and returns the exit code. |
123
+ | `Command` | One command: plain `Option`s, an `Interview`, a `run` function, and any subcommands. |
124
+ | `Interview` | The command's `Question`s in order, the `Consent`s it may ask for, and the sources of dynamic choices and defaults. |
125
+ | `Question` | One answer the command needs. Its flag is derived from its key (`max_workers` is `--max-workers`). |
126
+ | `Consent` | A confirmation before something hard to undo, such as deleting. Given by `--yes`, never by a default. |
127
+ | `Option` | A flag that chooses what the command does (`--dry-run`) rather than an answer the user could be asked. |
128
+
129
+ The run function receives an `Invocation` with the `answers`, the `options`, the `console` to
130
+ print and ask through, and the `interaction` to pass to `Consent.ask`.
131
+
132
+ ### Question kinds
133
+
134
+ | `kind` | Answer | Flag |
135
+ |---|---|---|
136
+ | `text` | `str` | `--flag VALUE` |
137
+ | `select` | one of `choices` | `--flag CHOICE` |
138
+ | `multiselect` | `list[str]`; every choice if omitted | repeat `--flag CHOICE` |
139
+ | `confirm` | `bool` | `--flag` or `--no-flag` |
140
+ | `int` | `int` or `None` | `--flag NUMBER` |
141
+ | `list` | `list[str]` | repeat `--flag VALUE` |
142
+ | `pairs` | `dict[str, str]` | repeat `--flag KEY=VALUE` |
143
+ | `per_item` | `dict[str, str]`, one value per item of a source | repeat `--flag ITEM=VALUE` |
144
+ | `custom` | whatever your `CustomKind` parses | your `CustomKind.flag_form` |
145
+
146
+ A question can also have:
147
+
148
+ - **conditions**: `when_all=(when("template", "service"),)` asks it only when an earlier answer,
149
+ an option or a derived fact matches. A flag for a question that does not apply fails the run.
150
+ - **a default**, which may reference earlier answers (`default="{name}_pkg"`), or a
151
+ `default_from` source. `required=True` means it has no safe default.
152
+ - **a validator**: any function that returns the value or raises `ValueError` with a message.
153
+ Built in: `non_empty`, `whole_number`, `byte_size`.
154
+ - **dynamic values**: `choices_from`, `items_from` and `suggestions_from` name a function in
155
+ `Interview.choices` that returns the current values. `suggestions_from` lists values under a
156
+ free-text input as you type, without enforcing them.
157
+ - **a group**: questions with safe defaults can share a `group`, so an assistant asks "keep these
158
+ defaults?" once instead of asking each.
159
+
160
+ Mistakes in a declaration, such as a `select` without choices or a reference to an undeclared
161
+ source, raise pydantic's `ValidationError` when the module is imported, not when a user runs the
162
+ command.
163
+
164
+ ## Who answers
165
+
166
+ Each run decides once whether it may prompt: it may when stdin is a terminal and `--yes` was not
167
+ given.
168
+
169
+ - **At a terminal**, each applicable question its flag did not answer is asked. An invalid answer
170
+ shows its error under the input and is asked again.
171
+ - **When nobody can be asked** (`--yes`, CI, a pipe), each question takes its default. One with
172
+ no safe default fails the run, naming its flag.
173
+ - **Consents** are given by `--yes`. Without it they are asked at a terminal; with nobody to ask,
174
+ a consent with a `safe_default` takes it, and any other fails the run, asking for `--yes`. A
175
+ piped stdin never consents by itself. Asking for a consent the command did not declare raises
176
+ `DeclarationError`, so `--questions` always lists every consent.
177
+
178
+ ## AI assistants
179
+
180
+ `--questions` prints the command's interview as a JSON document: every question with its
181
+ choices, defaults, conditions and current local values, the groups, the consents, the plain
182
+ options, and a `procedure` for the assistant to follow. Any assistant that can run a shell
183
+ command can use it, whichever product it is:
184
+
185
+ 1. It runs `projects new --questions`.
186
+ 2. It asks the user the questions in its own chat, skipping those whose conditions do not hold,
187
+ and describes each consent.
188
+ 3. It runs `projects new --yes --name demo --template service`. If the command rejects a value,
189
+ it asks that question again.
190
+
191
+ Point your assistant at the command in your repository's agent instructions, for example: "To
192
+ create a project, run `projects new --questions` and follow its `procedure`." The document has a
193
+ `version`, raised when a field changes meaning or is removed.
194
+
195
+ ## Output and errors
196
+
197
+ `Console.print` writes a command's result to stdout. Status lines (`info`, `success`, `warning`,
198
+ `error`) and progress bars go to stderr, so output can be piped. Status lines are plain text:
199
+ values a user typed are never interpreted as rich markup.
200
+
201
+ | Outcome | Exit code |
202
+ |---|---|
203
+ | Success | 0 |
204
+ | `CliError`, or an exception type listed in `App.expected_errors`: its message | 1 |
205
+ | Any other exception: one line naming it, or the traceback with `--debug` | 1 |
206
+ | Ctrl-C: `Aborted.` | 130 |
207
+ | Usage error (unknown flag, invalid choice) | 2 |
208
+
209
+ ## More
210
+
211
+ - [Requirements](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/docs/requirements.md):
212
+ what the package must do, what it deliberately does not, and how each is verified.
213
+ - [Design](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/docs/design.md):
214
+ how it works, the module map, decisions and trade-offs.
215
+ - [Contributing](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/CONTRIBUTING.md):
216
+ setup, checks and the release process.
217
+ - [Security](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/SECURITY.md):
218
+ how to report a vulnerability.
219
+
220
+ ## License
221
+
222
+ MIT, see [LICENSE](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE).
@@ -0,0 +1,190 @@
1
+ # cli-interview-kit
2
+
3
+ [![CI](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/cli-interview-kit)](https://pypi.org/project/cli-interview-kit/)
5
+ [![Python](https://img.shields.io/pypi/pyversions/cli-interview-kit)](https://pypi.org/project/cli-interview-kit/)
6
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE)
7
+
8
+ Build Python command-line tools whose prompts are declared once and answered in three ways:
9
+
10
+ - **at the terminal**, as an interview that validates every answer and asks again until it is
11
+ right;
12
+ - **by flags**, because every question has one, so the same command runs in CI and scripts;
13
+ - **by an AI assistant**: `--questions` prints the interview as JSON, the assistant asks the user
14
+ in its own chat and runs the command with their answers.
15
+
16
+ ```python
17
+ from cli_interview_kit import App, Command, Interview, Question
18
+
19
+ NEW = Command(
20
+ name="new",
21
+ help="Create a project.",
22
+ interview=Interview(
23
+ questions=(
24
+ Question(
25
+ key="name",
26
+ section="project",
27
+ kind="text",
28
+ prompt="Project name",
29
+ help="The project's name.",
30
+ required=True,
31
+ ),
32
+ Question(
33
+ key="template",
34
+ section="project",
35
+ kind="select",
36
+ prompt="Template",
37
+ help="What the project starts from.",
38
+ choices=("library", "service"),
39
+ default="library",
40
+ ),
41
+ ),
42
+ ),
43
+ run=lambda invocation: print(invocation.answers),
44
+ )
45
+
46
+ APP = App(name="projects", description="Create projects.", version="1.0.0", commands=(NEW,))
47
+
48
+ if __name__ == "__main__":
49
+ raise SystemExit(APP.main())
50
+ ```
51
+
52
+ ```console
53
+ $ projects new # a person at a terminal is interviewed
54
+ ? Project name demo
55
+ ? Template service
56
+ {'name': 'demo', 'template': 'service'}
57
+
58
+ $ projects new --yes --name demo # nobody to ask: flags and defaults
59
+ {'name': 'demo', 'template': 'library'}
60
+
61
+ $ projects new --yes # an answer with no safe default fails
62
+ ERROR --name is required: it has no safe default, so pass it when the run cannot prompt.
63
+
64
+ $ projects new --questions # what an AI assistant reads
65
+ { "version": 1, "command": "projects new --yes", "procedure": [...], "questions": [...] }
66
+ ```
67
+
68
+ **Status: alpha (`0.x`).** The API may change between minor versions; see the
69
+ [changelog](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/CHANGELOG.md).
70
+
71
+ ## Install
72
+
73
+ ```bash
74
+ uv add cli-interview-kit
75
+ # or: pip install cli-interview-kit
76
+ ```
77
+
78
+ Requires Python 3.11 or newer, on Linux, macOS or Windows. It builds on
79
+ [argparse](https://docs.python.org/3/library/argparse.html),
80
+ [questionary](https://github.com/tmbo/questionary) and [rich](https://github.com/Textualize/rich).
81
+
82
+ A complete example using every feature is in
83
+ [`examples/projects.py`](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/examples/projects.py).
84
+ From a clone, run `just example new` or `just example new --questions`.
85
+
86
+ ## Concepts
87
+
88
+ | You declare | What it is |
89
+ |---|---|
90
+ | `App` | The program: its name, version and commands. `App.main()` parses, runs and returns the exit code. |
91
+ | `Command` | One command: plain `Option`s, an `Interview`, a `run` function, and any subcommands. |
92
+ | `Interview` | The command's `Question`s in order, the `Consent`s it may ask for, and the sources of dynamic choices and defaults. |
93
+ | `Question` | One answer the command needs. Its flag is derived from its key (`max_workers` is `--max-workers`). |
94
+ | `Consent` | A confirmation before something hard to undo, such as deleting. Given by `--yes`, never by a default. |
95
+ | `Option` | A flag that chooses what the command does (`--dry-run`) rather than an answer the user could be asked. |
96
+
97
+ The run function receives an `Invocation` with the `answers`, the `options`, the `console` to
98
+ print and ask through, and the `interaction` to pass to `Consent.ask`.
99
+
100
+ ### Question kinds
101
+
102
+ | `kind` | Answer | Flag |
103
+ |---|---|---|
104
+ | `text` | `str` | `--flag VALUE` |
105
+ | `select` | one of `choices` | `--flag CHOICE` |
106
+ | `multiselect` | `list[str]`; every choice if omitted | repeat `--flag CHOICE` |
107
+ | `confirm` | `bool` | `--flag` or `--no-flag` |
108
+ | `int` | `int` or `None` | `--flag NUMBER` |
109
+ | `list` | `list[str]` | repeat `--flag VALUE` |
110
+ | `pairs` | `dict[str, str]` | repeat `--flag KEY=VALUE` |
111
+ | `per_item` | `dict[str, str]`, one value per item of a source | repeat `--flag ITEM=VALUE` |
112
+ | `custom` | whatever your `CustomKind` parses | your `CustomKind.flag_form` |
113
+
114
+ A question can also have:
115
+
116
+ - **conditions**: `when_all=(when("template", "service"),)` asks it only when an earlier answer,
117
+ an option or a derived fact matches. A flag for a question that does not apply fails the run.
118
+ - **a default**, which may reference earlier answers (`default="{name}_pkg"`), or a
119
+ `default_from` source. `required=True` means it has no safe default.
120
+ - **a validator**: any function that returns the value or raises `ValueError` with a message.
121
+ Built in: `non_empty`, `whole_number`, `byte_size`.
122
+ - **dynamic values**: `choices_from`, `items_from` and `suggestions_from` name a function in
123
+ `Interview.choices` that returns the current values. `suggestions_from` lists values under a
124
+ free-text input as you type, without enforcing them.
125
+ - **a group**: questions with safe defaults can share a `group`, so an assistant asks "keep these
126
+ defaults?" once instead of asking each.
127
+
128
+ Mistakes in a declaration, such as a `select` without choices or a reference to an undeclared
129
+ source, raise pydantic's `ValidationError` when the module is imported, not when a user runs the
130
+ command.
131
+
132
+ ## Who answers
133
+
134
+ Each run decides once whether it may prompt: it may when stdin is a terminal and `--yes` was not
135
+ given.
136
+
137
+ - **At a terminal**, each applicable question its flag did not answer is asked. An invalid answer
138
+ shows its error under the input and is asked again.
139
+ - **When nobody can be asked** (`--yes`, CI, a pipe), each question takes its default. One with
140
+ no safe default fails the run, naming its flag.
141
+ - **Consents** are given by `--yes`. Without it they are asked at a terminal; with nobody to ask,
142
+ a consent with a `safe_default` takes it, and any other fails the run, asking for `--yes`. A
143
+ piped stdin never consents by itself. Asking for a consent the command did not declare raises
144
+ `DeclarationError`, so `--questions` always lists every consent.
145
+
146
+ ## AI assistants
147
+
148
+ `--questions` prints the command's interview as a JSON document: every question with its
149
+ choices, defaults, conditions and current local values, the groups, the consents, the plain
150
+ options, and a `procedure` for the assistant to follow. Any assistant that can run a shell
151
+ command can use it, whichever product it is:
152
+
153
+ 1. It runs `projects new --questions`.
154
+ 2. It asks the user the questions in its own chat, skipping those whose conditions do not hold,
155
+ and describes each consent.
156
+ 3. It runs `projects new --yes --name demo --template service`. If the command rejects a value,
157
+ it asks that question again.
158
+
159
+ Point your assistant at the command in your repository's agent instructions, for example: "To
160
+ create a project, run `projects new --questions` and follow its `procedure`." The document has a
161
+ `version`, raised when a field changes meaning or is removed.
162
+
163
+ ## Output and errors
164
+
165
+ `Console.print` writes a command's result to stdout. Status lines (`info`, `success`, `warning`,
166
+ `error`) and progress bars go to stderr, so output can be piped. Status lines are plain text:
167
+ values a user typed are never interpreted as rich markup.
168
+
169
+ | Outcome | Exit code |
170
+ |---|---|
171
+ | Success | 0 |
172
+ | `CliError`, or an exception type listed in `App.expected_errors`: its message | 1 |
173
+ | Any other exception: one line naming it, or the traceback with `--debug` | 1 |
174
+ | Ctrl-C: `Aborted.` | 130 |
175
+ | Usage error (unknown flag, invalid choice) | 2 |
176
+
177
+ ## More
178
+
179
+ - [Requirements](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/docs/requirements.md):
180
+ what the package must do, what it deliberately does not, and how each is verified.
181
+ - [Design](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/docs/design.md):
182
+ how it works, the module map, decisions and trade-offs.
183
+ - [Contributing](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/CONTRIBUTING.md):
184
+ setup, checks and the release process.
185
+ - [Security](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/SECURITY.md):
186
+ how to report a vulnerability.
187
+
188
+ ## License
189
+
190
+ MIT, see [LICENSE](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE).
@@ -0,0 +1,92 @@
1
+ # Design
2
+
3
+ ## How it works
4
+
5
+ ```
6
+ App.main(argv)
7
+ -> build_parser(): one argparse subparser per Command
8
+ Option -> a plain flag or positional
9
+ Question -> a flag derived from its key, grouped by section
10
+ interactive -> --yes and --questions
11
+ -> parse_args
12
+ -> --questions? Interview.document() -> JSON on stdout, exit 0
13
+ -> Interaction.resolve(--yes, stdin is a terminal, declared consents)
14
+ -> Interview.run(): for each question in order
15
+ does it apply? conditions over options, earlier answers and facts
16
+ flag given? validate it, or fail naming the flag
17
+ can prompt? ask, validating at the prompt until it passes
18
+ otherwise its default, or fail if it has no safe default
19
+ -> Command.run(Invocation(console, interaction, answers, options))
20
+ Consent.ask(...) --yes, a person, the safe default, or fail
21
+ -> one error policy -> exit code
22
+ ```
23
+
24
+ ## Modules
25
+
26
+ | Module | Responsibility |
27
+ |---|---|
28
+ | `app.py` | `App`: builds the parser from the commands, dispatches, and owns the error policy and exit codes. |
29
+ | `command.py` | `Command`, `Option` and `Invocation`: what a command is and what its run receives. Checks names do not clash. |
30
+ | `interview.py` | `Question`, `Condition`/`when`, `CustomKind`, `Interview`, and the engine answering questions from flags, prompts or defaults. Renders `--questions`. |
31
+ | `interaction.py` | `Interaction` (may the run prompt; did `--yes` consent; which consents are declared) and `Consent`. |
32
+ | `console.py` | `Console`: output to stdout and stderr, and the prompts. |
33
+ | `validators.py` | The `Validator` shape and the built-in validators. |
34
+ | `errors.py` | `CliError` (the user's problem) and `DeclarationError` (the app author's). |
35
+ | `_prompts.py` | Prompt loops for answers with several items, and `KEY=VALUE` parsing. |
36
+ | `_suggest.py` | The free-text prompt listing suggestions under the input. |
37
+ | `_help.py` | Rich-rendered help and usage. |
38
+ | `_model.py` | The frozen pydantic base of every declaration. |
39
+
40
+ Everything public is exported from `cli_interview_kit`; modules starting with an underscore are
41
+ internal and may change in any release.
42
+
43
+ ## Decisions
44
+
45
+ - **Declarations are data.** A command is a frozen pydantic model, not decorated functions. The
46
+ same declaration then yields the parser, the interview, the unattended behaviour and the
47
+ `--questions` document, and pydantic validates it when it is built. *Rejected:* decorators in
48
+ the style of Click or Typer, which spread a command's prompts through its code where neither
49
+ the framework nor an assistant can list them.
50
+ - **argparse, not Click or Typer.** The parser is generated, so argparse's standard-library
51
+ stability matters more than a nicer authoring API, and it adds no dependency.
52
+ - **questionary for prompts, plus one custom prompt.** questionary covers select, checkbox,
53
+ confirm and text. Free text with suggestions is a small prompt_toolkit application
54
+ (`_suggest.py`) that lists matches as plain lines under the input; prompt_toolkit's own
55
+ completion menu floats in a framed box at the cursor and was rejected as hard to read.
56
+ - **Answers and consents are different things.** An answer has a default and is safe to take
57
+ unattended; a consent allows something hard to undo and is never given by a default unless the
58
+ author declares a safe one. A piped stdin never consents, so `yes | tool delete` does not
59
+ delete.
60
+ - **The assistant procedure travels with the document.** `--questions` carries the `procedure`
61
+ and `rules`, so the behaviour is the same in every assistant and nothing has to be installed
62
+ per assistant or per repository.
63
+ - **Validators are plain functions** of the form `(str) -> str` raising `ValueError`. The same
64
+ function checks a prompt answer (shown under the input) and a flag value (fails the run).
65
+ - **Status on stderr, plain text.** Results stay pipeable, and a user's input cannot be read as
66
+ rich markup. Status lines are not hard-wrapped, so a log or an assistant sees one line per
67
+ message.
68
+
69
+ ## Trade-offs
70
+
71
+ - **Answers are a `dict[str, Any]`** keyed by question key, not a typed object. Typed answers
72
+ would need a model per command; a key typo fails at run time with a `KeyError`.
73
+ - **The help renderer reads argparse's underscored attributes** (`_actions`, `_action_groups`).
74
+ They have been stable for many releases, and CI runs the help tests on the oldest and newest
75
+ supported Python to catch a change.
76
+ - **Dynamic sources run when needed.** `choices_from` and similar sources are called during the
77
+ interview, and by `--questions` only when listed in `offline`, so a slow or remote source never
78
+ delays the document.
79
+ - **Conditions on facts are checked at run time.** Facts are computed by a function, so a
80
+ condition naming a fact that does not exist is not caught when the declaration is built; it
81
+ simply never holds.
82
+
83
+ ## Extending
84
+
85
+ - **A new question kind** that most apps need belongs in `QuestionKind`, `HOW_TO_ANSWER`, the
86
+ kind rules in `Question._check_kind`, `Interview.add_arguments` and `_Asker`. One app's own
87
+ kind should be a `CustomKind` instead.
88
+ - **A new field in `--questions`** is additive and needs no version change; changing or removing
89
+ one raises `QUESTIONS_FORMAT_VERSION`, and the snapshot test shows the difference.
90
+
91
+ Anything that adds a runtime dependency, an option kept "just in case", or a second way to do an
92
+ existing thing should be discussed in an issue first (see CONTRIBUTING.md).
@@ -0,0 +1,58 @@
1
+ # Requirements
2
+
3
+ What this project must do, the conditions it works under, and what it deliberately does not do.
4
+ Each requirement says where it is verified, so a change that breaks one fails a test, not a user.
5
+
6
+ ## Purpose
7
+
8
+ Let a team build command-line tools whose every prompt is **declared once** and can be answered
9
+ by a person at a terminal, by flags in scripts and CI, or by an AI assistant interviewing the
10
+ user in its own chat, with the same validation and the same safety rules in all three.
11
+
12
+ ## Functional requirements
13
+
14
+ | ID | Requirement | Verified by |
15
+ |---|---|---|
16
+ | F1 | Every question has a flag derived from its key, so every command runs from flags alone. | `tests/test_unattended.py` |
17
+ | F2 | At a terminal, each applicable question its flag did not answer is asked; an invalid answer shows its error and is asked again. | `tests/test_terminal.py`, `tests/test_suggest.py` |
18
+ | F3 | When nobody can be asked (`--yes`, a non-terminal stdin), each question takes its default, and one with no safe default fails naming its flag. | `test_a_required_answer_nobody_gives_fails_naming_its_flag` |
19
+ | F4 | A consent is given only by `--yes` or a person; with nobody to ask it takes its safe default or fails asking for `--yes`. A command cannot ask for a consent it did not declare. | `test_yes_grants_consent`, `test_without_yes_a_consent_nobody_can_give_fails`, `test_an_undeclared_consent_is_a_declaration_error` |
20
+ | F5 | Questions apply conditionally on earlier answers, options and facts; a flag for a question that does not apply fails the run. | `test_conditions_see_options_and_facts`, `test_a_flag_for_a_question_that_does_not_apply_fails` |
21
+ | F6 | `--questions` prints a versioned JSON document from which any AI assistant can interview the user and run the command: questions, local choices and suggestions, groups, consents, options and a procedure. | `tests/test_questions_document.py` (snapshot) |
22
+ | F7 | A declaration that cannot work is rejected when it is built, naming the question and the problem. | `tests/test_declarations.py` |
23
+ | F8 | One error policy with fixed exit codes: 0 success, 1 failure, 2 usage error, 130 interrupted; `--debug` shows the traceback. | `tests/test_app.py` |
24
+ | F9 | Results go to stdout and status lines to stderr; user input in a status line is never read as markup. | `tests/test_console.py`, `test_messages_are_never_read_as_markup` |
25
+
26
+ ## Non-functional requirements
27
+
28
+ | ID | Requirement | Verified by |
29
+ |---|---|---|
30
+ | N1 | Works on macOS, Linux and Windows; Python 3.11 and newer. | CI matrix (3 OSes × Python 3.11, 3.14) |
31
+ | N2 | The declared dependency lower bounds work, not just the locked versions. | CI `test-lowest` |
32
+ | N3 | Four runtime dependencies only: pydantic, questionary, prompt-toolkit and rich. Another needs a strong case. | Review (`pyproject.toml`) |
33
+ | N4 | The package builds, renders on PyPI and imports from a clean environment; it is typed (`py.typed`). | CI `build` (`twine check --strict`) |
34
+ | N5 | 95% or more test coverage; warnings in tests are errors; the README and the example are executed by the tests. | CI `test`, `tests/test_readme.py` |
35
+ | N6 | Source passes strict type checking; workflows pass `actionlint` and `zizmor`; actions are pinned to commit SHAs. | CI `lint` |
36
+
37
+ ## Environment and constraints
38
+
39
+ - **Terminals:** prompts need a terminal prompt_toolkit supports, which includes the usual ones on
40
+ macOS, Linux and Windows. Without one, the command runs from flags and defaults.
41
+ - **Assistants:** any assistant that can run a shell command and read its output can follow
42
+ `--questions`; nothing in the package is specific to one assistant.
43
+ - **Repository rules** (managed outside this repo): a code owner must approve every pull
44
+ request, merges are squash-only, `ci-required` must pass, and tags matching `v*` are
45
+ protected. Releases are tags on `main`, published to PyPI and as a GitHub Release (see
46
+ CONTRIBUTING.md).
47
+
48
+ ## Out of scope
49
+
50
+ A replacement for argparse's parsing itself; shell completion; configuration files or
51
+ environment-variable defaults; translations; asynchronous commands; typed answer objects (answers
52
+ are a `dict` keyed by question key). Each can be proposed in an issue.
53
+
54
+ ## Verification status
55
+
56
+ Every requirement above is covered by the test suite and CI. The prompts are tested through
57
+ prompt_toolkit's pipe input, which checks answers and keys but not how a prompt looks; visual
58
+ changes are checked by hand in a real terminal.