Parse a CLI string value into an appropriate Python type. Parse order (JSON-first): 1. json.loads -- handles true/false, numbers, quoted strings, arrays, objects 2. Legacy bool -- Python-style "True"/"False" (case-insensitive) 3. int / float 4. bare string
(raw: str)
| 74 | |
| 75 | |
| 76 | def _parse_cli_value(raw: str) -> int | float | bool | list[Any] | dict[str, Any] | str | None: |
| 77 | """Parse a CLI string value into an appropriate Python type. |
| 78 | |
| 79 | Parse order (JSON-first): |
| 80 | 1. json.loads -- handles true/false, numbers, quoted strings, arrays, objects |
| 81 | 2. Legacy bool -- Python-style "True"/"False" (case-insensitive) |
| 82 | 3. int / float |
| 83 | 4. bare string |
| 84 | """ |
| 85 | import json |
| 86 | |
| 87 | # Skip json.loads for bare strings that cannot be valid JSON. |
| 88 | # Valid JSON values start with: digit, '-', '"', '[', '{', 't', 'n', 'f' |
| 89 | if raw and (raw[0].isdigit() or raw[0] == "-" or raw[0] in _JSON_START_CHARS): |
| 90 | try: |
| 91 | parsed: int | float | bool | list[Any] | dict[str, Any] | str | None = json.loads(raw) |
| 92 | return parsed |
| 93 | except (ValueError, json.JSONDecodeError): |
| 94 | pass |
| 95 | |
| 96 | if raw.lower() == "true": |
| 97 | return True |
| 98 | if raw.lower() == "false": |
| 99 | return False |
| 100 | |
| 101 | try: |
| 102 | return int(raw) |
| 103 | except ValueError: |
| 104 | pass |
| 105 | |
| 106 | try: |
| 107 | return float(raw) |
| 108 | except ValueError: |
| 109 | pass |
| 110 | |
| 111 | return raw |
no outgoing calls