From b33bdb9a033a4f6174578f2b85aefaf3f569cfb9 Mon Sep 17 00:00:00 2001 From: Tanue Eugen-Bleck Mbunwo Date: Tue, 13 May 2025 21:21:23 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20Display=20full=20help=20text=20for=20adk?= =?UTF-8?q?=20create=20CLI=20command=20when=20argument=E2=80=A6=20Copybara?= =?UTF-8?q?=20import=20of=20the=20project:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- dcfb9ff720562802f9ce15c90a71b4cd40f77280 by Eugen-Bleck : fix: Display full help text for adk create CLI command when arguments are missing -- f2eea8d1e5947b1631ac2de3824d8bf0cf833cbc by Eugen-Bleck : fix: Display full help text for adk run and eval CLI command when arguments are missing COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/443 from bl3ck:fix/cli-help-display c2bca420b00aa3391ab8cfb6baedc9ea0dbfb7e1 PiperOrigin-RevId: 758498242 --- src/google/adk/cli/cli_tools_click.py | 50 +++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/google/adk/cli/cli_tools_click.py b/src/google/adk/cli/cli_tools_click.py index 29476f1..d06d058 100644 --- a/src/google/adk/cli/cli_tools_click.py +++ b/src/google/adk/cli/cli_tools_click.py @@ -34,6 +34,50 @@ from .fast_api import get_fast_api_app from .utils import envs from .utils import logs + +class HelpfulCommand(click.Command): + """Command that shows full help on error instead of just the error message. + + A custom Click Command class that overrides the default error handling + behavior to display the full help text when a required argument is missing, + followed by the error message. This provides users with better context + about command usage without needing to run a separate --help command. + + Args: + *args: Variable length argument list to pass to the parent class. + **kwargs: Arbitrary keyword arguments to pass to the parent class. + + Returns: + None. Inherits behavior from the parent Click Command class. + + Returns: + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def parse_args(self, ctx, args): + """Override the parse_args method to show help text on error. + + Args: + ctx: Click context object for the current command. + args: List of command-line arguments to parse. + + Returns: + The parsed arguments as returned by the parent class's parse_args method. + + Raises: + click.MissingParameter: When a required parameter is missing, but this + is caught and handled by displaying the help text before exiting. + """ + try: + return super().parse_args(ctx, args) + except click.MissingParameter as exc: + click.echo(ctx.get_help()) + click.secho(f"\nError: {str(exc)}", fg="red", err=True) + ctx.exit(2) + + logger = logging.getLogger(__name__) @@ -49,7 +93,7 @@ def deploy(): pass -@main.command("create") +@main.command("create", cls=HelpfulCommand) @click.option( "--model", type=str, @@ -115,7 +159,7 @@ def validate_exclusive(ctx, param, value): return value -@main.command("run") +@main.command("run", cls=HelpfulCommand) @click.option( "--save_session", type=bool, @@ -196,7 +240,7 @@ def cli_run( ) -@main.command("eval") +@main.command("eval", cls=HelpfulCommand) @click.argument( "agent_module_file_path", type=click.Path(