From d5321d109290ff1823d8874c30b9d6ac223b25b3 Mon Sep 17 00:00:00 2001 From: abhas20 Date: Sun, 5 Oct 2025 10:37:45 +0530 Subject: [PATCH 1/3] added error handling --- cli/cli.py | 23 +++++++++++++++++++++-- cli/test_cli.py | 9 +++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 6c979b41..6901b64d 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -3,10 +3,29 @@ """ import typer - +import click +from typing import List from commands import hello, list, search, show, version +from typer.main import TyperGroup + +class CustomTyper(TyperGroup): + def resolve_command(self, ctx: click.Context, args:List[str]): + try: + return super().resolve_command(ctx, args) + except click.exceptions.UsageError as e: + original = e.format_message() + + if "No such command" in original: + script_name = ctx.find_root().info_name or "cli" + hint = f"💡 Hint: Run '{script_name} --help' to see available commands." + + new_message = f"{original}\n{hint}" + raise click.exceptions.UsageError(new_message, ctx=ctx) from e + + raise + -app = typer.Typer(help="101 Linux Commands CLI 🚀") +app = typer.Typer(help="101 Linux Commands CLI 🚀",cls=CustomTyper) app.add_typer(hello.app, name="hello") app.add_typer(list.app, name="list") app.add_typer(version.app, name="version") diff --git a/cli/test_cli.py b/cli/test_cli.py index 946f7690..c423779f 100644 --- a/cli/test_cli.py +++ b/cli/test_cli.py @@ -70,6 +70,14 @@ def test_version_show_command(): assert "101-linux v0.1.0" in result.stdout +def test_unknown_command(): + """Test that an unknown command shows a helpful error.""" + result = run_cli(["unknowncmd"]) + assert result.returncode != 0 + combined_output = result.stdout + result.stderr + assert "No such command" in combined_output + assert "Hint: Run 'cli.py --help' to see available commands." in combined_output + # ---------------------------- # Tests for `show` subcommand # ---------------------------- @@ -138,4 +146,5 @@ def test_search_no_match(): test_show_invalid() test_search_match() test_search_no_match() + test_unknown_command() print("✅ All tests passed!") From 628552461aa8a5b3df30d077291e58b63519862f Mon Sep 17 00:00:00 2001 From: abhas20 Date: Tue, 7 Oct 2025 02:54:43 +0530 Subject: [PATCH 2/3] fixed the issues --- cli/cli.py | 17 ++++++++++------- cli/test_cli.py | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 6901b64d..f0a5e2a7 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -2,14 +2,17 @@ CLI entry point for the 101 Linux Commands application. """ -import typer -import click from typing import List -from commands import hello, list, search, show, version + +import click +import typer from typer.main import TyperGroup +from commands import hello, list, search, show, version + + class CustomTyper(TyperGroup): - def resolve_command(self, ctx: click.Context, args:List[str]): + def resolve_command(self, ctx: click.Context, args: List[str]): try: return super().resolve_command(ctx, args) except click.exceptions.UsageError as e: @@ -21,11 +24,11 @@ def resolve_command(self, ctx: click.Context, args:List[str]): new_message = f"{original}\n{hint}" raise click.exceptions.UsageError(new_message, ctx=ctx) from e - + raise - -app = typer.Typer(help="101 Linux Commands CLI 🚀",cls=CustomTyper) + +app = typer.Typer(help="101 Linux Commands CLI 🚀", cls=CustomTyper) app.add_typer(hello.app, name="hello") app.add_typer(list.app, name="list") app.add_typer(version.app, name="version") diff --git a/cli/test_cli.py b/cli/test_cli.py index c423779f..f3a269fd 100644 --- a/cli/test_cli.py +++ b/cli/test_cli.py @@ -78,6 +78,7 @@ def test_unknown_command(): assert "No such command" in combined_output assert "Hint: Run 'cli.py --help' to see available commands." in combined_output + # ---------------------------- # Tests for `show` subcommand # ---------------------------- From cfec7f33002c66a328c2a745965a8383dd8d4a54 Mon Sep 17 00:00:00 2001 From: abhas20 Date: Tue, 7 Oct 2025 18:11:00 +0530 Subject: [PATCH 3/3] updated the test --- cli/test_cli.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cli/test_cli.py b/cli/test_cli.py index f3a269fd..979c5cb6 100644 --- a/cli/test_cli.py +++ b/cli/test_cli.py @@ -2,6 +2,7 @@ """Basic tests for the CLI module.""" import os +import re import subprocess import sys @@ -70,13 +71,24 @@ def test_version_show_command(): assert "101-linux v0.1.0" in result.stdout +ANSI_ESCAPE = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]") +EMOJI = re.compile("[\U0001f300-\U0001faff]", flags=re.UNICODE) + + +def clean_output(text: str) -> str: + """Remove ANSI colors and emojis.""" + text = ANSI_ESCAPE.sub("", text) + text = EMOJI.sub("", text) + return text + + def test_unknown_command(): - """Test that an unknown command shows a helpful error.""" result = run_cli(["unknowncmd"]) - assert result.returncode != 0 combined_output = result.stdout + result.stderr - assert "No such command" in combined_output - assert "Hint: Run 'cli.py --help' to see available commands." in combined_output + clean = clean_output(combined_output) + + assert "No such command" in clean + assert "Hint: Run 'cli.py --help' to see available commands." in clean # ----------------------------