Generate Pretty Value Errors similar to option/argument validation #1234
-
First Check
Commit to Help
Example Codecurrently I raise the following:
if private_key and password:
print(
"Contradicting options by setting both --password and --private-key. ."
)
raise typer.Exit(code=1) DescriptionIs there a way to generate your own errors similar to the option/argument validation? Example for if a file does not exist: uv run netcollector collect -i tests/inventory.yamll
Usage: netcollector collect [OPTIONS]
Try 'netcollector collect --help' for help.
╭─ Error ──────────────────────────────────────────────────────────────────────────────────╮
│ Invalid value for '--inventory-file' / '-i': File 'tests/inventory.yamll' does not exist.│
╰──────────────────────────────────────────────────────────────────────────────────────────╯ I'd love to generate something like this: NETCOLLECTOR_USER=uv run netcollector collect --private-key test.key --password test
Usage: netcollector collect [OPTIONS]
Try 'netcollector collect --help' for help.
╭─ Error ──────────────────────────────────────────────────────────────────────────────────╮
│ Contradicting options by setting both --password and --private-key. │
╰──────────────────────────────────────────────────────────────────────────────────────────╯ Operating SystemLinux, Windows, macOS Operating System DetailsNo response Typer Version0.15.4 Python VersionPython 3.12.10 Additional ContextThanks! |
Beta Was this translation helpful? Give feedback.
Answered by
ryanmerolle
May 28, 2025
Replies: 1 comment
-
I used import click
from typer import rich_utils
class ContradictingOptionsError(click.ClickException):
def __init__(self, message: str) -> None:
super().__init__(message)
self.exit_code = 1
rich_utils.rich_format_error(
ContradictingOptionsError(
"Contradicting options by setting both '--password' / '-p' and '--private-key' / '-pk'."
)
) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ryanmerolle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used
rich_utils.rich_format_error
.