Added libs
This commit is contained in:
51
app/common/args.py
Normal file
51
app/common/args.py
Normal file
@@ -0,0 +1,51 @@
|
||||
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
|
||||
Reference in New Issue
Block a user