10000 fix: Display full help text for adk create CLI command when argument… · Syntax404-coder/adk-python@b33bdb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b33bdb9

Browse files
bl3ckcopybara-github
authored andcommitted
fix: Display full help text for adk create CLI command when argument…
Copybara import of the project: -- dcfb9ff by Eugen-Bleck <eugenbleck@gmail.com>: fix: Display full help text for adk create CLI command when arguments are missing -- f2eea8d by Eugen-Bleck <eugenbleck@gmail.com>: fix: Display full help text for adk run and eval CLI command when arguments are missing COPYBARA_INTEGRATE_REVIEW=google#443 from bl3ck:fix/cli-help-display c2bca42 PiperOrigin-RevId: 758498242
1 parent fc40226 commit b33bdb9

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/google/adk/cli/cli_tools_click.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,50 @@
3434
from .utils import envs
3535
from .utils import logs
3636

37+
38+
class HelpfulCommand(click.Command):
39+
"""Command that shows full help on error instead of just the error message.
40+
41+
A custom Click Command class that overrides the default error handling
42+
behavior to display the full help text when a required argument is missing,
43+
followed by the error message. This provides users with better context
44+
about command usage without needing to run a separate --help command.
45+
46+
Args:
47+
*args: Variable length argument list to pass to the parent class.
48+
**kwargs: Arbitrary keyword arguments to pass to the parent class.
49+
50+
Returns:
51+
None. Inherits behavior from the parent Click Command class.
52+
53+
Returns:
54+
"""
55+
56+
def __init__(self, *args, **kwargs):
57+
super().__init__(*args, **kwargs)
58+
59+
def parse_args(self, ctx, args):
60+
"""Override the parse_args method to show help text on error.
61+
62+
Args:
63+
ctx: Click context object for the current command.
64+
args: List of command-line arguments to parse.
65+
66+
Returns:
67+
The parsed arguments as returned by the parent class's parse_args method.
68+
69+
Raises:
70+
click.MissingParameter: When a required parameter is missing, but this
71+
is caught and handled by displaying the help text before exiting.
72+
"""
73+
try:
74+
return super().parse_args(ctx, args)
75+
except click.MissingParameter as exc:
76+
click.echo(ctx.get_help())
77+
click.secho(f"\nError: {str(exc)}", fg="red", err=True)
78+
ctx.exit(2)
79+
80+
3781
logger = logging.getLogger(__name__)
3882

3983

@@ -49,7 +93,7 @@ def deploy():
4993
pass
5094

5195

52-
@main.command("create")
96+
@main.command("create", cls=HelpfulCommand)
5397
@click.option(
5498< AAC9 /td>
"--model",
5599
type=str,
@@ -115,7 +159,7 @@ def validate_exclusive(ctx, param, value):
115159
return value
116160

117161

118-
@main.command("run")
162+
@main.command("run", cls=HelpfulCommand)
119163
@click.option(
120164
"--save_session",
121165
type=bool,
@@ -196,7 +240,7 @@ def cli_run(
196240
)
197241

198242

199-
@main.command("eval")
243+
@main.command("eval", cls=HelpfulCommand)
200244
@click.argument(
201245
"agent_module_file_path",
202246
type=click.Path(

0 commit comments

Comments
 (0)
0