SimpleChatTC:SimpleProxy:LoadConfig ProcessArgs cleanup - initial
Now both follow a similar mechanism and do the following * exit on finding any issue, so that things are in a known state from usage perspective, without any confusion/overlook * check if the cmdlineArgCmd/configCmd being processed is a known one or not. * check value of the cmd is of the expected type * have a generic flow which can accomodate more cmds in future in a simple way
This commit is contained in:
parent
a1b33ecd1c
commit
f221a2c356
|
|
@ -32,6 +32,13 @@ gMe = {
|
||||||
'server': None
|
'server': None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gConfigType = {
|
||||||
|
'--port': 'int',
|
||||||
|
'--config': 'str',
|
||||||
|
'--debug': 'bool',
|
||||||
|
'--allowed.domains': 'list'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
class ProxyHandler(http.server.BaseHTTPRequestHandler):
|
||||||
"""
|
"""
|
||||||
|
|
@ -318,7 +325,18 @@ def load_config():
|
||||||
with open(gMe['--config']) as f:
|
with open(gMe['--config']) as f:
|
||||||
cfg = json.load(f)
|
cfg = json.load(f)
|
||||||
for k in cfg:
|
for k in cfg:
|
||||||
gMe[f"--{k}"] = cfg[k]
|
try:
|
||||||
|
cArg = f"--{k}"
|
||||||
|
aTypeCheck = gConfigType[cArg]
|
||||||
|
aValue = cfg[k]
|
||||||
|
aType = type(aValue).__name__
|
||||||
|
if aType != aTypeCheck:
|
||||||
|
print(f"ERRR:LoadConfig:{k}:expected type [{aTypeCheck}] got type [{aType}]")
|
||||||
|
exit(112)
|
||||||
|
gMe[cArg] = aValue
|
||||||
|
except KeyError:
|
||||||
|
print(f"ERRR:LoadConfig:{k}:UnknownCommand")
|
||||||
|
exit(113)
|
||||||
|
|
||||||
|
|
||||||
def process_args(args: list[str]):
|
def process_args(args: list[str]):
|
||||||
|
|
@ -327,38 +345,25 @@ def process_args(args: list[str]):
|
||||||
Helper to process command line arguments
|
Helper to process command line arguments
|
||||||
"""
|
"""
|
||||||
global gMe
|
global gMe
|
||||||
gMe['INTERNAL.ProcessArgs.Malformed'] = []
|
|
||||||
gMe['INTERNAL.ProcessArgs.Unknown'] = []
|
|
||||||
iArg = 1
|
iArg = 1
|
||||||
while iArg < len(args):
|
while iArg < len(args):
|
||||||
cArg = args[iArg]
|
cArg = args[iArg]
|
||||||
if (not cArg.startswith("--")):
|
if (not cArg.startswith("--")):
|
||||||
gMe['INTERNAL.ProcessArgs.Malformed'].append(cArg)
|
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:MalformedCommandOr???")
|
||||||
print(f"WARN:ProcessArgs:{iArg}:IgnoringMalformedCommandOr???:{cArg}")
|
exit(101)
|
||||||
|
try:
|
||||||
|
aTypeCheck = gConfigType[cArg]
|
||||||
iArg += 1
|
iArg += 1
|
||||||
continue
|
aValue = ast.literal_eval(args[iArg])
|
||||||
match cArg:
|
aType = type(aValue).__name__
|
||||||
case '--port':
|
if aType != aTypeCheck:
|
||||||
iArg += 1
|
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:expected type [{aTypeCheck}] got type [{aType}]")
|
||||||
gMe[cArg] = int(args[iArg])
|
exit(102)
|
||||||
iArg += 1
|
gMe[cArg] = aValue
|
||||||
case '--config':
|
|
||||||
iArg += 1
|
|
||||||
gMe[cArg] = args[iArg]
|
|
||||||
iArg += 1
|
|
||||||
load_config()
|
|
||||||
case '--allowed.domains':
|
|
||||||
iArg += 1
|
|
||||||
gMe[cArg] = ast.literal_eval(args[iArg])
|
|
||||||
iArg += 1
|
|
||||||
case '--debug':
|
|
||||||
iArg += 1
|
|
||||||
gMe[cArg] = ast.literal_eval(args[iArg])
|
|
||||||
iArg += 1
|
|
||||||
case _:
|
|
||||||
gMe['INTERNAL.ProcessArgs.Unknown'].append(cArg)
|
|
||||||
print(f"WARN:ProcessArgs:{iArg}:IgnoringUnknownCommand:{cArg}")
|
|
||||||
iArg += 1
|
iArg += 1
|
||||||
|
except KeyError:
|
||||||
|
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:UnknownCommand")
|
||||||
|
exit(103)
|
||||||
print(gMe)
|
print(gMe)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue