Skip to content

Commit 3723595

Browse files
committed
Hint on missing --text flag
1 parent 991be35 commit 3723595

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/labelle/cli/cli.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
# this notice are preserved.
77
# === END LICENSE STATEMENT ===
88
import logging
9+
import sys
910
from pathlib import Path
1011
from typing import List, NoReturn, Optional
1112

1213
import typer
14+
from click.exceptions import ClickException
15+
from click.exceptions import UsageError as ClickUsageError
1316
from rich.console import Console
1417
from rich.table import Table
18+
from typer.rich_utils import rich_format_error
1519
from typing_extensions import Annotated
1620

1721
from labelle import __version__
@@ -554,9 +558,35 @@ def default(
554558
output_bitmap(bitmap, output)
555559

556560

561+
def add_hint_about_text_option(e: ClickException) -> None:
562+
"""Insert a suggestion to use the --text flag when a command is not found.
563+
564+
In dymoprint the --text option was implicit. If labelle is invoked without
565+
--text, it presents as a ClickUsageError with the message "No such command..."
566+
We append to this error message a hint to use the --text flag.
567+
"""
568+
if isinstance(e, ClickUsageError):
569+
# Enhance the error message for dymoprint users who are
570+
# not used to the --text flag being mandatory.
571+
if e.message.startswith("No such command '") and e.message.endswith("'."):
572+
command = e.message[17:-2]
573+
if " " in command:
574+
command = f'"{command}"'
575+
e.message += f""" Did you mean --text {command} ?"""
576+
577+
557578
def main() -> None:
558579
configure_logging()
559-
app()
580+
try:
581+
app(standalone_mode=False)
582+
except ClickException as e:
583+
# Use standalone mode to avoid typer's default error handling here:
584+
# <https://github.com/tiangolo/typer/blob/773927208fbf03d30d50fc39fe2a1a18b7bd93cb/typer/core.py#L207-L216>
585+
# This allows us to add the following hint:
586+
add_hint_about_text_option(e)
587+
588+
rich_format_error(e)
589+
sys.exit(e.exit_code)
560590

561591

562592
if __name__ == "__main__":

0 commit comments

Comments
 (0)