Skip to content

πŸ™Œ OpenHands: AI-Driven Development

use argparse in main.py (#62)

geohotstanfd277666f686
diff --git a/agent/build-and-run.sh b/agent/build-and-run.sh
index f2fe4e76ff..8e29d82025 100755
--- a/agent/build-and-run.sh
+++ b/agent/build-and-run.sh
@@ -5,5 +5,5 @@ rm -rf `pwd`/workspace
mkdir -p `pwd`/workspace
docker build -t control-loop .
-docker run -e DEBUG=$DEBUG -e OPENAI_API_KEY=$OPENAI_API_KEY -v `pwd`/workspace:/workspace control-loop python /app/main.py /workspace "${1}"
+docker run -e DEBUG=$DEBUG -e OPENAI_API_KEY=$OPENAI_API_KEY -v `pwd`/workspace:/workspace control-loop python /app/main.py -d /workspace -t "${1}"
diff --git a/agent/main.py b/agent/main.py
index 0f66c49ee0..a8c34f92ef 100644
--- a/agent/main.py
+++ b/agent/main.py
@@ -1,17 +1,10 @@
-import sys
import os
+import argparse
from lib.agent import Agent
from lib.event import Event
from lib.controlloop import run_loop
-if len(sys.argv) < 3:
- print("Usage: python main.py <working_directory> <task>")
- sys.exit(1)
-
-working_directory = sys.argv[1]
-task = sys.argv[2]
-
INITIAL_THOUGHTS = [
"I exist!",
"Hmm...looks like I can type in a command line prompt",
@@ -51,13 +44,18 @@ INITIAL_THOUGHTS = [
]
def main():
- print("Working in directory:", sys.argv[1])
- os.chdir(working_directory)
+ parser = argparse.ArgumentParser(description="Run an agent with a specific task")
+ parser.add_argument("-d", "--directory", required=True, type=str, help="The working directory for the agent")
+ parser.add_argument("-t", "--task", required=True, type=str, help="The task for the agent to perform")
+ args = parser.parse_args()
+
+ print("Working in directory:", args.directory)
+ os.chdir(args.directory)
- agent = Agent(task)
+ agent = Agent(args.task)
next_is_output = False
for thought in INITIAL_THOUGHTS:
- thought = thought.replace("$TASK", task)
+ thought = thought.replace("$TASK", args.task)
if next_is_output:
event = Event('output', {'output': thought})
next_is_output = False
diff --git a/agent/regression/run.sh b/agent/regression/run.sh
index dd2b74cb22..715f2a65eb 100755
--- a/agent/regression/run.sh
+++ b/agent/regression/run.sh
@@ -21,6 +21,6 @@ for case in $(ls $CASES_DIR); do
else
mkdir $case_dir/workspace
fi
- docker run -e DEBUG=$DEBUG -e OPENAI_API_KEY=$OPENAI_API_KEY -v $case_dir/workspace:/workspace control-loop python /app/main.py /workspace "${task}" | tee $case_dir/logs.txt
+ docker run -e DEBUG=$DEBUG -e OPENAI_API_KEY=$OPENAI_API_KEY -v $case_dir/workspace:/workspace control-loop python /app/main.py -d /workspace -t "${task}" | tee $case_dir/logs.txt
rm -rf $case_dir/workspace/.git
done