51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
import sys
|
|
from typing import Dict, List, Any
|
|
|
|
def read_kargs(argv: List[str] = None) -> Dict[str, Any]:
|
|
"""
|
|
Parse command-line arguments in the format key=value.
|
|
|
|
Example:
|
|
python start.py updateApp=True port=8080
|
|
|
|
Returns:
|
|
dict: {
|
|
"updateApp": True,
|
|
"port": "8080"
|
|
}
|
|
"""
|
|
if argv is None:
|
|
argv = sys.argv
|
|
|
|
kargs: Dict[str, Any] = {}
|
|
for param in argv:
|
|
if "=" in param:
|
|
key, value = param.split("=", 1)
|
|
|
|
# Normalize booleans
|
|
if value.lower() == "true":
|
|
value = True
|
|
elif value.lower() == "false":
|
|
value = False
|
|
|
|
kargs[key] = value
|
|
|
|
return kargs
|
|
|
|
|
|
def kargs_to_array(kargs: Dict[str, Any] = None) -> List[str]:
|
|
"""
|
|
Convert a dictionary of arguments back to an array of key=value.
|
|
|
|
Example:
|
|
{"updateApp": True, "port": 8080}
|
|
Returns:
|
|
["updateApp=True", "port=8080"]
|
|
"""
|
|
if not kargs:
|
|
kargs = read_kargs()
|
|
|
|
array: List[str] = []
|
|
for key, value in kargs.items():
|
|
array.append(f"{key}={value}")
|
|
return array |