MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / parse_params

Function parse_params

examples/API Test/test_api.py:771–830  ·  view source on GitHub ↗

Parse parameters from command line. Supports two formats: - JSON: '{"key": "value"}' - Key=Value pairs: key=value key2=value2 For key=value, attempts to auto-convert types: - "true"/"false" -> bool - integers -> int - floats -> float - everything els

(param_list: list[str])

Source from the content-addressed store, hash-verified

769
770
771def parse_params(param_list: list[str]) -> Optional[dict]:
772 """
773 Parse parameters from command line.
774 Supports two formats:
775 - JSON: '{"key": "value"}'
776 - Key=Value pairs: key=value key2=value2
777
778 For key=value, attempts to auto-convert types:
779 - "true"/"false" -> bool
780 - integers -> int
781 - floats -> float
782 - everything else -> string
783 """
784 if not param_list:
785 return None
786
787 joined = " ".join(param_list)
788 stripped = joined.strip()
789 if stripped.startswith("{"):
790 try:
791 return json.loads(stripped)
792 except json.JSONDecodeError:
793 # PowerShell often strips quotes around keys -- fix {key:value}
794 import re
795
796 fixed = re.sub(
797 r"(\{|,)\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:", r'\1"\2":', stripped
798 )
799 try:
800 return json.loads(fixed)
801 except json.JSONDecodeError as e:
802 raise ValueError(
803 f"Invalid JSON: {e}\n"
804 "Tip: On PowerShell, use key=value format instead: -p baudRate=115200"
805 )
806
807 params = {}
808 for param in param_list:
809 if "=" not in param:
810 raise ValueError(
811 f"Invalid parameter format: '{param}'. Use key=value or JSON format."
812 )
813 key, value = param.split("=", 1)
814 key = key.strip()
815 value = value.strip()
816
817 if value.lower() == "true":
818 params[key] = True
819 elif value.lower() == "false":
820 params[key] = False
821 else:
822 try:
823 params[key] = int(value)
824 except ValueError:
825 try:
826 params[key] = float(value)
827 except ValueError:
828 params[key] = value

Callers 1

cmd_sendFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected