Parse a global variable string in the format 'NAME=VALUE
(global_string)
| 134 | |
| 135 | |
| 136 | def parse_global_variable(global_string): |
| 137 | """Parse a global variable string in the format 'NAME=VALUE'""" |
| 138 | if '=' not in global_string: |
| 139 | raise argparse.ArgumentTypeError(f"Global variable must be in format 'NAME=VALUE', got: {global_string}") |
| 140 | |
| 141 | name, value = global_string.split('=', 1) |
| 142 | if not name.strip(): |
| 143 | raise argparse.ArgumentTypeError(f"Global variable name cannot be empty: {global_string}") |
| 144 | |
| 145 | # Try to convert value to appropriate type |
| 146 | name = name.strip() |
| 147 | value = value.strip() |
| 148 | |
| 149 | # Try to parse as number or boolean, otherwise keep as string |
| 150 | if value.lower() in ('true', 'false'): |
| 151 | return name, value.lower() == 'true' |
| 152 | |
| 153 | try: |
| 154 | # Try integer first |
| 155 | return name, int(value) |
| 156 | except ValueError: |
| 157 | try: |
| 158 | # Try float |
| 159 | return name, float(value) |
| 160 | except ValueError: |
| 161 | # Keep as string, removing quotes if present |
| 162 | if value.startswith('"') and value.endswith('"'): |
| 163 | value = value[1:-1] |
| 164 | elif value.startswith("'") and value.endswith("'"): |
| 165 | value = value[1:-1] |
| 166 | return name, value |
| 167 | |
| 168 | |
| 169 | def main() -> int: |
nothing calls this directly
no outgoing calls
no test coverage detected