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.
File without changes
@@ -0,0 +1,53 @@
1
+ """Answer validators: functions that return the value or raise ``ValueError``.
2
+
3
+ A validator receives the answer as typed and returns it, or raises
4
+ ``ValueError`` with a message for the user. At a prompt the message is shown
5
+ under the input and the question is asked again; a flag value that fails
6
+ validation fails the run, naming the flag. Any function with this shape is a
7
+ validator; these are the built-in ones.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import re
13
+ from collections.abc import Callable
14
+
15
+ Validator = Callable[[str], str]
16
+ """``(value) -> value``, raising ``ValueError`` with a user-facing message."""
17
+
18
+ _BYTE_SIZE = re.compile(r"\d+[kmgt]?b?")
19
+
20
+
21
+ def non_empty(value: str) -> str:
22
+ """Reject a blank answer."""
23
+ if not value.strip():
24
+ raise ValueError("A value is required.")
25
+ return value
26
+
27
+
28
+ def whole_number(value: str) -> str:
29
+ """Accept a whole number, or a blank answer."""
30
+ stripped = value.strip()
31
+ if stripped and not stripped.removeprefix("-").isdigit():
32
+ raise ValueError("Enter a whole number, or leave it blank.")
33
+ return value
34
+
35
+
36
+ def byte_size(value: str) -> str:
37
+ """Accept a size such as ``512m`` or ``1g``, or a blank answer."""
38
+ stripped = value.strip()
39
+ if stripped and not _BYTE_SIZE.fullmatch(stripped.lower()):
40
+ raise ValueError("Enter a size such as 512m or 1g, or leave it blank.")
41
+ return value
42
+
43
+
44
+ def all_of(*validators: Validator | None) -> Validator:
45
+ """Return a validator applying each given one in order, skipping ``None``."""
46
+
47
+ def _validate(value: str) -> str:
48
+ for validator in validators:
49
+ if validator is not None:
50
+ _ = validator(value)
51
+ return value
52
+
53
+ return _validate
@@ -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,17 @@
1
+ cli_interview_kit/__init__.py,sha256=HeH9HsAOKBR6rZrkuCDmF7mRbvsBuzxdwLZkSCzOH9k,1609
2
+ cli_interview_kit/_help.py,sha256=982dNrc-5VxvFlnChPiNbGUQgT_5LHr21neH8Zv_bC8,4388
3
+ cli_interview_kit/_model.py,sha256=Glk_sFtYanK6sG7EsbxBEx4xmzX5-ewA_rNhPO_oyDI,462
4
+ cli_interview_kit/_prompts.py,sha256=E9GrWWcTqQzwZDaG3BWwtLtfRXGu6m3EeQqcrsOQASU,2646
5
+ cli_interview_kit/_suggest.py,sha256=B_PBl5l0S6lUH6BepGtSyI3KLuQ51X4OsSxKafm46BA,6793
6
+ cli_interview_kit/app.py,sha256=2sxXeXqO5jcmIQTFrpPHieXYpP-LNdm2WYTsD88Vb0M,8705
7
+ cli_interview_kit/command.py,sha256=7sPHcWV0l5ZmyycGeBGcbuABOptaSyJny36aluNch-A,6114
8
+ cli_interview_kit/console.py,sha256=3XD1yRl0rKj1IPPewwC3u-PDg6H63t60ZqAyUW_7ISg,9251
9
+ cli_interview_kit/errors.py,sha256=jNys-p3K0bVUxtLIhbIBvMHCy-vdeaUlqlk_O-_PiHM,702
10
+ cli_interview_kit/interaction.py,sha256=5SqpFm69xUSAa0VsxSpVHsSZpfvYpQH6aebd1Y5n_vw,3688
11
+ cli_interview_kit/interview.py,sha256=eZsjLYPA3rWGAPLtSh7_YTJXf-kaRgdtpQc_LMFkXFc,37105
12
+ cli_interview_kit/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ cli_interview_kit/validators.py,sha256=2M8HM_0ix5JsJZuCoKpVkvZyf2pQKN4flSipHE9d3UA,1717
14
+ cli_interview_kit-0.1.0.dist-info/METADATA,sha256=lVPIW8k3vJf84cJnY26pLhEMDGEDJiVajK8ZJcFxLmo,10458
15
+ cli_interview_kit-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
16
+ cli_interview_kit-0.1.0.dist-info/licenses/LICENSE,sha256=omg-klCAZJHC5HKL1tynLFBkmFb6gsysszTxvhElGXs,1076
17
+ cli_interview_kit-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.