-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,16 @@ | |
# this notice are preserved. | ||
# === END LICENSE STATEMENT === | ||
import logging | ||
import sys | ||
from pathlib import Path | ||
from typing import List, NoReturn, Optional | ||
|
||
import typer | ||
from click.exceptions import ClickException | ||
from click.exceptions import UsageError as ClickUsageError | ||
from rich.console import Console | ||
from rich.table import Table | ||
from typer.rich_utils import rich_format_error | ||
from typing_extensions import Annotated | ||
|
||
from labelle import __version__ | ||
|
@@ -554,9 +558,35 @@ def default( | |
output_bitmap(bitmap, output) | ||
|
||
|
||
def add_hint_about_text_option(e: ClickException) -> None: | ||
"""Insert a suggestion to use the --text flag when a command is not found. | ||
In dymoprint the --text option was implicit. If labelle is invoked without | ||
--text, it presents as a ClickUsageError with the message "No such command..." | ||
We append to this error message a hint to use the --text flag. | ||
""" | ||
if isinstance(e, ClickUsageError): | ||
# Enhance the error message for dymoprint users who are | ||
# not used to the --text flag being mandatory. | ||
if e.message.startswith("No such command '") and e.message.endswith("'."): | ||
command = e.message[17:-2] | ||
if " " in command: | ||
command = f'"{command}"' | ||
e.message += f""" Did you mean --text {command} ?""" | ||
|
||
|
||
def main() -> None: | ||
configure_logging() | ||
app() | ||
try: | ||
app(standalone_mode=False) | ||
except ClickException as e: | ||
# Use standalone mode to avoid typer's default error handling here: | ||
# <https://github.com/tiangolo/typer/blob/773927208fbf03d30d50fc39fe2a1a18b7bd93cb/typer/core.py#L207-L216> | ||
# This allows us to add the following hint: | ||
add_hint_about_text_option(e) | ||
|
||
rich_format_error(e) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
maresb
Author
Contributor
|
||
sys.exit(e.exit_code) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
1 comment
on commit 3723595
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great! 🔥
Can lines 588-589 be replaced with a mere
raise
?