topmark.cli.cmd_common¶
Common command utilities for Click-based commands.
This module holds small, focused helpers used by multiple CLI commands. They intentionally avoid policy (exit code rules, messages) and only encapsulate plumbing such as running pipelines, filtering, and error exits.
It also contains a small initializer (init_common_state) that prepares the
shared typed CLI state (TopmarkCliState) for commands that own verbosity,
color, and related human-output controls rather than the root command group.
PreparedCliConfig
dataclass
¶
Prepared CLI config state for a command run.
Attributes:
| Name | Type | Description |
|---|---|---|
resolved_toml |
ResolvedTopmarkTomlSources
|
Resolved TOML-side state for the current invocation. |
draft |
MutableConfig
|
Mutable config draft after layered CLI overrides. |
init_common_state ¶
Initialize shared UI/runtime state on the Click context.
It initializes the shared TopmarkCliState stored on ctx.obj.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Current Click context; will have |
required |
verbosity
|
int
|
Count of |
required |
quiet
|
bool
|
Whether |
required |
color_mode
|
ColorMode | None
|
Explicit color mode from |
required |
no_color
|
bool
|
Whether |
required |
Source code in src/topmark/cli/cmd_common.py
build_file_resolution ¶
Return file-list resolution diagnostics for the current invocation.
- In content-on-STDIN mode, return a synthetic resolution containing the single temp path created for staged input content.
- Otherwise, delegate to the unified resolver that uses
config.files,files_from, include/exclude patterns, and file types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_options
|
RunOptions
|
Invocation-wide runtime options for the current run. |
required |
config
|
FrozenConfig
|
Effective run config used for file discovery. |
required |
temp_path
|
Path | None
|
Temporary file path created for content-on-STDIN mode, if any. |
required |
Returns:
| Type | Description |
|---|---|
FileListResolution
|
|
FileListResolution
|
containing selected files and any discovery diagnostics. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If |
Source code in src/topmark/cli/cmd_common.py
build_run_options ¶
build_run_options(
*,
apply_changes,
write_mode,
stdin_mode,
stdin_filename,
prune_views=True,
keep_diff_view=False,
)
Build invocation-wide runtime options for a CLI command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apply_changes
|
bool
|
Whether the command should write changes. |
required |
write_mode
|
str | None
|
Effective CLI write mode ( |
required |
stdin_mode
|
bool
|
Whether the command is operating in content-on-STDIN mode. |
required |
stdin_filename
|
str | None
|
Synthetic file name associated with STDIN content, if any. |
required |
prune_views
|
bool
|
If |
True
|
keep_diff_view
|
bool
|
Whether to preserve the diff view. |
False
|
Returns:
| Type | Description |
|---|---|
RunOptions
|
The execution-only runtime options for the current CLI invocation. |
When available on the current Click context, resolved persisted writer preferences from TopMark TOML discovery are overlaid unless explicit runtime intent already selected a conflicting output mode or file write strategy.
Source code in src/topmark/cli/cmd_common.py
exit_if_no_files ¶
Echo a friendly message and return True if there is nothing to process.
Source code in src/topmark/cli/cmd_common.py
maybe_route_console_to_stderr ¶
Route human-facing console output to stderr when stdout carries file content.
TopMark can emit rewritten file content to STDOUT in two situations:
- content-on-STDIN mode (a lone
-path) when--applyis set; and - when
--write-mode=stdoutis used with--apply.
In both cases, the command must keep STDOUT clean for the content stream so that output can be piped safely. Human-facing output (summaries, warnings, diagnostics) is therefore routed to STDERR.
The function updates the console stored on the shared typed CLI state when rerouting is needed and returns the effective console instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Current Click context. |
required |
run_options
|
RunOptions
|
Invocation-wide runtime options for the current run. |
required |
enable_color
|
bool
|
Whether color output is enabled for this invocation. |
required |
Returns:
| Type | Description |
|---|---|
ConsoleProtocol
|
The effective console instance to use for human-facing output. |
Source code in src/topmark/cli/cmd_common.py
maybe_exit_on_error ¶
If an error code was encountered, cleanup and exit with it.
Source code in src/topmark/cli/cmd_common.py
build_cli_policy_overrides ¶
Build structured policy overrides from typed CLI state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
TopmarkCliState
|
Shared typed CLI invocation state. |
required |
Returns:
| Type | Description |
|---|---|
PolicyOverrides
|
Structured policy overrides derived from the mutable CLI policy object. |
Source code in src/topmark/cli/cmd_common.py
build_resolved_toml_sources_and_config_for_plan ¶
build_resolved_toml_sources_and_config_for_plan(
*,
ctx,
plan,
no_config,
config_paths,
strict,
include_file_types,
exclude_file_types,
align_fields,
relative_to,
)
Build resolved TOML sources and a config draft for an input plan.
This helper keeps the CLI layer intentionally thin:
- Compute a discovery anchor from the input plan.
- Resolve TOML sources once.
- Build the layered config draft from that resolved TOML state.
- Apply layered CLI overrides via
topmark.config.overrides.apply_config_overrides().
Resolution order remains:
defaults -> resolved TOML sources -> config draft -> CLI overrides
As a side effect, this helper also resolves persisted TOML writer preferences for the same discovery inputs and stores them on the shared typed CLI state for later runtime-option assembly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Click context carrying the shared typed CLI state. |
required |
plan
|
InputPlan
|
Input plan containing paths, STDIN metadata, and pattern-source options. |
required |
no_config
|
bool
|
Whether to skip discovered config files. |
required |
config_paths
|
list[str]
|
Explicit extra config files passed on the CLI. |
required |
strict
|
bool | None
|
Optional CLI override for strict config
checking. |
required |
include_file_types
|
list[str]
|
CLI include file-type filters. |
required |
exclude_file_types
|
list[str]
|
CLI exclude file-type filters. |
required |
align_fields
|
bool | None
|
Optional CLI override for header alignment. |
required |
relative_to
|
str | None
|
Optional CLI override for header-relative path rendering. |
required |
Returns:
| Type | Description |
|---|---|
PreparedCliConfig
|
Prepared CLI config state containing the resolved TOML-side state and |
PreparedCliConfig
|
mutable config draft after layered CLI overrides. |
Source code in src/topmark/cli/cmd_common.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |