jarbin-c-coding-style 2.0.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.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
sources/cli/context.py
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
from collections.abc import Iterator, Mapping
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from jarbin_toolkit_error import JErrorKey
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True, slots=True)
|
|
9
|
+
class FlagValue[T]:
|
|
10
|
+
"""
|
|
11
|
+
Parsed state of one flag.
|
|
12
|
+
(dataclass)
|
|
13
|
+
|
|
14
|
+
Attributes
|
|
15
|
+
----------
|
|
16
|
+
name : str
|
|
17
|
+
Flag name, e.g. "no_color".
|
|
18
|
+
value : T
|
|
19
|
+
Converted value, or the flag default when it was not called.
|
|
20
|
+
is_called : bool
|
|
21
|
+
Whether the flag appeared on the command line.
|
|
22
|
+
count : int
|
|
23
|
+
Number of times the flag appeared.
|
|
24
|
+
token : str | None
|
|
25
|
+
Token used by the first occurrence (e.g. "-o"), None when not called.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
name: str
|
|
29
|
+
value: T
|
|
30
|
+
is_called: bool = False
|
|
31
|
+
count: int = 0
|
|
32
|
+
token: str | None = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True, slots=True)
|
|
36
|
+
class CommandArguments:
|
|
37
|
+
"""
|
|
38
|
+
Parsed flags of one command of the selected chain. Every flag defined on the command is present,
|
|
39
|
+
called or not.
|
|
40
|
+
(dataclass)
|
|
41
|
+
|
|
42
|
+
Attributes
|
|
43
|
+
----------
|
|
44
|
+
name : str
|
|
45
|
+
Command name.
|
|
46
|
+
flags : Mapping[str, FlagValue[Any]]
|
|
47
|
+
Parsed flags, by name (read-only).
|
|
48
|
+
operands : tuple[Any, ...]
|
|
49
|
+
Converted positional arguments, in command-line order.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
name: str
|
|
53
|
+
flags: Mapping[str, FlagValue[Any]]
|
|
54
|
+
operands: tuple[Any, ...] = ()
|
|
55
|
+
|
|
56
|
+
def __contains__(self, name: object) -> bool:
|
|
57
|
+
"""
|
|
58
|
+
Whether the command defines a flag with this name.
|
|
59
|
+
|
|
60
|
+
Parameters
|
|
61
|
+
----------
|
|
62
|
+
name : object
|
|
63
|
+
Flag name.
|
|
64
|
+
|
|
65
|
+
Returns
|
|
66
|
+
----------
|
|
67
|
+
bool
|
|
68
|
+
True if the flag exists on this command.
|
|
69
|
+
"""
|
|
70
|
+
return name in self.flags
|
|
71
|
+
|
|
72
|
+
def __getitem__(self, name: str) -> FlagValue[Any]:
|
|
73
|
+
"""
|
|
74
|
+
Get a flag of this command.
|
|
75
|
+
|
|
76
|
+
Parameters
|
|
77
|
+
----------
|
|
78
|
+
name : str
|
|
79
|
+
Flag name.
|
|
80
|
+
|
|
81
|
+
Returns
|
|
82
|
+
----------
|
|
83
|
+
FlagValue[Any]
|
|
84
|
+
The parsed flag.
|
|
85
|
+
|
|
86
|
+
Raises
|
|
87
|
+
----------
|
|
88
|
+
JErrorKey
|
|
89
|
+
The command defines no flag with this name.
|
|
90
|
+
"""
|
|
91
|
+
flag: FlagValue[Any] | None = self.flags.get(name)
|
|
92
|
+
if flag is None:
|
|
93
|
+
raise JErrorKey(f"Command '{self.name}' defines no flag named '{name}'.", key=name)
|
|
94
|
+
return flag
|
|
95
|
+
|
|
96
|
+
def __iter__(self) -> Iterator[FlagValue[Any]]:
|
|
97
|
+
"""
|
|
98
|
+
Iterate over every flag of this command.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
----------
|
|
102
|
+
Iterator[FlagValue[Any]]
|
|
103
|
+
The parsed flags, in definition order.
|
|
104
|
+
"""
|
|
105
|
+
return iter(self.flags.values())
|
|
106
|
+
|
|
107
|
+
@property
|
|
108
|
+
def called(self) -> tuple[FlagValue[Any], ...]:
|
|
109
|
+
"""
|
|
110
|
+
Flags of this command that appeared on the command line.
|
|
111
|
+
|
|
112
|
+
Returns
|
|
113
|
+
----------
|
|
114
|
+
tuple[FlagValue[Any], ...]
|
|
115
|
+
The called flags, in definition order.
|
|
116
|
+
"""
|
|
117
|
+
return tuple(flag for flag in self.flags.values() if flag.is_called)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
@dataclass(frozen=True, slots=True)
|
|
121
|
+
class ParsedArguments:
|
|
122
|
+
"""
|
|
123
|
+
Result of parsing a command line: the chain of selected commands, from the root (program) to the
|
|
124
|
+
deepest selected subcommand.
|
|
125
|
+
(dataclass)
|
|
126
|
+
|
|
127
|
+
Attributes
|
|
128
|
+
----------
|
|
129
|
+
commands : tuple[CommandArguments, ...]
|
|
130
|
+
Parsed commands, root first; never empty.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
commands: tuple[CommandArguments, ...]
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def root(self) -> CommandArguments:
|
|
137
|
+
"""
|
|
138
|
+
Parsed global flags (the program itself).
|
|
139
|
+
|
|
140
|
+
Returns
|
|
141
|
+
----------
|
|
142
|
+
CommandArguments
|
|
143
|
+
The root command.
|
|
144
|
+
"""
|
|
145
|
+
return self.commands[0]
|
|
146
|
+
|
|
147
|
+
@property
|
|
148
|
+
def command(self) -> CommandArguments:
|
|
149
|
+
"""
|
|
150
|
+
Deepest selected command (the root when no subcommand was given).
|
|
151
|
+
|
|
152
|
+
Returns
|
|
153
|
+
----------
|
|
154
|
+
CommandArguments
|
|
155
|
+
The selected command.
|
|
156
|
+
"""
|
|
157
|
+
return self.commands[-1]
|
|
158
|
+
|
|
159
|
+
@property
|
|
160
|
+
def operands(self) -> tuple[Any, ...]:
|
|
161
|
+
"""
|
|
162
|
+
Positional arguments of the selected command.
|
|
163
|
+
|
|
164
|
+
Returns
|
|
165
|
+
----------
|
|
166
|
+
tuple[Any, ...]
|
|
167
|
+
The converted operands (empty when none was given).
|
|
168
|
+
"""
|
|
169
|
+
return self.command.operands
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def path(self) -> tuple[str, ...]:
|
|
173
|
+
"""
|
|
174
|
+
Names of the selected subcommands, without the program name.
|
|
175
|
+
|
|
176
|
+
Returns
|
|
177
|
+
----------
|
|
178
|
+
tuple[str, ...]
|
|
179
|
+
E.g. ("check",), or () when no subcommand was given.
|
|
180
|
+
"""
|
|
181
|
+
return tuple(command.name for command in self.commands[1:])
|
|
182
|
+
|
|
183
|
+
def flag(self, name: str) -> FlagValue[Any]:
|
|
184
|
+
"""
|
|
185
|
+
Get a flag as seen from the selected command. Commands are searched from the deepest to the root:
|
|
186
|
+
the first one where the flag was called wins; if it was called nowhere, the deepest definition
|
|
187
|
+
is returned (so its default applies).
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
name : str
|
|
192
|
+
Flag name.
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
----------
|
|
196
|
+
FlagValue[Any]
|
|
197
|
+
The parsed flag.
|
|
198
|
+
|
|
199
|
+
Raises
|
|
200
|
+
----------
|
|
201
|
+
JErrorKey
|
|
202
|
+
No command of the chain defines this flag.
|
|
203
|
+
"""
|
|
204
|
+
fallback: FlagValue[Any] | None = None
|
|
205
|
+
for command in reversed(self.commands):
|
|
206
|
+
current: FlagValue[Any] | None = command.flags.get(name)
|
|
207
|
+
if current is None:
|
|
208
|
+
continue
|
|
209
|
+
if current.is_called:
|
|
210
|
+
return current
|
|
211
|
+
if fallback is None:
|
|
212
|
+
fallback = current
|
|
213
|
+
if fallback is None:
|
|
214
|
+
path: str = " ".join(command.name for command in self.commands)
|
|
215
|
+
raise JErrorKey(f"No command of '{path}' defines a flag named '{name}'.", key=name)
|
|
216
|
+
return fallback
|
|
217
|
+
|
|
218
|
+
def value(self, name: str) -> Any:
|
|
219
|
+
"""
|
|
220
|
+
Shortcut for `flag(name).value`.
|
|
221
|
+
|
|
222
|
+
Parameters
|
|
223
|
+
----------
|
|
224
|
+
name : str
|
|
225
|
+
Flag name.
|
|
226
|
+
|
|
227
|
+
Returns
|
|
228
|
+
----------
|
|
229
|
+
Any
|
|
230
|
+
The value of the flag.
|
|
231
|
+
|
|
232
|
+
Raises
|
|
233
|
+
----------
|
|
234
|
+
JErrorKey
|
|
235
|
+
No command of the chain defines this flag.
|
|
236
|
+
"""
|
|
237
|
+
return self.flag(name).value
|
|
238
|
+
|
|
239
|
+
def is_called(self, name: str) -> bool:
|
|
240
|
+
"""
|
|
241
|
+
Shortcut for `flag(name).is_called`.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
name : str
|
|
246
|
+
Flag name.
|
|
247
|
+
|
|
248
|
+
Returns
|
|
249
|
+
----------
|
|
250
|
+
bool
|
|
251
|
+
Whether the flag appeared on the command line.
|
|
252
|
+
|
|
253
|
+
Raises
|
|
254
|
+
----------
|
|
255
|
+
JErrorKey
|
|
256
|
+
No command of the chain defines this flag.
|
|
257
|
+
"""
|
|
258
|
+
return self.flag(name).is_called
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
from sources.errors.exceptions import ValueConversionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Literal accepted by `positive_int_or_auto` to let JCCS choose the value.
|
|
7
|
+
AUTO_KEYWORD: str = "auto"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _parse_int(raw: str) -> int:
|
|
11
|
+
"""
|
|
12
|
+
Parse a base-10 integer.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
raw : str
|
|
17
|
+
The value as typed by the user.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
----------
|
|
21
|
+
int
|
|
22
|
+
The parsed integer.
|
|
23
|
+
|
|
24
|
+
Raises
|
|
25
|
+
----------
|
|
26
|
+
ValueConversionError
|
|
27
|
+
The value is not an integer.
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
return int(raw)
|
|
31
|
+
except ValueError as error:
|
|
32
|
+
raise ValueConversionError("not an integer", raw) from error
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _parse_float(raw: str) -> float:
|
|
36
|
+
"""
|
|
37
|
+
Parse a decimal number.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
raw : str
|
|
42
|
+
The value as typed by the user.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
----------
|
|
46
|
+
float
|
|
47
|
+
The parsed number.
|
|
48
|
+
|
|
49
|
+
Raises
|
|
50
|
+
----------
|
|
51
|
+
ValueConversionError
|
|
52
|
+
The value is not a number.
|
|
53
|
+
"""
|
|
54
|
+
try:
|
|
55
|
+
return float(raw)
|
|
56
|
+
except ValueError as error:
|
|
57
|
+
raise ValueConversionError("not a number", raw) from error
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def non_empty_path(raw: str) -> Path:
|
|
61
|
+
"""
|
|
62
|
+
Convert a flag value into a path. `-` is kept as is and means stdin/stdout.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
raw : str
|
|
67
|
+
The value as typed by the user.
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
----------
|
|
71
|
+
Path
|
|
72
|
+
The converted path (not resolved, existence not checked).
|
|
73
|
+
|
|
74
|
+
Raises
|
|
75
|
+
----------
|
|
76
|
+
ValueConversionError
|
|
77
|
+
The value is empty.
|
|
78
|
+
"""
|
|
79
|
+
if not raw:
|
|
80
|
+
raise ValueConversionError("path must not be empty", raw)
|
|
81
|
+
return Path(raw)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def non_empty_str(raw: str) -> str:
|
|
85
|
+
"""
|
|
86
|
+
Accept any non-empty string.
|
|
87
|
+
|
|
88
|
+
Parameters
|
|
89
|
+
----------
|
|
90
|
+
raw : str
|
|
91
|
+
The value as typed by the user.
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
----------
|
|
95
|
+
str
|
|
96
|
+
The value unchanged.
|
|
97
|
+
|
|
98
|
+
Raises
|
|
99
|
+
----------
|
|
100
|
+
ValueConversionError
|
|
101
|
+
The value is empty.
|
|
102
|
+
"""
|
|
103
|
+
if not raw:
|
|
104
|
+
raise ValueConversionError("value must not be empty", raw)
|
|
105
|
+
return raw
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def non_negative_int(raw: str) -> int:
|
|
109
|
+
"""
|
|
110
|
+
Convert a flag value into an integer >= 0.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
raw : str
|
|
115
|
+
The value as typed by the user.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
----------
|
|
119
|
+
int
|
|
120
|
+
The converted integer.
|
|
121
|
+
|
|
122
|
+
Raises
|
|
123
|
+
----------
|
|
124
|
+
ValueConversionError
|
|
125
|
+
The value is not an integer or is negative.
|
|
126
|
+
"""
|
|
127
|
+
value: int = _parse_int(raw)
|
|
128
|
+
if value < 0:
|
|
129
|
+
raise ValueConversionError("must be >= 0", raw)
|
|
130
|
+
return value
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def positive_int(raw: str) -> int:
|
|
134
|
+
"""
|
|
135
|
+
Convert a flag value into an integer >= 1.
|
|
136
|
+
|
|
137
|
+
Parameters
|
|
138
|
+
----------
|
|
139
|
+
raw : str
|
|
140
|
+
The value as typed by the user.
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
----------
|
|
144
|
+
int
|
|
145
|
+
The converted integer.
|
|
146
|
+
|
|
147
|
+
Raises
|
|
148
|
+
----------
|
|
149
|
+
ValueConversionError
|
|
150
|
+
The value is not an integer or is lower than 1.
|
|
151
|
+
"""
|
|
152
|
+
value: int = _parse_int(raw)
|
|
153
|
+
if value < 1:
|
|
154
|
+
raise ValueConversionError("must be >= 1", raw)
|
|
155
|
+
return value
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def positive_int_or_auto(raw: str) -> int | None:
|
|
159
|
+
"""
|
|
160
|
+
Convert a flag value into an integer >= 1, or None for `auto`.
|
|
161
|
+
|
|
162
|
+
Parameters
|
|
163
|
+
----------
|
|
164
|
+
raw : str
|
|
165
|
+
The value as typed by the user.
|
|
166
|
+
|
|
167
|
+
Returns
|
|
168
|
+
----------
|
|
169
|
+
int | None
|
|
170
|
+
The converted integer, or None when the user asked for `auto`.
|
|
171
|
+
|
|
172
|
+
Raises
|
|
173
|
+
----------
|
|
174
|
+
ValueConversionError
|
|
175
|
+
The value is neither `auto` nor an integer >= 1.
|
|
176
|
+
"""
|
|
177
|
+
if raw == AUTO_KEYWORD:
|
|
178
|
+
return None
|
|
179
|
+
return positive_int(raw)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def positive_float(raw: str) -> float:
|
|
183
|
+
"""
|
|
184
|
+
Convert a flag value into a float > 0.
|
|
185
|
+
|
|
186
|
+
Parameters
|
|
187
|
+
----------
|
|
188
|
+
raw : str
|
|
189
|
+
The value as typed by the user.
|
|
190
|
+
|
|
191
|
+
Returns
|
|
192
|
+
----------
|
|
193
|
+
float
|
|
194
|
+
The converted float.
|
|
195
|
+
|
|
196
|
+
Raises
|
|
197
|
+
----------
|
|
198
|
+
ValueConversionError
|
|
199
|
+
The value is not a number or is not strictly positive.
|
|
200
|
+
"""
|
|
201
|
+
value: float = _parse_float(raw)
|
|
202
|
+
if not value > 0:
|
|
203
|
+
raise ValueConversionError("must be > 0", raw)
|
|
204
|
+
return value
|
sources/cli/help.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from sources.cli.arguments import CommandSpec, FlagSpec
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# Indentation of the entries of a help section.
|
|
9
|
+
HELP_INDENT: str = " "
|
|
10
|
+
|
|
11
|
+
# Minimum number of spaces between an entry and its description.
|
|
12
|
+
HELP_GAP: int = 3
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _program_line(chain: tuple[CommandSpec, ...]) -> str:
|
|
16
|
+
"""
|
|
17
|
+
Command path as typed by the user, e.g. "jccs check".
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
chain : tuple[CommandSpec, ...]
|
|
22
|
+
Commands from the root to the current one.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
----------
|
|
26
|
+
str
|
|
27
|
+
The command path.
|
|
28
|
+
"""
|
|
29
|
+
return " ".join(command.name for command in chain)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def render_usage(chain: tuple[CommandSpec, ...]) -> str:
|
|
33
|
+
"""
|
|
34
|
+
Short usage line of a command, e.g. "Usage: jccs check [FLAGS] [<path> ...]".
|
|
35
|
+
|
|
36
|
+
Parameters
|
|
37
|
+
----------
|
|
38
|
+
chain : tuple[CommandSpec, ...]
|
|
39
|
+
Commands from the root to the current one.
|
|
40
|
+
|
|
41
|
+
Returns
|
|
42
|
+
----------
|
|
43
|
+
str
|
|
44
|
+
The usage line.
|
|
45
|
+
"""
|
|
46
|
+
command: CommandSpec = chain[-1]
|
|
47
|
+
parts: list[str] = [f"Usage: {_program_line(chain)}"]
|
|
48
|
+
if command.flags or len(chain) > 1:
|
|
49
|
+
parts.append("[FLAGS]")
|
|
50
|
+
if command.subcommands:
|
|
51
|
+
parts.append("<" + "|".join(command.subcommand_names) + ">")
|
|
52
|
+
parts.append("[...]")
|
|
53
|
+
if command.operands is not None:
|
|
54
|
+
parts.append(f"[{command.operands.display()} ...]")
|
|
55
|
+
return " ".join(parts)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _default_text(flag: FlagSpec) -> str:
|
|
59
|
+
"""
|
|
60
|
+
Default value of a flag as shown in help, or "" when there is nothing worth showing.
|
|
61
|
+
|
|
62
|
+
Parameters
|
|
63
|
+
----------
|
|
64
|
+
flag : FlagSpec
|
|
65
|
+
The flag.
|
|
66
|
+
|
|
67
|
+
Returns
|
|
68
|
+
----------
|
|
69
|
+
str
|
|
70
|
+
The default text.
|
|
71
|
+
"""
|
|
72
|
+
value: Any = flag.default_value
|
|
73
|
+
if flag.is_switch or value is None or value == () or value is False:
|
|
74
|
+
return ""
|
|
75
|
+
if isinstance(value, Enum):
|
|
76
|
+
value = value.value
|
|
77
|
+
if isinstance(value, Path):
|
|
78
|
+
value = str(value)
|
|
79
|
+
return f" (default: {value})"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _table(entries: list[tuple[str, str]]) -> list[str]:
|
|
83
|
+
"""
|
|
84
|
+
Align two-column help entries.
|
|
85
|
+
|
|
86
|
+
Parameters
|
|
87
|
+
----------
|
|
88
|
+
entries : list[tuple[str, str]]
|
|
89
|
+
(left, right) pairs.
|
|
90
|
+
|
|
91
|
+
Returns
|
|
92
|
+
----------
|
|
93
|
+
list[str]
|
|
94
|
+
The aligned lines.
|
|
95
|
+
"""
|
|
96
|
+
width: int = max((len(left) for left, _ in entries), default=0) + HELP_GAP
|
|
97
|
+
return [f"{HELP_INDENT}{left.ljust(width)}{right}".rstrip() for left, right in entries]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def render_help(chain: tuple[CommandSpec, ...]) -> str:
|
|
101
|
+
"""
|
|
102
|
+
Full help of a command: description, usage, subcommands, own flags and flags inherited from its
|
|
103
|
+
ancestors (generated from the specifications, never written by hand).
|
|
104
|
+
|
|
105
|
+
Parameters
|
|
106
|
+
----------
|
|
107
|
+
chain : tuple[CommandSpec, ...]
|
|
108
|
+
Commands from the root to the current one.
|
|
109
|
+
|
|
110
|
+
Returns
|
|
111
|
+
----------
|
|
112
|
+
str
|
|
113
|
+
The help text (without final newline).
|
|
114
|
+
"""
|
|
115
|
+
command: CommandSpec = chain[-1]
|
|
116
|
+
lines: list[str] = [f"{_program_line(chain)} - {command.description}", "", render_usage(chain)]
|
|
117
|
+
if command.operands is not None:
|
|
118
|
+
lines.extend(["", "Arguments:"])
|
|
119
|
+
lines.extend(_table([(f"{command.operands.display()} ...", "Files or directories to process (default: the whole project).")]))
|
|
120
|
+
if command.subcommands:
|
|
121
|
+
lines.extend(["", "Commands:"])
|
|
122
|
+
lines.extend(_table([(subcommand.name, subcommand.description) for subcommand in command.subcommands]))
|
|
123
|
+
if command.flags:
|
|
124
|
+
lines.extend(["", "Flags:"])
|
|
125
|
+
lines.extend(_table([(flag.usage(), flag.description + _default_text(flag)) for flag in command.flags]))
|
|
126
|
+
inherited: list[FlagSpec] = [flag for ancestor in chain[:-1] for flag in ancestor.flags if flag.inherited]
|
|
127
|
+
if inherited:
|
|
128
|
+
lines.extend(["", "Global flags:"])
|
|
129
|
+
lines.extend(_table([(flag.usage(), flag.description) for flag in inherited]))
|
|
130
|
+
if command.subcommands:
|
|
131
|
+
lines.extend(["", f"Run '{_program_line(chain)} <command> --help' for the help of a command."])
|
|
132
|
+
return "\n".join(lines)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def command_chain(root: CommandSpec, path: tuple[str, ...]) -> tuple[CommandSpec, ...]:
|
|
136
|
+
"""
|
|
137
|
+
Specifications of the commands named by a parsed path.
|
|
138
|
+
|
|
139
|
+
Parameters
|
|
140
|
+
----------
|
|
141
|
+
root : CommandSpec
|
|
142
|
+
Root of the command tree.
|
|
143
|
+
path : tuple[str, ...]
|
|
144
|
+
Names of the selected subcommands (without the program).
|
|
145
|
+
|
|
146
|
+
Returns
|
|
147
|
+
----------
|
|
148
|
+
tuple[CommandSpec, ...]
|
|
149
|
+
The chain, root first.
|
|
150
|
+
|
|
151
|
+
Raises
|
|
152
|
+
----------
|
|
153
|
+
KeyError
|
|
154
|
+
A name does not match a subcommand.
|
|
155
|
+
"""
|
|
156
|
+
chain: list[CommandSpec] = [root]
|
|
157
|
+
for name in path:
|
|
158
|
+
subcommand: CommandSpec | None = chain[-1].find_subcommand(name)
|
|
159
|
+
if subcommand is None:
|
|
160
|
+
raise KeyError(name)
|
|
161
|
+
chain.append(subcommand)
|
|
162
|
+
return tuple(chain)
|