Skip to content

πŸ™Œ OpenHands: AI-Driven Development

(feat) allow specification of config.toml location via args (solves #3947) (#4168)

Rehan Ganapathyc8a933590ac9
diff --git a/openhands/core/cli.py b/openhands/core/cli.py
index 7c4380a27a..fafad7e116 100644
--- a/openhands/core/cli.py
+++ b/openhands/core/cli.py
@@ -91,7 +91,7 @@ async def main():
return
logger.setLevel(logging.WARNING)
- config = load_app_config()
+ config = load_app_config(config_file=args.config_file)
sid = 'cli'
agent_cls: Type[Agent] = Agent.get_cls(config.default_agent)
diff --git a/openhands/core/config/utils.py b/openhands/core/config/utils.py
index 1026d26c01..15b64eb6d6 100644
--- a/openhands/core/config/utils.py
+++ b/openhands/core/config/utils.py
@@ -281,6 +281,12 @@ def get_llm_config_arg(
def get_parser() -> argparse.ArgumentParser:
"""Get the parser for the command line arguments."""
parser = argparse.ArgumentParser(description='Run an agent with a specific task')
+ parser.add_argument(
+ '--config-file',
+ type=str,
+ default='config.toml',
+ help='Path to the config file (default: config.toml in the current directory)',
+ )
parser.add_argument(
'-d',
'--directory',
@@ -375,14 +381,17 @@ def parse_arguments() -> argparse.Namespace:
return parsed_args
-def load_app_config(set_logging_levels: bool = True) -> AppConfig:
- """Load the configuration from the config.toml file and environment variables.
+def load_app_config(
+ set_logging_levels: bool = True, config_file: str = 'config.toml'
+) -> AppConfig:
+ """Load the configuration from the specified config file and environment variables.
Args:
set_logger_levels: Whether to set the global variables for logging levels.
+ config_file: Path to the config file. Defaults to 'config.toml' in the current directory.
"""
config = AppConfig()
- load_from_toml(config)
+ load_from_toml(config, config_file)
load_from_env(config, os.environ)
finalize_config(config)
if set_logging_levels:
diff --git a/openhands/core/main.py b/openhands/core/main.py
index 2b03f2f7a9..2e34dc643f 100644
--- a/openhands/core/main.py
+++ b/openhands/core/main.py
@@ -228,7 +228,7 @@ if __name__ == '__main__':
# Load the app config
# this will load config from config.toml in the current directory
# as well as from the environment variables
- config = load_app_config()
+ config = load_app_config(config_file=args.config_file)
# Override default LLM configs ([llm] section in config.toml)
if args.llm_config:
diff --git a/tests/unit/test_arg_parser.py b/tests/unit/test_arg_parser.py
index 22b22708fd..6788904ced 100644
--- a/tests/unit/test_arg_parser.py
+++ b/tests/unit/test_arg_parser.py
@@ -123,10 +123,11 @@ def test_help_message(capsys):
'--eval-ids EVAL_IDS',
'-l LLM_CONFIG, --llm-config LLM_CONFIG',
'-n NAME, --name NAME',
+ '--config-file CONFIG_FILE',
]
for element in expected_elements:
assert element in help_output, f"Expected '{element}' to be in the help message"
option_count = help_output.count(' -')
- assert option_count == 14, f'Expected 14 options, found {option_count}'
+ assert option_count == 15, f'Expected 15 options, found {option_count}'