SimpleChatTC:SimpleProxy: Prg Parameters handling cleanup - next

Ensure load_config gets called on encountering --config in cmdline,
so that the user has control over whether cmdline or config file
will decide the final value of any given parameter.

Ensure that str type values in cmdline are picked up directly, without
running them through ast.literal_eval, bcas otherwise one will have to
ensure throught the cmdline arg mechanism that string quote is retained
for literal_eval

Have the """ function note/description below def line immidiately
so that it is interpreted as a function description.
This commit is contained in:
hanishkvc 2025-10-27 09:43:49 +05:30
parent f221a2c356
commit 0caa2e8101
1 changed files with 22 additions and 10 deletions

View File

@ -325,6 +325,7 @@ 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:
print(f"DBUG:LoadConfig:{k}")
try: try:
cArg = f"--{k}" cArg = f"--{k}"
aTypeCheck = gConfigType[cArg] aTypeCheck = gConfigType[cArg]
@ -340,10 +341,17 @@ def load_config():
def process_args(args: list[str]): def process_args(args: list[str]):
"""
Helper to process command line arguments.
Flow setup below such that
* location of --config in commandline will decide whether command line or config file will get
priority wrt setting program parameters.
* str type values in cmdline are picked up directly, without running them through ast.literal_eval,
bcas otherwise one will have to ensure throught the cmdline arg mechanism that string quote is
retained for literal_eval
"""
import ast import ast
"""
Helper to process command line arguments
"""
global gMe global gMe
iArg = 1 iArg = 1
while iArg < len(args): while iArg < len(args):
@ -351,16 +359,20 @@ def process_args(args: list[str]):
if (not cArg.startswith("--")): if (not cArg.startswith("--")):
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:MalformedCommandOr???") print(f"ERRR:ProcessArgs:{iArg}:{cArg}:MalformedCommandOr???")
exit(101) exit(101)
print(f"DBUG:ProcessArgs:{iArg}:{cArg}")
try: try:
aTypeCheck = gConfigType[cArg] aTypeCheck = gConfigType[cArg]
iArg += 1 aValue = args[iArg+1]
aValue = ast.literal_eval(args[iArg]) if aTypeCheck != 'str':
aType = type(aValue).__name__ aValue = ast.literal_eval(aValue)
if aType != aTypeCheck: aType = type(aValue).__name__
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:expected type [{aTypeCheck}] got type [{aType}]") if aType != aTypeCheck:
exit(102) print(f"ERRR:ProcessArgs:{iArg}:{cArg}:expected type [{aTypeCheck}] got type [{aType}]")
exit(102)
gMe[cArg] = aValue gMe[cArg] = aValue
iArg += 1 iArg += 2
if cArg == '--config':
load_config()
except KeyError: except KeyError:
print(f"ERRR:ProcessArgs:{iArg}:{cArg}:UnknownCommand") print(f"ERRR:ProcessArgs:{iArg}:{cArg}:UnknownCommand")
exit(103) exit(103)