aider: Added /exit command to aider/commands.py.

# Context:
USER: add an /exit command which calls sys.exit
ASSISTANT: To add an `/exit` command, we need to modify the `commands.py` file to add a new command method and update the `is_command` and `run` methods to recognize and execute the new command. We also need to import `sys` to use `sys.exit()`.

Here are the changes:

```python
aider/commands.py
<<<<<<< ORIGINAL
from . import io
=======
import sys
from . import io
>>>>>>> UPDATED
```

```python
aider/commands
^C KeyboardInterrupt
USER: I added the content of these additional files: aider/commands.py
ASSISTANT: I apologize for the confusion. Here are the changes to add the `/exit` command to the `aider/commands.py` file:

```python
aider/commands.py
<<<<<<< ORIGINAL
import os
import git
import subprocess
import shlex
=======
import sys
import os
import git
import subprocess
import shlex
>>>>>>> UPDATED
```

```python
aider/commands.py
<<<<<<< ORIGINAL
        commands = ["/help"]
=======
        commands = ["/help", "/exit"]
>>>>>>> UPDATED
```

```python
aider/commands.py
<<<<<<< ORIGINAL
        else:
            self.io.tool_error(f"Error: {first_word} is not a valid command.")
=======
        elif first_word == "/exit":
            self.cmd_exit()
        else:
            self.io.tool_error(f"Error: {first_word} is not a valid command.")
>>>>>>> UPDATED
```

```python
aider/commands.py
<<<<<<< ORIGINAL
    def cmd_ls(self, args):
        "List all known files and those included in the chat session"
=======
    def cmd_exit(self):
        "Exit the application"
        sys.exit()

    def cmd_ls(self, args):
        "List all known files and those included in the chat session"
>>>>>>> UPDATED
```

These changes will add the `/exit` command to the list of commands, recognize it when entered, and call the `cmd_exit` method to exit the application using `sys.exit()`.
This commit is contained in:
Paul Gauthier 2023-05-26 12:16:59 -07:00
parent 7cc9cdfb21
commit c40d763bce

View file

@ -1,3 +1,4 @@
import sys
import os
import git
import subprocess
@ -29,7 +30,7 @@ class Commands:
self.io.tool(f"{cmd} No description available.")
def get_commands(self):
commands = ["/help"]
commands = ["/help", "/exit"]
for attr in dir(self):
if attr.startswith("cmd_"):
commands.append("/" + attr[4:])
@ -68,6 +69,8 @@ class Commands:
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()
else:
self.io.tool_error(f"Error: {first_word} is not a valid command.")
@ -251,6 +254,10 @@ class Commands:
)
return msg
def cmd_exit(self):
"Exit the application"
sys.exit()
def cmd_ls(self, args):
"List all known files and those included in the chat session"