Skip to content

Commit

Permalink
Fix: cli flag parsing (AbanteAI#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakethekoenig authored Nov 3, 2023
1 parent 30feb64 commit e79d664
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
28 changes: 11 additions & 17 deletions mentat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path

import attr
from attr import validators
from attr import converters, validators

from mentat.git_handler import get_shared_git_root_for_paths
from mentat.parsers.parser import Parser
Expand All @@ -18,14 +18,6 @@
user_config_path = mentat_dir_path / config_file_name


def parse_bool(s: str | bool | None) -> bool:
if isinstance(s, bool):
return s
if s is None:
return False
return s.lower() in ("true", "1", "t", "y", "yes")


def int_or_none(s: str | None) -> int | None:
if s is not None:
return int(s)
Expand Down Expand Up @@ -76,7 +68,7 @@ class Config:
metadata={
"description": "Fetch/compare embeddings to auto-generate code context"
},
converter=parse_bool,
converter=converters.optional(converters.to_bool),
)
no_code_map: bool = attr.field(
default=False,
Expand All @@ -85,7 +77,7 @@ class Config:
"Exclude the file structure/syntax map from the system prompt"
)
},
converter=parse_bool,
converter=converters.optional(converters.to_bool),
)
auto_tokens: int | None = attr.field(
default=0,
Expand Down Expand Up @@ -155,12 +147,14 @@ def create(cls, args: Namespace | None = None) -> Config:
return config

def load_namespace(self, args: Namespace) -> None:
for field in vars(args):
if hasattr(self, field) and getattr(args, field) is not None:
try:
setattr(self, field, getattr(args, field))
except (ValueError, TypeError) as e:
self.error(f"Warning: Illegal value for {field}: {e}")
for field in attr.fields(Config):
if field.name in args and field.name != "_errors":
value = getattr(args, field.name)
if value is not None and value != field.default:
try:
setattr(self, field.name, value)
except (ValueError, TypeError) as e:
self.error(f"Warning: Illegal value for {field}: {e}")

def load_file(self, path: Path) -> None:
if path.exists():
Expand Down
6 changes: 4 additions & 2 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ async def test_config_creation():
assert args.parser is None
assert args.use_embeddings
assert args.auto_tokens == "2"
assert not args.no_code_map

with open(config_file_name, "w") as project_config_file:
project_config_file.write(dedent("""\
{
"input_style": [[ "project", "yes" ]]
"input_style": [[ "project", "yes" ]],
"no_code_map": true
}"""))

mentat.config.user_config_path = Path(str(config_file_name) + "1")
Expand All @@ -58,9 +60,9 @@ async def test_config_creation():
assert config.maximum_context == 1
assert type(config.parser) == ReplacementParser
assert config.use_embeddings
assert not config.no_code_map
assert config.auto_tokens == 2
assert config.input_style == [["project", "yes"]]
assert config.no_code_map


@pytest.mark.asyncio
Expand Down

0 comments on commit e79d664

Please sign in to comment.