topmark.cli.io¶
STDIN handling utilities for Click commands.
This module intentionally focuses only on normalizing STDIN usage for the CLI: - List mode: consume newline-delimited paths from STDIN. - Content mode: materialize STDIN bytes as a temporary file path.
Higher-level concerns like config building or file discovery are handled elsewhere.
StdinMode ¶
Bases: str, Enum
How STDIN was interpreted by consume_stdin().
StdinResult ¶
Bases: NamedTuple
Result of consuming STDIN for CLI commands that support it.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
StdinMode
|
How STDIN was interpreted: - "none": no STDIN was consumed (stdin is a TTY or empty) - "list": STDIN contained a list of paths (one per line) - "content": STDIN contained file content (written to a temp file) |
paths |
list[Path]
|
In "list" mode, the parsed paths. In "content", a single temp path. |
temp_path |
Path | None
|
The temp file created for "content" mode; otherwise None. |
errors |
list[str]
|
Non-fatal errors/warnings (strings) collected during processing. |
InputPlan
dataclass
¶
InputPlan(
*,
stdin_mode,
stdin_filename,
temp_path,
paths,
include_patterns,
exclude_patterns,
files_from,
include_from,
exclude_from,
)
Normalized inputs for building a MutableConfig and file list.
Attributes:
| Name | Type | Description |
|---|---|---|
stdin_mode |
bool
|
True when reading a single file's content from STDIN via "-". |
stdin_filename |
str | None
|
The filename to use when in STDIN mode. |
temp_path |
Path | None
|
Temporary file path used in content-on-STDIN mode; None otherwise. |
paths |
list[str]
|
Positional PATH arguments after normalization (and/or from --files-from -). |
include_patterns |
list[str]
|
Include glob patterns after merging CLI and include-from. |
exclude_patterns |
list[str]
|
Exclude glob patterns after merging CLI and exclude-from. |
files_from |
list[str]
|
File paths to read additional candidate paths from (no '-' sentinels). |
include_from |
list[str]
|
File paths to read include patterns from (no '-' sentinels). |
exclude_from |
list[str]
|
File paths to read exclude patterns from (no '-' sentinels). |
consume_stdin ¶
Consume STDIN if present and return a normalized result.
If stdin_filename is provided (or expect="content"), STDIN is treated
as the contents of a single file which is written to a temporary path.
The returned paths will contain exactly that temp path and mode == "content".
Otherwise (default), STDIN is treated as a list of paths, one per line.
Empty lines and lines starting with '#' are ignored. mode == "list".
If STDIN is a TTY or empty, returns mode == "none".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expect
|
Literal['auto', 'list', 'content']
|
Force interpretation of STDIN ("list" or "content"), or "auto". |
'auto'
|
stdin_filename
|
str | None
|
Target filename to use when interpreting STDIN as file content. If omitted in content mode, a default name is used. |
None
|
encoding
|
str
|
Text encoding for reading and writing. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
StdinResult
|
A struct describing what (if anything) was consumed. |
Source code in src/topmark/cli/io.py
merge_cli_paths_with_stdin ¶
Merge CLI-provided paths with STDIN, with predictable semantics.
Rules
- mode == "none": just return CLI paths.
- mode == "list": return CLI paths + list from STDIN (in that order).
- mode == "content": ignore CLI paths and return the single temp file path.
This keeps command bodies small and avoids subtle drift across subcommands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cli_paths
|
Iterable[str]
|
list of CLI-provided paths |
required |
stdin_result
|
StdinResult
|
result of consuming STDIN |
required |
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of paths (merged with STDIN input) |
Note
Do not use this to feed data for --files-from -, --include-from -, or --exclude-from -; those options expect the STDIN lines to be routed into their respective option lists instead of positional PATHS.
Source code in src/topmark/cli/io.py
plan_cli_inputs ¶
plan_cli_inputs(
*,
ctx,
files_from,
include_from,
exclude_from,
include_patterns,
exclude_patterns,
stdin_filename,
allow_empty_paths=False,
)
Normalize CLI args and STDIN into a plan, with strict guards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Click context. |
required |
files_from
|
Iterable[str]
|
Iterable of files to read candidate paths from (may include '-'). |
required |
include_from
|
Iterable[str]
|
Iterable of files to read include patterns from (may include '-'). |
required |
exclude_from
|
Iterable[str]
|
Iterable of files to read exclude patterns from (may include '-'). |
required |
include_patterns
|
Iterable[str]
|
Iterable of include glob patterns. |
required |
exclude_patterns
|
Iterable[str]
|
Iterable of exclude glob patterns. |
required |
stdin_filename
|
str | None
|
Optional assumed filename when reading content from STDIN. |
required |
allow_empty_paths
|
bool
|
If True, do not raise an error if no paths are provided. (Used for commands like dump-config that are file-agnostic.) |
False
|
Raises:
| Type | Description |
|---|---|
TopmarkCliUsageError
|
If - mixing content '-' with any ...-from '-' option. - using '-' as a PATH without --stdin-filename. - no input is provided (unless allow_empty_paths is True). |
Returns:
| Type | Description |
|---|---|
InputPlan
|
The normalized input plan for config and file discovery. |
Source code in src/topmark/cli/io.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |