aider: Refactored cmd_run method to use shlex.split() instead of args.split() to correctly parse arguments, respecting quotes and other special characters.

# Context:
USER: is there a better way to parse args than args.split(), like a shell would, to respect quotes, etc
ASSISTANT: Yes, you can use the `shlex` module to split the arguments like a shell would, respecting quotes and other special characters. We will modify the `cmd_run` method in the `aider/commands.py` file to use `shlex.split()` instead of `args.split()`.

Here are the changes to `aider/commands.py`:

```python
aider/commands.py
<<<<<<< ORIGINAL
  3 import subprocess
=======
  3 import subprocess
  4 import shlex
>>>>>>> UPDATED
```

```python
aider/commands.py
<<<<<<< ORIGINAL
230         try:
231             result = subprocess.run(args.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
=======
230         try:
231             parsed_args = shlex.split(args)
232             result = subprocess.run(parsed_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
>>>>>>> UPDATED
```

These changes import the `shlex` module and replace `args.split()` with `shlex.split(args)` in the `cmd_run` method. This will correctly parse the arguments, respecting quotes and other special characters.
This commit is contained in:
Paul Gauthier 2023-05-18 10:01:24 -07:00
parent f21857655e
commit aaf8c9e811

View file

@ -1,6 +1,7 @@
1 import os
2 import git
3 import subprocess
4 import shlex
from rich.prompt import Confirm
from prompt_toolkit.completion import Completion
from aider import prompts
@ -228,7 +229,8 @@ class Commands:
228 def cmd_run(self, args):
229 "Run the supplied command in a subprocess and combine stdout and stderr into a single string"
230 try:
231 result = subprocess.run(args.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
231 parsed_args = shlex.split(args)
232 result = subprocess.run(parsed_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
232 combined_output = result.stdout + result.stderr
233 print(combined_output)
234 except Exception as e: