fix(cli): support equals-syntax and safe float validation for provider pricing flags

This commit is contained in:
Aditya kumar singh committed 2026-11-06 01:08:28 +05:30
1 parent 16218bfc80
commit e39355c7fe
2 files changed
+76 -2

No files matched your search

+28 -2
View File
@@ -1873,9 +1873,35 @@ def dispatch_command(cmd: str) -> None:
elif a.startswith("--env-key="):
env_key = a.split("=", 1)[1]; i += 1
elif a == "--pricing-input" and i + 1 < len(args):
pricing_input = float(args[i + 1]); i += 2
try:
pricing_input = float(args[i + 1])
except ValueError:
print(f"Error: invalid float for --pricing-input: '{args[i + 1]}'", file=sys.stderr)
sys.exit(1)
i += 2
elif a.startswith("--pricing-input="):
val = a.split("=", 1)[1]
try:
pricing_input = float(val)
except ValueError:
print(f"Error: invalid float for --pricing-input: '{val}'", file=sys.stderr)
sys.exit(1)
i += 1
elif a == "--pricing-output" and i + 1 < len(args):
pricing_output = float(args[i + 1]); i += 2
try:
pricing_output = float(args[i + 1])
except ValueError:
print(f"Error: invalid float for --pricing-output: '{args[i + 1]}'", file=sys.stderr)
sys.exit(1)
i += 2
elif a.startswith("--pricing-output="):
val = a.split("=", 1)[1]
try:
pricing_output = float(val)
except ValueError:
print(f"Error: invalid float for --pricing-output: '{val}'", file=sys.stderr)
sys.exit(1)
i += 1
else:
i += 1
if not base_url or not default_model or not env_key:
+48
View File
@@ -162,3 +162,51 @@ def test_detect_backend_custom_provider_after_builtins(monkeypatch):
result = llm.detect_backend()
assert result == "myprovider"
def test_dispatch_command_provider_add_with_pricing_equals(tmp_path, monkeypatch):
"""`graphify provider add` supports --pricing-input=VAL and --pricing-output=VAL syntax."""
import sys
from graphify import cli, llm
providers_file = tmp_path / "providers.json"
monkeypatch.setattr(llm, "_custom_providers_path", lambda global_=True: providers_file)
test_args = [
"graphify", "provider", "add", "mycustom",
"--base-url=http://localhost:8080/v1",
"--default-model=model-x",
"--env-key=MY_KEY",
"--pricing-input=0.15",
"--pricing-output=0.60",
]
monkeypatch.setattr(sys, "argv", test_args)
cli.dispatch_command("provider")
assert providers_file.exists()
data = json.loads(providers_file.read_text(encoding="utf-8"))
assert "mycustom" in data
assert data["mycustom"]["pricing"] == {"input": 0.15, "output": 0.60}
def test_dispatch_command_provider_add_invalid_pricing_float(tmp_path, monkeypatch, capsys):
"""Invalid float for --pricing-input exits with code 1 and prints an error."""
import sys
from graphify import cli, llm
providers_file = tmp_path / "providers.json"
monkeypatch.setattr(llm, "_custom_providers_path", lambda global_=True: providers_file)
test_args = [
"graphify", "provider", "add", "mycustom",
"--base-url=http://localhost:8080/v1",
"--default-model=model-x",
"--env-key=MY_KEY",
"--pricing-input=invalid_number",
]
monkeypatch.setattr(sys, "argv", test_args)
with pytest.raises(SystemExit) as exc:
cli.dispatch_command("provider")
assert exc.value.code == 1
err = capsys.readouterr().err
assert "invalid float for --pricing-input" in err