cleaned up /exit, normalized /help

This commit is contained in:
Paul Gauthier 2023-05-26 12:21:43 -07:00
parent c40d763bce
commit 4e557baad4

View file

@ -3,7 +3,6 @@ import os
import git
import subprocess
import shlex
from rich.prompt import Confirm
from prompt_toolkit.completion import Completion
from aider import prompts
@ -17,20 +16,8 @@ class Commands:
if inp[0] == "/":
return True
def help(self):
"Show help about all commands"
commands = self.get_commands()
for cmd in commands:
cmd_method_name = f"cmd_{cmd[1:]}"
cmd_method = getattr(self, cmd_method_name, None)
if cmd_method:
description = cmd_method.__doc__
self.io.tool(f"{cmd} {description}")
else:
self.io.tool(f"{cmd} No description available.")
def get_commands(self):
commands = ["/help", "/exit"]
commands = []
for attr in dir(self):
if attr.startswith("cmd_"):
commands.append("/" + attr[4:])
@ -63,17 +50,15 @@ class Commands:
all_commands = self.get_commands()
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
if len(matching_commands) == 1:
if matching_commands[0] == "/help":
self.help()
else:
return self.do_run(matching_commands[0][1:], rest_inp)
elif len(matching_commands) > 1:
self.io.tool_error("Ambiguous command: ', '.join(matching_commands)}")
elif first_word == "/exit":
self.cmd_exit()
self.io.tool_error(f"Ambiguous command: {', '.join(matching_commands)}")
else:
self.io.tool_error(f"Error: {first_word} is not a valid command.")
# any method called cmd_xxx becomes a command automatically.
# each one must take an args param.
def cmd_commit(self, args):
"Commit edits to the repo made outside the chat (commit message optional)"
@ -254,7 +239,7 @@ class Commands:
)
return msg
def cmd_exit(self):
def cmd_exit(self, args):
"Exit the application"
sys.exit()
@ -281,3 +266,15 @@ class Commands:
self.io.tool("\nRepo files not in the chat:\n")
for file in other_files:
self.io.tool(f" {file}")
def cmd_help(self, args):
"Show help about all commands"
commands = sorted(self.get_commands())
for cmd in commands:
cmd_method_name = f"cmd_{cmd[1:]}"
cmd_method = getattr(self, cmd_method_name, None)
if cmd_method:
description = cmd_method.__doc__
self.io.tool(f"{cmd} {description}")
else:
self.io.tool(f"{cmd} No description available.")