Utilities Command API Reference¶
This section documents the internals of the utilities
command in Bijux CLI.
bijux_cli.commands.utilities ¶
Provides shared, reusable utilities for Bijux CLI commands.
This module centralizes common logic to ensure consistency and reduce code duplication across the various command implementations. It includes a suite of functions for handling standard CLI tasks, such as:
- Validation: Functions for validating common CLI flags (like
--format
) and checking the environment for non-ASCII characters or malformed configuration files. - Output & Exit: A set of high-level emitters (
emit_and_exit
,emit_error_and_exit
) that handle payload serialization (JSON/YAML), pretty-printing, and terminating the application with a contract-compliant exit code and structured message. - Command Orchestration: A primary helper (
new_run_command
) that encapsulates the standard lifecycle of a command: validation, payload construction, and emission. - Parsing & Sanitization: Helpers for sanitizing strings to be ASCII-safe and a pre-parser for global flags (
--quiet
,--debug
, etc.) that operates before Typer's main dispatch. - Plugin Management: Utilities for discovering and listing installed plugins from the filesystem.
ascii_safe ¶
Converts any value to a string containing only printable ASCII characters.
Non-ASCII characters are replaced with '?'. Newlines, carriage returns, and tabs are preserved.
Parameters:
-
text
(Any
) –The value to sanitize.
-
_field
(str
, default:''
) –An unused parameter for potential future use in context or telemetry. Defaults to "".
Returns:
-
str
(str
) –An ASCII-safe string.
Source code in src/bijux_cli/commands/utilities.py
contains_non_ascii_env ¶
Checks for non-ASCII characters in the CLI's environment.
This function returns True if any of the following are detected: * The BIJUXCLI_CONFIG
environment variable contains non-ASCII characters. * The file path pointed to by BIJUXCLI_CONFIG
exists and its contents cannot be decoded as ASCII. * Any environment variable with a name starting with BIJUXCLI_
has a value containing non-ASCII characters.
Returns:
-
bool
(bool
) –True if a non-ASCII condition is found, otherwise False.
Source code in src/bijux_cli/commands/utilities.py
emit_and_exit ¶
emit_and_exit(
payload: Mapping[str, Any],
fmt: OutputFormat,
effective_pretty: bool,
verbose: bool,
debug: bool,
quiet: bool,
command: str,
*,
exit_code: int = 0,
) -> NoReturn
Serializes and emits a payload, records history, and exits.
Parameters:
-
payload
(Mapping[str, Any]
) –The data to serialize and print.
-
fmt
(OutputFormat
) –The output format (JSON or YAML).
-
effective_pretty
(bool
) –If True, pretty-prints the output.
-
verbose
(bool
) –If True, includes runtime info in history records.
-
debug
(bool
) –If True, emits a diagnostic message to stderr.
-
quiet
(bool
) –If True, suppresses all output and exits immediately.
-
command
(str
) –The command name, used for history tracking.
-
exit_code
(int
, default:0
) –The exit status code to use.
Raises:
-
SystemExit
–Always exits the process with
exit_code
.
Source code in src/bijux_cli/commands/utilities.py
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
emit_error_and_exit ¶
emit_error_and_exit(
message: str,
code: int,
failure: str,
command: str | None = None,
fmt: str | None = None,
quiet: bool = False,
include_runtime: bool = False,
debug: bool = False,
extra: dict[str, Any] | None = None,
) -> NoReturn
Emits a structured error payload to stderr and exits the process.
Parameters:
-
message
(str
) –The primary error message.
-
code
(int
) –The exit status code.
-
failure
(str
) –A short, machine-readable failure code.
-
command
(str | None
, default:None
) –The command name where the error occurred.
-
fmt
(str | None
, default:None
) –The output format context.
-
quiet
(bool
, default:False
) –If True, suppresses all output and exits immediately.
-
include_runtime
(bool
, default:False
) –If True, adds runtime info to the error payload.
-
debug
(bool
, default:False
) –If True, prints a full traceback to stderr.
-
extra
(dict[str, Any] | None
, default:None
) –Additional fields to merge into the payload.
Raises:
-
SystemExit
–Always exits the process with the specified
code
.
Source code in src/bijux_cli/commands/utilities.py
handle_list_plugins ¶
handle_list_plugins(
command: str,
quiet: bool,
verbose: bool,
fmt: str,
pretty: bool,
debug: bool,
) -> None
Handles the logic for commands that list installed plugins.
This function serves as a common handler for plugins list
and similar commands. It retrieves the list of plugins and uses new_run_command
to emit the result.
Parameters:
-
command
(str
) –The name of the command being executed.
-
quiet
(bool
) –If True, suppresses normal output.
-
verbose
(bool
) –If True, includes runtime metadata in the payload.
-
fmt
(str
) –The requested output format ("json" or "yaml").
-
pretty
(bool
) –If True, pretty-prints the output.
-
debug
(bool
) –If True, enables debug mode.
Returns:
-
None
(None
) –
Source code in src/bijux_cli/commands/utilities.py
list_installed_plugins ¶
Scans the plugins directory and returns a list of installed plugin names.
A directory is considered a valid plugin if it is a direct child of the plugins directory and contains a plugin.py
file.
Returns:
-
list[str]
–list[str]: A sorted list of valid plugin names.
Raises:
-
RuntimeError
–If the plugins directory is invalid, inaccessible, is not a directory, or contains a symlink loop.
Source code in src/bijux_cli/commands/utilities.py
new_run_command ¶
new_run_command(
command_name: str,
payload_builder: Callable[[bool], Mapping[str, object]],
quiet: bool,
verbose: bool,
fmt: str,
pretty: bool,
debug: bool,
exit_code: int = 0,
) -> NoReturn
Orchestrates the standard execution flow of a CLI command.
This function handles dependency resolution, validation, payload construction, and final emission, ensuring a consistent lifecycle for all commands that use it.
Parameters:
-
command_name
(str
) –The name of the command for telemetry/error context.
-
payload_builder
(Callable[[bool], Mapping[str, object]]
) –A function that takes a boolean
include_runtime
and returns the command's structured output payload. -
quiet
(bool
) –If True, suppresses normal output.
-
verbose
(bool
) –If True, includes runtime metadata in the output.
-
fmt
(str
) –The output format ("json" or "yaml").
-
pretty
(bool
) –If True, pretty-prints the output.
-
debug
(bool
) –If True, enables debug-level output.
-
exit_code
(int
, default:0
) –The exit code to use on successful execution.
Raises:
-
SystemExit
–Always exits the process with the given
exit_code
or an appropriate error code on failure.
Source code in src/bijux_cli/commands/utilities.py
normalize_format ¶
Normalizes a format string to lowercase and removes whitespace.
Parameters:
-
fmt
(str | None
) –The format string to normalize.
Returns:
-
str
(str
) –The normalized format string, or an empty string if input is None.
Source code in src/bijux_cli/commands/utilities.py
parse_global_flags ¶
Parses global CLI flags from sys.argv
before Typer dispatch.
This function inspects and consumes known global flags, rewriting sys.argv
to contain only the remaining arguments. This allows global settings to be processed independently of the command-specific parsing done by Typer.
Returns:
-
dict[str, Any]
–dict[str, Any]: A dictionary of parsed flag values, such as
help
,quiet
,debug
,verbose
,format
, andpretty
.
Raises:
-
SystemExit
–If a flag requires an argument that is missing (e.g.,
--format
with no value).
Source code in src/bijux_cli/commands/utilities.py
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 |
|
validate_common_flags ¶
Validates common CLI flags and environment settings.
This function ensures the format is supported and the environment is ASCII-safe, exiting with a structured error if validation fails.
Parameters:
-
fmt
(str
) –The requested output format.
-
command
(str
) –The name of the command for error reporting context.
-
quiet
(bool
) –If True, suppresses output on error before exiting.
-
include_runtime
(bool
, default:False
) –If True, includes runtime info in error payloads.
Returns:
-
str
(str
) –The validated and normalized format string ("json" or "yaml").
Raises:
-
SystemExit
–Exits with code 2 for an unsupported format or 3 for a non-ASCII environment.
Source code in src/bijux_cli/commands/utilities.py
validate_env_file_if_present ¶
Validates the syntax of an environment configuration file if it exists.
Checks that every non-comment, non-blank line conforms to a KEY=VALUE
pattern.
Parameters:
-
path_str
(str
) –The path to the environment file.
Raises:
-
ValueError
–If the file cannot be read or contains a malformed line.