MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / parse_global_variable

Function parse_global_variable

ExpandTemplateGenerator/ExpandTemplate.py:136–166  ·  view source on GitHub ↗

Parse a global variable string in the format 'NAME=VALUE

(global_string)

Source from the content-addressed store, hash-verified

134
135
136def 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
169def main() -> int:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected