Improve UX when wrong command is selected (#3179)

* improve error when wrong command is input

* lint

* fix pylint

* lint

* Tests : fixing ArgumentParser issue on MacOS CI

* Typing

Co-authored-by: minhhoang1023 <40023817+minhhoang1023@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
This commit is contained in:
DidierRLopes
2022-11-03 15:05:01 +00:00
committed by GitHub
parent be3f95a92c
commit 0ae672b821
2 changed files with 15 additions and 11 deletions

View File

@@ -134,9 +134,10 @@ class BaseController(metaclass=ABCMeta):
self.completer: Union[None, NestedCompleter] = None
self.parser = argparse.ArgumentParser(
add_help=False, prog=self.path[-1] if self.PATH != "/" else "terminal"
add_help=False,
prog=self.path[-1] if self.PATH != "/" else "terminal",
)
self.parser.exit_on_error = False # type: ignore
self.parser.add_argument("cmd", choices=self.controller_choices)
theme.applyMPLstyle()
@@ -325,7 +326,12 @@ class BaseController(metaclass=ABCMeta):
# Single command fed, process
else:
(known_args, other_args) = self.parser.parse_known_args(an_input.split())
try:
(known_args, other_args) = self.parser.parse_known_args(
an_input.split()
)
except Exception as exc:
raise SystemExit from exc
if RECORD_SESSION:
SESSION_RECORDED.append(an_input)
@@ -344,7 +350,6 @@ class BaseController(metaclass=ABCMeta):
set_command_location(f"{self.PATH}{known_args.cmd}")
self.log_cmd_and_queue(known_args.cmd, ";".join(other_args), an_input)
# This is what mutes portfolio issue
getattr(
self,
"call_" + known_args.cmd,
@@ -871,8 +876,7 @@ class BaseController(metaclass=ABCMeta):
self.PATH,
)
console.print(
f"\nThe command '{an_input}' doesn't exist on the {self.PATH} menu.\n",
end="",
f"[red]The command '{an_input}' doesn't exist on the {self.PATH} menu.[/red]",
)
similar_cmd = difflib.get_close_matches(
an_input.split(" ")[0] if " " in an_input else an_input,
@@ -890,18 +894,18 @@ class BaseController(metaclass=ABCMeta):
self.queue = []
console.print("\n")
continue
an_input = candidate_input
else:
an_input = similar_cmd[0]
if not self.contains_keys(an_input):
logger.warning("Replacing by %s", an_input)
console.print(f" Replacing by '{an_input}'.")
console.print(f"\n[green]Replacing by '{an_input}'.[/green]\n")
self.queue.insert(0, an_input)
else:
if self.TRY_RELOAD and obbff.RETRY_WITH_LOAD:
console.print(f"Trying `load {an_input}`")
console.print(f"\nTrying `load {an_input}`\n")
self.queue.insert(0, "load " + an_input)
console.print("")
class StockBaseController(BaseController, metaclass=ABCMeta):

View File

@@ -917,7 +917,7 @@ def terminal(jobs_cmds: List[str] = None, test_mode=False):
an_input,
)
console.print(
f"\nThe command '{an_input}' doesn't exist on the / menu", end=""
f"[red]The command '{an_input}' doesn't exist on the / menu.[/red]",
)
similar_cmd = difflib.get_close_matches(
an_input.split(" ")[0] if " " in an_input else an_input,
@@ -939,7 +939,7 @@ def terminal(jobs_cmds: List[str] = None, test_mode=False):
else:
an_input = similar_cmd[0]
console.print(f" Replacing by '{an_input}'.")
console.print(f"\n[green]Replacing by '{an_input}'.[/green]")
t_controller.queue.insert(0, an_input)
else:
console.print("\n")