Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/claude_monitor/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,31 +269,51 @@ def load_with_last_used(cls, argv: Optional[List[str]] = None) -> "Settings":

clear_config = argv and "--clear" in argv

# Parse CLI arguments manually
cli_args = {}
cli_provided_fields = set()
if argv:
i = 0
while i < len(argv):
arg = argv[i]
if arg.startswith("--"):
field_name = arg[2:].replace("-", "_")
if field_name in cls.model_fields:
cli_provided_fields.add(field_name)
# Check if this is a boolean flag or requires a value
if i + 1 < len(argv) and not argv[i + 1].startswith("--"):
# Has a value
cli_args[field_name] = argv[i + 1]
i += 2
else:
# Boolean flag
cli_args[field_name] = True
i += 1
else:
i += 1
else:
i += 1

if clear_config:
last_used = LastUsedParams()
last_used.clear()
settings = cls(_cli_parse_args=argv)
settings = cls(**cli_args)
else:
last_used = LastUsedParams()
last_params = last_used.load()

settings = cls(_cli_parse_args=argv)

cli_provided_fields = set()
if argv:
for _i, arg in enumerate(argv):
if arg.startswith("--"):
field_name = arg[2:].replace("-", "_")
if field_name in cls.model_fields:
cli_provided_fields.add(field_name)

# Merge last_params with cli_args, CLI args take precedence
merged_args = {}
for key, value in last_params.items():
if key == "plan":
continue
if not hasattr(settings, key):
continue
if key not in cli_provided_fields:
setattr(settings, key, value)
merged_args[key] = value

# Add CLI args (they override last_params)
merged_args.update(cli_args)

settings = cls(**merged_args)

if (
"plan" in cli_provided_fields
Expand Down
19 changes: 19 additions & 0 deletions src/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,25 @@ def test_load_with_last_used_custom_plan_reset(
assert settings.plan == "custom"
assert settings.custom_limit_tokens is None # Should be reset

@patch("claude_monitor.core.settings.Settings._get_system_time_format")
@patch("claude_monitor.core.settings.Settings._get_system_timezone")
def test_load_with_last_used_plan_parsing(
self, mock_timezone: Mock, mock_time_format: Mock
) -> None:
"""Test plan parameter is correctly parsed from CLI arguments."""
mock_timezone.return_value = "UTC"
mock_time_format.return_value = "24h"

with patch("claude_monitor.core.settings.LastUsedParams") as MockLastUsed:
mock_instance = Mock()
mock_instance.load.return_value = {}
MockLastUsed.return_value = mock_instance

# Test various plan values
for plan in ["pro", "max5", "max20"]:
settings = Settings.load_with_last_used(["--plan", plan])
assert settings.plan == plan

def test_to_namespace(self) -> None:
"""Test conversion to argparse.Namespace."""
settings = Settings(
Expand Down
Loading