Commit a509b8f6 authored by Neelay Shah's avatar Neelay Shah Committed by GitHub
Browse files

feat: updated to override command line and support non equals and space in … (#182)

parent a2773f3e
...@@ -63,48 +63,63 @@ def deprecated_option(*param_decls: str, **attrs: t.Any): ...@@ -63,48 +63,63 @@ def deprecated_option(*param_decls: str, **attrs: t.Any):
return decorator return decorator
def _parse_service_arg(arg: str) -> tuple[str, str, t.Any] | None: def _parse_service_arg(arg_name: str, arg_value: str) -> tuple[str, str, t.Any]:
"""Parse a single CLI argument into service name, key, and value.""" """Parse a single CLI argument into service name, key, and value."""
if not (arg.startswith("--") and "=" in arg):
return None
# Remove leading dashes service, key = arg_name.split(".", 1)
param = arg[2:]
key_path, value_str = param.split("=", 1)
if "." not in key_path:
return None
service, key = key_path.split(".", 1)
# Parse value based on type # Parse value based on type
try: try:
# Try as JSON for complex types # Try as JSON for complex types
value = json.loads(value_str) value = json.loads(arg_value)
except json.JSONDecodeError: except json.JSONDecodeError:
# Handle basic types # Handle basic types
if value_str.isdigit(): if arg_value.isdigit():
value = int(value_str) value = int(arg_value)
elif value_str.replace(".", "", 1).isdigit() and value_str.count(".") <= 1: elif arg_value.replace(".", "", 1).isdigit() and arg_value.count(".") <= 1:
value = float(value_str) value = float(arg_value)
elif value_str.lower() in ("true", "false"): elif arg_value.lower() in ("true", "false"):
value = value_str.lower() == "true" value = arg_value.lower() == "true"
else: else:
value = value_str value = arg_value
return service, key, value return service, key, value
def _parse_service_args(args: list[str]) -> t.Dict[str, t.Any] | None: def _parse_service_args(args: list[str]) -> t.Dict[str, t.Any]:
service_configs: t.DefaultDict[str, t.Dict[str, t.Any]] = collections.defaultdict( service_configs: t.DefaultDict[str, t.Dict[str, t.Any]] = collections.defaultdict(
dict dict
) )
for arg in args: index = 0
parsed = _parse_service_arg(arg) while index < len(args):
if parsed: next_arg = args[index]
arg_name = ""
arg_value = ""
if not (next_arg.startswith("--") or "." not in next_arg):
continue
try:
if "=" in next_arg:
arg_name, arg_value = next_arg.split("=", 1)
index += 1
elif args[index + 1] == "=":
arg_name = next_arg
arg_value = args[index + 2]
index += 3
else:
arg_name = next_arg
arg_value = args[index + 1]
index += 2
if arg_value.startswith("-"):
raise ValueError("Service arg value can not start with -")
arg_name = arg_name[2:]
parsed = _parse_service_arg(arg_name, arg_value)
service, key, value = parsed service, key, value = parsed
service_configs[service][key] = value service_configs[service][key] = value
except Exception:
raise ValueError(f"Error parsing service arg: {args[index]}")
return service_configs return service_configs
...@@ -331,15 +346,13 @@ def build_serve_command() -> click.Group: ...@@ -331,15 +346,13 @@ def build_serve_command() -> click.Group:
from dynamo.sdk.lib.service import LinkedServices from dynamo.sdk.lib.service import LinkedServices
# Process service-specific options service_configs: dict[str, dict[str, t.Any]] = {}
service_configs: t.Optional[t.Dict[str, t.Any]] = _parse_service_args(ctx.args)
# Load and merge config file if provided # Load file if provided
if file: if file:
with open(file) as f: with open(file) as f:
yaml_configs = yaml.safe_load(f) yaml_configs = yaml.safe_load(f)
# Initialize service_configs as empty dict if it's None # Initialize service_configs as empty dict if it's None
service_configs = dict(service_configs or {})
# Convert nested YAML structure to flat dict with dot notation # Convert nested YAML structure to flat dict with dot notation
for service, configs in yaml_configs.items(): for service, configs in yaml_configs.items():
for key, value in configs.items(): for key, value in configs.items():
...@@ -347,7 +360,15 @@ def build_serve_command() -> click.Group: ...@@ -347,7 +360,15 @@ def build_serve_command() -> click.Group:
service_configs[service] = {} service_configs[service] = {}
service_configs[service][key] = value service_configs[service][key] = value
# print("service_configs", service_configs) # Process Overrides
# Process service-specific options
cmdline_overrides: t.Dict[str, t.Any] = _parse_service_args(ctx.args)
for service, configs in cmdline_overrides.items():
for key, value in configs.items():
if service not in service_configs:
service_configs[service] = {}
service_configs[service][key] = value
# Set environment variable with service configuration # Set environment variable with service configuration
if service_configs: if service_configs:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment