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
exit_for_config_validation_error ¶
exit_for_config_validation_error(
*,
ctx,
console,
exc,
config,
fmt,
meta,
verbosity_level,
quiet,
color,
)
Render config diagnostics for a validation failure and exit.
Strict config validation may stop a command before normal probe or
processing output exists. This helper preserves the existing CONFIG_ERROR
exit contract while ensuring the diagnostics that caused the failure remain
visible in the active output format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context used to exit with |
required |
console
|
ConsoleProtocol
|
Console used for command output. |
required |
exc
|
ConfigValidationError
|
Validation exception raised by config validation. |
required |
config
|
FrozenConfig
|
Effective frozen configuration carrying validation logs. |
required |
fmt
|
OutputFormat
|
Active output format. |
required |
meta
|
MetaPayload
|
Machine-output metadata for JSON and NDJSON diagnostics. |
required |
verbosity_level
|
int
|
Effective TEXT verbosity level. |
required |
quiet
|
bool
|
Whether TEXT quiet mode is enabled. |
required |
color
|
bool
|
Whether TEXT output should use color. |
required |
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
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |