cli-interview-kit 0.1.0__tar.gz → 0.1.2__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.
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/CHANGELOG.md +24 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/PKG-INFO +3 -3
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/README.md +2 -2
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/pyproject.toml +8 -2
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/_model.py +3 -5
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/_prompts.py +14 -6
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/_suggest.py +4 -22
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/app.py +33 -15
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/command.py +33 -19
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/console.py +85 -41
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/interaction.py +38 -12
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/interview.py +149 -54
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/validators.py +48 -3
- cli_interview_kit-0.1.2/tests/test_docstrings.py +199 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/.gitignore +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/LICENSE +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/docs/design.md +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/docs/requirements.md +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/examples/projects.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/__init__.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/_help.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/errors.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/src/cli_interview_kit/py.typed +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/conftest.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/helpers.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/snapshots/projects_new_questions.json +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_app.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_console.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_declarations.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_help.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_questions_document.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_readme.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_suggest.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_terminal.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_unattended.py +0 -0
- {cli_interview_kit-0.1.0 → cli_interview_kit-0.1.2}/tests/test_validators.py +0 -0
|
@@ -7,6 +7,30 @@ version).
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.2] - 2026-11-27
|
|
11
|
+
|
|
12
|
+
No code changes.
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- The README's PyPI and Python-version badges render on the PyPI page again; they now use URLs
|
|
17
|
+
that bypass a stale cached error.
|
|
18
|
+
|
|
19
|
+
## [0.1.1] - 2026-11-27
|
|
20
|
+
|
|
21
|
+
No behavior changes.
|
|
22
|
+
|
|
23
|
+
### Added
|
|
24
|
+
|
|
25
|
+
- A Code of Conduct.
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- Docstrings follow the NumPy convention (parameters, returns, raises, attributes and methods,
|
|
30
|
+
without types) and are checked in CI.
|
|
31
|
+
- CI now lints for common security issues (ruff's `S`/bandit rules), matching the scrutiny a
|
|
32
|
+
package installed by third parties warrants.
|
|
33
|
+
|
|
10
34
|
## [0.1.0] - 2026-11-27
|
|
11
35
|
|
|
12
36
|
First release.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: cli-interview-kit
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Python CLIs whose prompts are declared once and answered at the terminal, by flags, or by an AI assistant.
|
|
5
5
|
Project-URL: Homepage, https://github.com/Northbridge-Capital/cli-interview-kit
|
|
6
6
|
Project-URL: Source, https://github.com/Northbridge-Capital/cli-interview-kit
|
|
@@ -33,8 +33,8 @@ Description-Content-Type: text/markdown
|
|
|
33
33
|
# cli-interview-kit
|
|
34
34
|
|
|
35
35
|
[](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml)
|
|
36
|
-
[](https://pypi.org/project/cli-interview-kit/)
|
|
37
|
-
[](https://pypi.org/project/cli-interview-kit/)
|
|
36
|
+
[](https://pypi.org/project/cli-interview-kit/)
|
|
37
|
+
[](https://pypi.org/project/cli-interview-kit/)
|
|
38
38
|
[](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE)
|
|
39
39
|
|
|
40
40
|
Build Python command-line tools whose prompts are declared once and answered in three ways:
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# cli-interview-kit
|
|
2
2
|
|
|
3
3
|
[](https://github.com/Northbridge-Capital/cli-interview-kit/actions/workflows/ci.yml)
|
|
4
|
-
[](https://pypi.org/project/cli-interview-kit/)
|
|
5
|
-
[](https://pypi.org/project/cli-interview-kit/)
|
|
4
|
+
[](https://pypi.org/project/cli-interview-kit/)
|
|
5
|
+
[](https://pypi.org/project/cli-interview-kit/)
|
|
6
6
|
[](https://github.com/Northbridge-Capital/cli-interview-kit/blob/main/LICENSE)
|
|
7
7
|
|
|
8
8
|
Build Python command-line tools whose prompts are declared once and answered in three ways:
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "cli-interview-kit"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "Python CLIs whose prompts are declared once and answered at the terminal, by flags, or by an AI assistant."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -59,7 +59,13 @@ line-length = 100
|
|
|
59
59
|
target-version = "py311"
|
|
60
60
|
|
|
61
61
|
[tool.ruff.lint]
|
|
62
|
-
select = ["E", "F", "W", "I", "B", "UP", "DTZ", "SIM", "PT", "RUF", "C4", "PIE"]
|
|
62
|
+
select = ["E", "F", "W", "I", "B", "UP", "DTZ", "S", "SIM", "PT", "RUF", "C4", "PIE", "D"]
|
|
63
|
+
|
|
64
|
+
[tool.ruff.lint.pydocstyle]
|
|
65
|
+
convention = "numpy"
|
|
66
|
+
|
|
67
|
+
[tool.ruff.lint.per-file-ignores]
|
|
68
|
+
"tests/**/*.py" = ["D", "S101", "S102"]
|
|
63
69
|
|
|
64
70
|
[tool.ruff.format]
|
|
65
71
|
line-ending = "lf"
|
|
@@ -6,10 +6,8 @@ from pydantic import BaseModel, ConfigDict
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class FrozenModel(BaseModel):
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
Declarations hold callables (run functions, validators, choice sources),
|
|
12
|
-
so arbitrary types are allowed.
|
|
13
|
-
"""
|
|
9
|
+
"""An immutable pydantic base for every declaration and value object."""
|
|
14
10
|
|
|
11
|
+
# Declarations hold callables (run functions, validators, choice sources), so arbitrary
|
|
12
|
+
# types are allowed.
|
|
15
13
|
model_config = ConfigDict(frozen=True, arbitrary_types_allowed=True, extra="forbid")
|
|
@@ -12,10 +12,21 @@ from cli_interview_kit.validators import Validator, all_of, non_empty
|
|
|
12
12
|
def parse_kv(items: Sequence[str] | None, flag: str) -> dict[str, str]:
|
|
13
13
|
"""Parse repeated ``KEY=VALUE`` flag values into a dict.
|
|
14
14
|
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
items
|
|
18
|
+
The values of a repeatable flag, or ``None`` if it was not given.
|
|
19
|
+
flag
|
|
20
|
+
The flag's name, for error messages.
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
The values, by key.
|
|
25
|
+
|
|
15
26
|
Raises
|
|
16
27
|
------
|
|
17
28
|
CliError
|
|
18
|
-
|
|
29
|
+
A value has no ``=`` or an empty key.
|
|
19
30
|
"""
|
|
20
31
|
result: dict[str, str] = {}
|
|
21
32
|
for item in items or ():
|
|
@@ -69,11 +80,8 @@ def prompt_pairs(
|
|
|
69
80
|
|
|
70
81
|
|
|
71
82
|
def _keep_adding(console: Console, label: str, required: bool, collected: int) -> bool:
|
|
72
|
-
"""Return whether to ask for another item.
|
|
73
|
-
|
|
74
|
-
A required collection asks for its first item straight away, so it can
|
|
75
|
-
never end up empty.
|
|
76
|
-
"""
|
|
83
|
+
"""Return whether to ask for another item."""
|
|
84
|
+
# A required collection asks for its first item straight away, so it can never end up empty.
|
|
77
85
|
if collected:
|
|
78
86
|
return console.confirm("Add another?", default=False)
|
|
79
87
|
return required or console.confirm(f"Add {_with_article(label)}?", default=False)
|
|
@@ -47,6 +47,9 @@ class _State(BaseModel):
|
|
|
47
47
|
return found[:MAX_SHOWN]
|
|
48
48
|
|
|
49
49
|
|
|
50
|
+
# ``validate`` returns ``True`` when valid, else the message to show. ``style`` uses the ``qmark``,
|
|
51
|
+
# ``question``, ``answer``, ``pointer``, ``suggestion`` and ``validation-toolbar`` classes, like
|
|
52
|
+
# the other prompts. The answer is ``None`` when the user cancels with Ctrl-C.
|
|
50
53
|
def suggest_text(
|
|
51
54
|
prompt: str,
|
|
52
55
|
suggestions: Sequence[str],
|
|
@@ -55,28 +58,7 @@ def suggest_text(
|
|
|
55
58
|
validate: Callable[[str], bool | str] | None = None,
|
|
56
59
|
style: BaseStyle | None = None,
|
|
57
60
|
) -> str | None:
|
|
58
|
-
"""Ask for free text with suggestions listed under the input.
|
|
59
|
-
|
|
60
|
-
Parameters
|
|
61
|
-
----------
|
|
62
|
-
prompt : str
|
|
63
|
-
The question.
|
|
64
|
-
suggestions : Sequence[str]
|
|
65
|
-
Values listed while they match what is typed.
|
|
66
|
-
default : str, optional
|
|
67
|
-
Initial input.
|
|
68
|
-
validate : Callable[[str], bool | str] | None, optional
|
|
69
|
-
``True`` when valid, else the message to show.
|
|
70
|
-
style : BaseStyle | None, optional
|
|
71
|
-
Uses the ``qmark``, ``question``, ``answer``, ``pointer``,
|
|
72
|
-
``suggestion`` and ``validation-toolbar`` classes, like the other
|
|
73
|
-
prompts.
|
|
74
|
-
|
|
75
|
-
Returns
|
|
76
|
-
-------
|
|
77
|
-
str | None
|
|
78
|
-
The answer, or ``None`` when cancelled with Ctrl-C.
|
|
79
|
-
"""
|
|
61
|
+
"""Ask for free text with suggestions listed under the input."""
|
|
80
62
|
state = _State(suggestions=list(dict.fromkeys(suggestions)))
|
|
81
63
|
buffer = Buffer(document=Document(default, len(default)), multiline=False)
|
|
82
64
|
buffer.on_text_changed += lambda _buffer: _reset(state) # pyright: ignore[reportUnknownLambdaType]
|
|
@@ -37,21 +37,23 @@ _PATH = "_cik_path"
|
|
|
37
37
|
|
|
38
38
|
|
|
39
39
|
class _ArgumentParser(argparse.ArgumentParser):
|
|
40
|
-
"""An ``ArgumentParser`` whose help and usage render through rich.
|
|
40
|
+
"""An ``ArgumentParser`` whose help and usage render through rich."""
|
|
41
41
|
|
|
42
|
-
``add_subparsers()`` gives every subparser its parent's class, so this
|
|
43
|
-
|
|
44
|
-
"""
|
|
42
|
+
# ``add_subparsers()`` gives every subparser its parent's class, so this one subclass covers
|
|
43
|
+
# every command.
|
|
45
44
|
|
|
46
45
|
def print_help(self, file: object = None) -> None:
|
|
46
|
+
"""Print the help through rich, ignoring ``file``."""
|
|
47
47
|
_ = file
|
|
48
48
|
print_help(self)
|
|
49
49
|
|
|
50
50
|
def print_usage(self, file: object = None) -> None:
|
|
51
|
+
"""Print the usage line through rich, ignoring ``file``."""
|
|
51
52
|
_ = file
|
|
52
53
|
print_usage(self)
|
|
53
54
|
|
|
54
55
|
def error(self, message: str) -> NoReturn:
|
|
56
|
+
"""Print the usage and the message, then exit with status 2."""
|
|
55
57
|
print_usage(self)
|
|
56
58
|
Console().error(message)
|
|
57
59
|
self.exit(2)
|
|
@@ -62,17 +64,24 @@ class App(FrozenModel):
|
|
|
62
64
|
|
|
63
65
|
Attributes
|
|
64
66
|
----------
|
|
65
|
-
name
|
|
67
|
+
name
|
|
66
68
|
Program name.
|
|
67
|
-
description
|
|
69
|
+
description
|
|
68
70
|
Top-level help.
|
|
69
|
-
version
|
|
71
|
+
version
|
|
70
72
|
What ``--version`` prints after the name.
|
|
71
|
-
commands
|
|
73
|
+
commands
|
|
72
74
|
The top-level commands.
|
|
73
|
-
expected_errors
|
|
75
|
+
expected_errors
|
|
74
76
|
Further exception types whose message is reported like a
|
|
75
77
|
``CliError``, for failures the app's own libraries raise.
|
|
78
|
+
|
|
79
|
+
Methods
|
|
80
|
+
-------
|
|
81
|
+
main
|
|
82
|
+
Run the command ``argv`` selects and return the exit code.
|
|
83
|
+
build_parser
|
|
84
|
+
Return the argument parser for every declared command.
|
|
76
85
|
"""
|
|
77
86
|
|
|
78
87
|
name: str = Field(min_length=1)
|
|
@@ -94,14 +103,17 @@ class App(FrozenModel):
|
|
|
94
103
|
|
|
95
104
|
Parameters
|
|
96
105
|
----------
|
|
97
|
-
argv
|
|
106
|
+
argv
|
|
98
107
|
The arguments after the program name; ``sys.argv[1:]`` if omitted.
|
|
99
108
|
|
|
100
109
|
Returns
|
|
101
110
|
-------
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
The exit code: 0 on success, 1 on a failure, 130 when interrupted.
|
|
112
|
+
|
|
113
|
+
Raises
|
|
114
|
+
------
|
|
115
|
+
SystemExit
|
|
116
|
+
A usage error (an unknown flag, a bad choice) exits with status 2, as argparse does.
|
|
105
117
|
"""
|
|
106
118
|
console = Console()
|
|
107
119
|
args = self.build_parser().parse_args(argv)
|
|
@@ -127,7 +139,12 @@ class App(FrozenModel):
|
|
|
127
139
|
return 0
|
|
128
140
|
|
|
129
141
|
def build_parser(self) -> argparse.ArgumentParser:
|
|
130
|
-
"""Return the argument parser for every declared command.
|
|
142
|
+
"""Return the argument parser for every declared command.
|
|
143
|
+
|
|
144
|
+
Returns
|
|
145
|
+
-------
|
|
146
|
+
The parser, with ``--version``, ``--debug`` and one subparser per command.
|
|
147
|
+
"""
|
|
131
148
|
parser = _ArgumentParser(prog=self.name, description=self.description)
|
|
132
149
|
_ = parser.add_argument(
|
|
133
150
|
"--version", action="version", version=f"{self.name} {self.version}"
|
|
@@ -141,6 +158,7 @@ class App(FrozenModel):
|
|
|
141
158
|
return parser
|
|
142
159
|
|
|
143
160
|
def _dispatch(self, console: Console, values: dict[str, Any]) -> None:
|
|
161
|
+
"""Print the selected command's ``--questions``, or answer its interview and run it."""
|
|
144
162
|
command: Command = values[_COMMAND]
|
|
145
163
|
command_line = " ".join(values[_PATH])
|
|
146
164
|
options = {option.dest: values.get(option.dest) for option in command.options}
|
|
@@ -155,7 +173,7 @@ class App(FrozenModel):
|
|
|
155
173
|
)
|
|
156
174
|
answers = command.interview.run(console, interaction, values, options)
|
|
157
175
|
# A command without a run has subcommands, and argparse requires one.
|
|
158
|
-
assert command.run is not None
|
|
176
|
+
assert command.run is not None # noqa: S101 -- see the comment above
|
|
159
177
|
command.run(
|
|
160
178
|
Invocation(console=console, interaction=interaction, answers=answers, options=options)
|
|
161
179
|
)
|
|
@@ -38,24 +38,31 @@ class Option(FrozenModel):
|
|
|
38
38
|
|
|
39
39
|
Attributes
|
|
40
40
|
----------
|
|
41
|
-
dest
|
|
41
|
+
dest
|
|
42
42
|
The name the value is read by, in snake_case; the flag is ``--``
|
|
43
43
|
plus the name with dashes.
|
|
44
|
-
help
|
|
44
|
+
help
|
|
45
45
|
Help text.
|
|
46
|
-
kind
|
|
46
|
+
kind
|
|
47
47
|
``switch`` is an on/off flag; ``value`` takes one value; ``append``
|
|
48
48
|
is repeatable; ``positional`` is an argument.
|
|
49
|
-
metavar
|
|
49
|
+
metavar
|
|
50
50
|
Value placeholder in help.
|
|
51
|
-
choices
|
|
51
|
+
choices
|
|
52
52
|
Allowed values.
|
|
53
|
-
nargs
|
|
53
|
+
nargs
|
|
54
54
|
A positional's arity, e.g. ``"*"``.
|
|
55
|
-
convert
|
|
55
|
+
convert
|
|
56
56
|
Converts and checks a value; raising ``ValueError`` is a usage error.
|
|
57
|
-
exclusive
|
|
57
|
+
exclusive
|
|
58
58
|
Options sharing this name are mutually exclusive.
|
|
59
|
+
flag
|
|
60
|
+
The option's flag, or its name for a positional.
|
|
61
|
+
|
|
62
|
+
Methods
|
|
63
|
+
-------
|
|
64
|
+
describe
|
|
65
|
+
Describe the option as ``--questions`` lists it.
|
|
59
66
|
"""
|
|
60
67
|
|
|
61
68
|
dest: str = Field(pattern=r"^[a-z][a-z0-9_]*$")
|
|
@@ -86,7 +93,12 @@ class Option(FrozenModel):
|
|
|
86
93
|
return "--" + self.dest.replace("_", "-")
|
|
87
94
|
|
|
88
95
|
def describe(self) -> dict[str, Any]:
|
|
89
|
-
"""
|
|
96
|
+
"""Describe the option as ``--questions`` lists it.
|
|
97
|
+
|
|
98
|
+
Returns
|
|
99
|
+
-------
|
|
100
|
+
The option's flag, kind and help.
|
|
101
|
+
"""
|
|
90
102
|
return {"flag": self.flag, "kind": self.kind, "help": self.help}
|
|
91
103
|
|
|
92
104
|
|
|
@@ -95,14 +107,14 @@ class Invocation(FrozenModel):
|
|
|
95
107
|
|
|
96
108
|
Attributes
|
|
97
109
|
----------
|
|
98
|
-
console
|
|
110
|
+
console
|
|
99
111
|
Output and prompts.
|
|
100
|
-
interaction
|
|
112
|
+
interaction
|
|
101
113
|
Whether the run may prompt and has consent; pass it to
|
|
102
114
|
``Consent.ask``.
|
|
103
|
-
answers
|
|
115
|
+
answers
|
|
104
116
|
The answers of the questions that applied, by key.
|
|
105
|
-
options
|
|
117
|
+
options
|
|
106
118
|
The options' values, by ``dest``.
|
|
107
119
|
"""
|
|
108
120
|
|
|
@@ -117,19 +129,21 @@ class Command(FrozenModel):
|
|
|
117
129
|
|
|
118
130
|
Attributes
|
|
119
131
|
----------
|
|
120
|
-
name
|
|
132
|
+
name
|
|
121
133
|
The command's name on the command line.
|
|
122
|
-
help
|
|
134
|
+
help
|
|
123
135
|
One-line help.
|
|
124
|
-
options
|
|
136
|
+
options
|
|
125
137
|
Plain options and positionals.
|
|
126
|
-
interview
|
|
138
|
+
interview
|
|
127
139
|
Questions and consents. A command with any gets ``--yes`` and
|
|
128
140
|
``--questions``.
|
|
129
|
-
run
|
|
141
|
+
run
|
|
130
142
|
What the command does; ``None`` for a group of subcommands.
|
|
131
|
-
subcommands
|
|
143
|
+
subcommands
|
|
132
144
|
Nested commands.
|
|
145
|
+
interactive
|
|
146
|
+
Whether the command has questions or consents.
|
|
133
147
|
"""
|
|
134
148
|
|
|
135
149
|
name: str = Field(pattern=r"^[a-z][a-z0-9-]*$")
|
|
@@ -51,19 +51,25 @@ class Console:
|
|
|
51
51
|
|
|
52
52
|
Methods
|
|
53
53
|
-------
|
|
54
|
-
print
|
|
55
|
-
Print a result to stdout: text, rich markup or
|
|
56
|
-
info
|
|
57
|
-
Print
|
|
58
|
-
|
|
54
|
+
print
|
|
55
|
+
Print a result to stdout: text, rich markup or a rich renderable.
|
|
56
|
+
info
|
|
57
|
+
Print an informational status line.
|
|
58
|
+
success
|
|
59
|
+
Print a success status line.
|
|
60
|
+
warning
|
|
61
|
+
Print a warning status line.
|
|
62
|
+
error
|
|
63
|
+
Print an error status line.
|
|
64
|
+
progress
|
|
59
65
|
Show a progress bar on stderr for a series of named steps.
|
|
60
|
-
select
|
|
66
|
+
select
|
|
61
67
|
Ask for one of ``choices``.
|
|
62
|
-
checkbox
|
|
68
|
+
checkbox
|
|
63
69
|
Ask for any number of ``choices``.
|
|
64
|
-
confirm
|
|
70
|
+
confirm
|
|
65
71
|
Ask a yes/no question.
|
|
66
|
-
text
|
|
72
|
+
text
|
|
67
73
|
Ask for free text, validated, optionally with suggestions.
|
|
68
74
|
"""
|
|
69
75
|
|
|
@@ -72,26 +78,57 @@ class Console:
|
|
|
72
78
|
self._err = RichConsole(stderr=True)
|
|
73
79
|
|
|
74
80
|
def print(self, renderable: object) -> None:
|
|
75
|
-
"""Print a result to stdout: text, rich markup or a rich renderable.
|
|
81
|
+
"""Print a result to stdout: text, rich markup or a rich renderable.
|
|
82
|
+
|
|
83
|
+
Parameters
|
|
84
|
+
----------
|
|
85
|
+
renderable
|
|
86
|
+
Text, rich markup or any rich renderable.
|
|
87
|
+
"""
|
|
76
88
|
self._out.print(renderable)
|
|
77
89
|
|
|
78
90
|
def info(self, message: str) -> None:
|
|
79
|
-
"""Print an informational status line.
|
|
91
|
+
"""Print an informational status line.
|
|
92
|
+
|
|
93
|
+
Parameters
|
|
94
|
+
----------
|
|
95
|
+
message
|
|
96
|
+
The text to show, printed literally.
|
|
97
|
+
"""
|
|
80
98
|
self._status("INFO", "bold cyan", message)
|
|
81
99
|
|
|
82
100
|
def success(self, message: str) -> None:
|
|
83
|
-
"""Print a success status line.
|
|
101
|
+
"""Print a success status line.
|
|
102
|
+
|
|
103
|
+
Parameters
|
|
104
|
+
----------
|
|
105
|
+
message
|
|
106
|
+
The text to show, printed literally.
|
|
107
|
+
"""
|
|
84
108
|
self._status("OK", "bold green", message)
|
|
85
109
|
|
|
86
110
|
def warning(self, message: str) -> None:
|
|
87
|
-
"""Print a warning status line.
|
|
111
|
+
"""Print a warning status line.
|
|
112
|
+
|
|
113
|
+
Parameters
|
|
114
|
+
----------
|
|
115
|
+
message
|
|
116
|
+
The text to show, printed literally.
|
|
117
|
+
"""
|
|
88
118
|
self._status("WARN", "bold yellow", message)
|
|
89
119
|
|
|
90
120
|
def error(self, message: str) -> None:
|
|
91
|
-
"""Print an error status line.
|
|
121
|
+
"""Print an error status line.
|
|
122
|
+
|
|
123
|
+
Parameters
|
|
124
|
+
----------
|
|
125
|
+
message
|
|
126
|
+
The text to show, printed literally.
|
|
127
|
+
"""
|
|
92
128
|
self._status("ERROR", "bold red", message)
|
|
93
129
|
|
|
94
130
|
def _status(self, label: str, style: str, message: str) -> None:
|
|
131
|
+
"""Print a labelled status line to stderr."""
|
|
95
132
|
# Soft wrap: a long message stays one line when piped, for whatever reads it.
|
|
96
133
|
self._err.print(
|
|
97
134
|
Text.assemble((label, style), " ", message), highlight=False, soft_wrap=True
|
|
@@ -103,14 +140,13 @@ class Console:
|
|
|
103
140
|
|
|
104
141
|
Parameters
|
|
105
142
|
----------
|
|
106
|
-
description
|
|
143
|
+
description
|
|
107
144
|
What the steps do together, e.g. ``"Checking files"``.
|
|
108
145
|
|
|
109
146
|
Yields
|
|
110
147
|
------
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
the step's 1-based index and what it is doing.
|
|
148
|
+
``update(index, total, detail)``: call it before each step, with the step's
|
|
149
|
+
1-based index and what it is doing.
|
|
114
150
|
"""
|
|
115
151
|
with Progress(
|
|
116
152
|
TextColumn("[bold cyan]INFO[/bold cyan]"),
|
|
@@ -132,24 +168,23 @@ class Console:
|
|
|
132
168
|
|
|
133
169
|
Parameters
|
|
134
170
|
----------
|
|
135
|
-
prompt
|
|
171
|
+
prompt
|
|
136
172
|
The question.
|
|
137
|
-
choices
|
|
173
|
+
choices
|
|
138
174
|
What can be chosen.
|
|
139
|
-
label
|
|
175
|
+
label
|
|
140
176
|
How each choice is shown.
|
|
141
177
|
|
|
142
178
|
Returns
|
|
143
179
|
-------
|
|
144
|
-
|
|
145
|
-
The chosen item.
|
|
180
|
+
The chosen item.
|
|
146
181
|
|
|
147
182
|
Raises
|
|
148
183
|
------
|
|
149
184
|
CliError
|
|
150
|
-
|
|
185
|
+
There is nothing to choose from.
|
|
151
186
|
KeyboardInterrupt
|
|
152
|
-
|
|
187
|
+
The user cancels.
|
|
153
188
|
"""
|
|
154
189
|
if not choices:
|
|
155
190
|
raise CliError(f"{prompt}: nothing to choose from.")
|
|
@@ -173,27 +208,26 @@ class Console:
|
|
|
173
208
|
|
|
174
209
|
Parameters
|
|
175
210
|
----------
|
|
176
|
-
prompt
|
|
211
|
+
prompt
|
|
177
212
|
The question.
|
|
178
|
-
choices
|
|
213
|
+
choices
|
|
179
214
|
What can be chosen.
|
|
180
|
-
label
|
|
215
|
+
label
|
|
181
216
|
How each choice is shown.
|
|
182
|
-
checked
|
|
217
|
+
checked
|
|
183
218
|
The choices ticked when the prompt opens; every choice when
|
|
184
219
|
omitted, so the common answer "all of them" is one keypress.
|
|
185
220
|
|
|
186
221
|
Returns
|
|
187
222
|
-------
|
|
188
|
-
|
|
189
|
-
The chosen items, in ``choices`` order.
|
|
223
|
+
The chosen items, in ``choices`` order.
|
|
190
224
|
|
|
191
225
|
Raises
|
|
192
226
|
------
|
|
193
227
|
CliError
|
|
194
|
-
|
|
228
|
+
There is nothing to choose from.
|
|
195
229
|
KeyboardInterrupt
|
|
196
|
-
|
|
230
|
+
The user cancels.
|
|
197
231
|
"""
|
|
198
232
|
if not choices:
|
|
199
233
|
raise CliError(f"{prompt}: nothing to choose from.")
|
|
@@ -214,10 +248,21 @@ class Console:
|
|
|
214
248
|
def confirm(self, prompt: str, default: bool) -> bool:
|
|
215
249
|
"""Ask a yes/no question.
|
|
216
250
|
|
|
251
|
+
Parameters
|
|
252
|
+
----------
|
|
253
|
+
prompt
|
|
254
|
+
The question.
|
|
255
|
+
default
|
|
256
|
+
The answer an empty reply gives.
|
|
257
|
+
|
|
258
|
+
Returns
|
|
259
|
+
-------
|
|
260
|
+
The answer.
|
|
261
|
+
|
|
217
262
|
Raises
|
|
218
263
|
------
|
|
219
264
|
KeyboardInterrupt
|
|
220
|
-
|
|
265
|
+
The user cancels.
|
|
221
266
|
"""
|
|
222
267
|
answer: object = questionary.confirm(prompt, default=default, style=PROMPT_STYLE).ask()
|
|
223
268
|
if not isinstance(answer, bool):
|
|
@@ -235,26 +280,25 @@ class Console:
|
|
|
235
280
|
|
|
236
281
|
Parameters
|
|
237
282
|
----------
|
|
238
|
-
prompt
|
|
283
|
+
prompt
|
|
239
284
|
The question.
|
|
240
|
-
default
|
|
285
|
+
default
|
|
241
286
|
Text the input starts with.
|
|
242
|
-
validate
|
|
287
|
+
validate
|
|
243
288
|
Raises ``ValueError`` for an invalid answer; its message is shown
|
|
244
289
|
under the input until the answer changes.
|
|
245
|
-
suggestions
|
|
290
|
+
suggestions
|
|
246
291
|
Values listed under the input while they match what is typed.
|
|
247
292
|
Any other value can still be entered.
|
|
248
293
|
|
|
249
294
|
Returns
|
|
250
295
|
-------
|
|
251
|
-
|
|
252
|
-
The answer.
|
|
296
|
+
The answer.
|
|
253
297
|
|
|
254
298
|
Raises
|
|
255
299
|
------
|
|
256
300
|
KeyboardInterrupt
|
|
257
|
-
|
|
301
|
+
The user cancels.
|
|
258
302
|
"""
|
|
259
303
|
|
|
260
304
|
def _verdict(value: str) -> bool | str:
|