topmark.cli.validators¶
CLI input validation and option policy helpers.
This module centralizes small, Click-layer checks and normalizations that are shared across multiple commands.
Conventions
apply_*helpers apply a policy and may update the shared typed CLI state (for example, to disable ANSI color for output formats that must remain plain). Someapply_*helpers also return the effective value they set or normalized.validate_*helpers enforce a policy and raiseTopmarkCliUsageErrorwhen the invocation is invalid or unsupported.
Notes
- Messages should use
ctx.command_pathfor consistency across command groups. - All
validate_*helpers raiseTopmarkCliUsageErroron invalid usage.
validate_common_forbidden_path_command_options_in_extra_args ¶
Reject common unsupported options left behind by permissive path parsing.
Path commands use ignore_unknown_options=True and allow_extra_args=True
to support Black-style path handling. This means a mistyped or unsupported
option can survive Click parsing in ctx.args. This validator catches
known common spellings before input planning can treat them as file paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context whose |
required |
Raises:
| Type | Description |
|---|---|
TopmarkCliUsageError
|
If a known common unsupported option is present in
|
Source code in src/topmark/cli/validators.py
validate_forbidden_options_in_extra_args ¶
Reject known-but-forbidden options that remain in ctx.args.
This is used for commands that enable Click's permissive path-oriented parsing
(ignore_unknown_options=True / allow_extra_args=True), where unsupported
options would otherwise be silently accepted as extra arguments.
Source code in src/topmark/cli/validators.py
validate_mutually_exclusive ¶
Validate that at most one of the provided flags is enabled.
This is a small Click-oriented utility for enforcing mutual exclusion between boolean CLI options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context (used for |
required |
flags
|
dict[str, bool]
|
Mapping of user-facing option spellings (e.g. |
required |
message
|
str | None
|
Optional override for the error message. When omitted, a standard message using the enabled option spellings is emitted. |
None
|
Raises:
| Type | Description |
|---|---|
TopmarkCliUsageError
|
If more than one flag is enabled. |
Source code in src/topmark/cli/validators.py
validate_machine_format_forbids_flags ¶
Validate that certain flags are forbidden when a machine-readable output format is used.
This is a Click-layer policy helper to enforce that specific CLI options are not compatible with machine-readable output formats (e.g. JSON, NDJSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context (used for |
required |
fmt
|
OutputFormat
|
Effective output format selected for the command. |
required |
flags
|
Mapping[str, bool]
|
Mapping of user-facing option spellings to their parsed boolean values. |
required |
reason
|
str
|
Explanation string appended to the error message. Should include a leading verb phrase such as "is not supported" or "are not supported". |
required |
Raises:
| Type | Description |
|---|---|
TopmarkCliUsageError
|
If any of the specified flags are enabled with a machine-readable format. |
Source code in src/topmark/cli/validators.py
warn_and_clear ¶
Warn the user and clear one supported field on typed CLI state.
This helper is for non-fatal output-policy adjustments where TopMark warns
that an explicitly requested option is ignored and then updates the
corresponding TopmarkCliState field. It is intentionally not a generic
state setter; supported fields are defined by _set_cleared_cli_state_value().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context carrying the shared typed CLI state. |
required |
message
|
str
|
Warning message to emit before mutating state. |
required |
obj_key
|
str
|
Supported |
required |
cleared_value
|
_T
|
Replacement value to assign to the typed state field. |
required |
Returns:
| Type | Description |
|---|---|
_T
|
The same |
_T
|
expressions. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
TypeError
|
If |
Source code in src/topmark/cli/validators.py
warn_if_report_scope_ignored ¶
Warn when an explicitly provided --report value will have no effect.
--report only affects human per-file output. It is ignored when:
- a machine-readable output format is selected, or
- summary mode is enabled.
To avoid noisy warnings for the default case, this helper only emits a
warning when the user explicitly provided --report on the command line.
Warnings are emitted through the CLI console helper so machine-readable payloads on stdout remain unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context. |
required |
output_format
|
OutputFormat
|
Effective output format. |
required |
summary_mode
|
bool
|
Whether summary-only mode is enabled. |
required |
report_scope
|
ReportScope
|
Effective report scope. |
required |
Source code in src/topmark/cli/validators.py
apply_color_policy_for_output_format ¶
Enforce the CLI color policy for the selected output format.
Colorized (ANSI) output is only supported for OutputFormat.TEXT. For all other formats
(e.g. markdown, json, ndjson), ANSI color codes must be disabled to avoid corrupting
structured or copy/paste-friendly output.
Behavior
- If
fmtis notOutputFormat.TEXT, forcestate.color_enabledtoFalse. - If the user explicitly requested
--color=alwaysand the format does not support color, emit a warning explaining that the option is being ignored.
This helper updates the shared typed CLI state and is intended to be called by subcommands after the effective output format has been resolved.
Requires on TopmarkCliState:
- console: The active console instance.
- color_mode: The color mode specified with --color or --no-color.
- color_enabled: Whether color output is effectively enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context carrying the shared typed CLI state. |
required |
fmt
|
OutputFormat
|
Effective output format selected for the command. |
required |
Source code in src/topmark/cli/validators.py
apply_ignore_positional_paths_policy ¶
Ignore positional PATHS for file-agnostic commands.
Some CLI commands do not operate on input files (for example, configuration inspection
commands like topmark config check). When such commands are implemented with
allow_extra_args=True / ignore_unknown_options=True, Click places unexpected
positional arguments into ctx.args.
This helper applies a consistent policy:
- If any positional arguments were provided, emit a warning that they are ignored.
- If
"-"was provided (STDIN sentinel) andwarn_stdin_dashis enabled, emit a dedicated warning that STDIN is ignored. - Clear
ctx.argsso downstream logic can assume the command is file-agnostic.
Messages use Click's computed ctx.command_path (for example, "topmark config check"), which
already includes the full group/subcommand path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context. |
required |
warn_stdin_dash
|
bool
|
If True, emit an extra warning when |
True
|
Returns:
| Type | Description |
|---|---|
None
|
None. This helper mutates |
Source code in src/topmark/cli/validators.py
validate_output_verbosity_policy ¶
Validate TEXT-only verbosity and quiet policy for the current invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context carrying the shared typed CLI state. |
required |
verbosity
|
int
|
Raw verbosity count from |
required |
quiet
|
bool
|
Whether |
required |
fmt
|
OutputFormat
|
Effective output format for this invocation. |
required |
Behavior
- For non-TEXT formats,
--verboseand--quietare cleared silently. - For TEXT output,
--verboseand--quietare mutually exclusive.
Source code in src/topmark/cli/validators.py
validate_diff_policy_for_output_format ¶
Validate that unified diffs are only supported with human-readable output formats.
Unified diffs are a human-facing rendering feature and are not supported for machine-readable
output (json/ndjson). If --diff is requested with a machine-readable format, raise a
TopmarkCliUsageError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context carrying the shared typed CLI state. |
required |
diff
|
bool
|
Whether the user requested unified diffs. |
required |
fmt
|
OutputFormat
|
Effective output format for this invocation. |
required |
Source code in src/topmark/cli/validators.py
validate_human_only_config_flags_for_machine_format ¶
Validate that human-only config template flags are not used with machine-readable formats.
Some options only affect human-facing template rendering (e.g. injecting root = true
or emitting a pyproject-scoped [tool.topmark] header). Machine-readable output formats
(JSON/NDJSON) should remain schema-driven and are not compatible with such template-only
toggles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context. |
required |
config_root
|
bool
|
Whether the user requested |
required |
for_pyproject
|
bool
|
Whether the user requested |
required |
fmt
|
OutputFormat
|
Effective output format for this invocation. |
required |
Source code in src/topmark/cli/validators.py
validate_stdin_dash_requires_piped_input ¶
Fail fast if a --*-from - option is used without piped STDIN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
Context
|
Active Click context. |
required |
files_from
|
list[str] | None
|
Parsed |
required |
include_from
|
list[str] | None
|
Parsed |
required |
exclude_from
|
list[str] | None
|
Parsed |
required |
Raises:
| Type | Description |
|---|---|
TopmarkCliUsageError
|
if any of the |