fix: misleading CLI error message for missing command (#25049)

This commit is contained in:
Ankush Menat 2024-03-04 16:15:46 +05:30 committed by GitHub
parent 7d7468c45f
commit ef5a173db0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View file

@ -1010,3 +1010,10 @@ class TestSchedulerCLI(BaseTestCommands):
self.execute("bench --site {site} scheduler resume")
self.assertEqual(self.returncode, 0)
self.assertRegex(self.stdout, r"Scheduler is resumed for site .*")
class TestCLIImplementation(BaseTestCommands):
def test_missing_commands(self):
self.execute("bench --site {site} migrat")
self.assertNotEqual(self.returncode, 0)
self.assertRegex(self.stderr, r"No such.*migrat.*migrate")

View file

@ -3,7 +3,6 @@ import json
import os
import traceback
import warnings
from pathlib import Path
from textwrap import dedent
import click
@ -14,10 +13,25 @@ import frappe.utils
click.disable_unicode_literals_warning = True
class FrappeCommandGroup(click.Group):
def get_command(self, ctx, cmd_name):
rv = super().get_command(ctx, cmd_name)
if rv is not None:
return rv
all_commands = self.list_commands(ctx)
from difflib import get_close_matches
possibilities = get_close_matches(cmd_name, all_commands)
raise click.NoSuchOption(
cmd_name, possibilities=possibilities, message=f"No such command: {cmd_name}"
)
def main():
commands = get_app_groups()
commands.update({"get-frappe-commands": get_frappe_commands, "get-frappe-help": get_frappe_help})
click.Group(commands=commands)(prog_name="bench")
FrappeCommandGroup(commands=commands)(prog_name="bench")
def get_app_groups() -> dict[str, click.Group]:
@ -27,7 +41,7 @@ def get_app_groups() -> dict[str, click.Group]:
for app in get_apps():
if app_commands := get_app_commands(app):
commands |= app_commands
return dict(frappe=click.group(name="frappe", commands=commands)(app_group))
return dict(frappe=click.group(name="frappe", commands=commands, cls=FrappeCommandGroup)(app_group))
def get_app_group(app: str) -> click.Group: