[DEV] add basic argument parser to simplify argument checking and displaying help
This commit is contained in:
parent
d699557711
commit
a181c5bdde
163
lutin.py
163
lutin.py
@ -8,41 +8,31 @@ import lutinDebug as debug
|
||||
import lutinEnv
|
||||
import lutinModule
|
||||
import lutinMultiprocess
|
||||
import lutinArg
|
||||
|
||||
mylutinArg = lutinArg.lutinArg()
|
||||
mylutinArg.Add(lutinArg.argDefine("h", "help", desc="display this help"))
|
||||
mylutinArg.AddSection("option", "Can be set one time in all case")
|
||||
mylutinArg.Add(lutinArg.argDefine("v", "verbose", list=[["0","None"],["1","error"],["2","warning"],["3","info"],["4","debug"],["5","verbose"]], desc="Display makefile debug level (verbose) default =2"))
|
||||
mylutinArg.Add(lutinArg.argDefine("c", "color", desc="Display makefile output in color"))
|
||||
mylutinArg.Add(lutinArg.argDefine("f", "force", desc="Force the rebuild without checking the dependency"))
|
||||
mylutinArg.Add(lutinArg.argDefine("j", "jobs", haveParam=True, desc="Specifies the number of jobs (commands) to run simultaneously"))
|
||||
|
||||
mylutinArg.AddSection("properties", "keep in the sequency of the cible")
|
||||
mylutinArg.Add(lutinArg.argDefine("t", "target", list=[["Android",""],["Linux",""],["MacOs",""],["Windows",""]], desc="Select a target (by default the platform is the computer that compile this"))
|
||||
mylutinArg.Add(lutinArg.argDefine("C", "compilator", list=[["clang",""],["gcc",""]], desc="Compile with clang or Gcc mode (by default gcc will be used)"))
|
||||
mylutinArg.Add(lutinArg.argDefine("m", "mode", list=[["debug",""],["release",""]], desc="Compile in release or debug mode (default release)"))
|
||||
mylutinArg.Add(lutinArg.argDefine("p", "package", desc="Disable the package generation (usefull when just compile for test on linux ...)"))
|
||||
|
||||
mylutinArg.AddSection("cible", "generate in order set")
|
||||
localArgument = mylutinArg.Parse()
|
||||
|
||||
"""
|
||||
Display the help of this makefile
|
||||
"""
|
||||
def usage():
|
||||
print "usage:"
|
||||
print " " + sys.argv[0] + " [options] [cible/properties] ..."
|
||||
print " [help] display this help"
|
||||
print " [option] : keep the last set"
|
||||
print " -h / --help"
|
||||
print " Display this help and break"
|
||||
print " -v / -v? / --verbose=?"
|
||||
print " Display makefile debug level (verbose) default =2"
|
||||
print " 0 : None"
|
||||
print " 1 : error"
|
||||
print " 2 : warning"
|
||||
print " 3 : info"
|
||||
print " 4 : debug"
|
||||
print " 5 : verbose"
|
||||
print " -c / --color"
|
||||
print " Display makefile output in color"
|
||||
print " -f / --force"
|
||||
print " Force the rebuild without checking the dependency"
|
||||
print " -j= / --jobs"
|
||||
print " Specifies the number of jobs (commands) to run simultaneously."
|
||||
print " [properties] : keep in the sequency of the cible"
|
||||
print " -t=... / --target=..."
|
||||
print " (Android/Linux/MacOs/Windows) Select a target (by default the platform is the computer that compile this"
|
||||
print " -C= / --compilator="
|
||||
print " (clang/gcc) Compile with clang or Gcc mode (by default gcc will be used)"
|
||||
print " -m=... / --mode=..."
|
||||
print " (debug/release) Compile in release or debug mode (default release)"
|
||||
print " -p / --package"
|
||||
print " disable the package generation (usefull when just compile for test on linux ...)"
|
||||
print " [cible] : generate in order set"
|
||||
# generic argument displayed :
|
||||
mylutinArg.Display()
|
||||
print " all"
|
||||
print " Build all (only for the current selected board) (bynary and packages)"
|
||||
print " clean"
|
||||
@ -58,55 +48,24 @@ def usage():
|
||||
|
||||
# preparse the argument to get the erbose element for debug mode
|
||||
def parseGenericArg(argument,active):
|
||||
if argument == "-h" \
|
||||
or argument == "--help":
|
||||
if argument.GetOptionName() == "help":
|
||||
#display help
|
||||
if active==False:
|
||||
usage()
|
||||
return True
|
||||
elif argument[:3] == "-j=" \
|
||||
or argument[:2] == "-j" \
|
||||
or argument[:7] == "--jobs=":
|
||||
elif argument.GetOptionName()=="jobs":
|
||||
if active==True:
|
||||
val = "1"
|
||||
if argument[:3] == "-j=":
|
||||
val = argument[3:]
|
||||
elif argument[:2] == "-j":
|
||||
if len(argument) == 2:
|
||||
val = "1"
|
||||
else:
|
||||
val = argument[2:]
|
||||
else:
|
||||
val = argument[7:]
|
||||
lutinMultiprocess.SetCoreNumber(int(val))
|
||||
lutinMultiprocess.SetCoreNumber(int(argument.GetArg()))
|
||||
return True
|
||||
elif argument[:3] == "-v=" \
|
||||
or argument[:2] == "-v" \
|
||||
or argument[:10] == "--verbose=" \
|
||||
or argument[:9] == "--verbose":
|
||||
elif argument.GetOptionName() == "verbose":
|
||||
if active==True:
|
||||
val = "5"
|
||||
if argument[:3] == "-v=":
|
||||
val = argument[3:]
|
||||
elif argument[:2] == "-v":
|
||||
if len(argument) == 2:
|
||||
val = "5"
|
||||
else:
|
||||
val = argument[2:]
|
||||
else:
|
||||
if len(argument) == 9:
|
||||
val = "5"
|
||||
else:
|
||||
val = argument[10:]
|
||||
debug.SetLevel(int(val))
|
||||
debug.SetLevel(int(argument.GetArg()))
|
||||
return True
|
||||
elif argument == "-c" \
|
||||
or argument == "--color":
|
||||
elif argument.GetOptionName() == "color":
|
||||
if active==True:
|
||||
debug.EnableColor()
|
||||
return True
|
||||
elif argument == "-f" \
|
||||
or argument == "--force":
|
||||
elif argument.GetOptionName() == "force":
|
||||
if active==True:
|
||||
lutinEnv.SetForceMode(True)
|
||||
return True
|
||||
@ -114,8 +73,7 @@ def parseGenericArg(argument,active):
|
||||
|
||||
# parse default unique argument:
|
||||
if __name__ == "__main__":
|
||||
sys.path.append(os.path.dirname(__file__) + "/corePython/" )
|
||||
for argument in sys.argv:
|
||||
for argument in localArgument:
|
||||
parseGenericArg(argument, True)
|
||||
|
||||
# now import other standard module (must be done here and not before ...
|
||||
@ -139,66 +97,39 @@ def Start():
|
||||
target = None
|
||||
actionDone=False
|
||||
# parse all argument
|
||||
for argument in sys.argv[1:]:
|
||||
for argument in localArgument:
|
||||
if True==parseGenericArg(argument, False):
|
||||
None # nothing to do ...
|
||||
elif argument == "--package" \
|
||||
or argument == "-p":
|
||||
elif argument.GetOptionName() == "package":
|
||||
generatePackage=False
|
||||
elif argument[:13] == "--compilator=" \
|
||||
or argument[:3] == "-C=":
|
||||
tmpArg=""
|
||||
if argument[:3] == "-C=":
|
||||
tmpArg=argument[3:]
|
||||
else:
|
||||
tmpArg=argument[13:]
|
||||
# check input ...
|
||||
if tmpArg=="gcc" \
|
||||
or tmpArg=="clang":
|
||||
if compilator!=tmpArg:
|
||||
debug.debug("change compilator ==> " + tmpArg)
|
||||
compilator=tmpArg
|
||||
#remove previous target
|
||||
target = None
|
||||
else:
|
||||
debug.error("Set --compilator/-C: '" + tmpArg + "' but only availlable : [gcc/clang]")
|
||||
elif argument[:9] == "--target=" \
|
||||
or argument[:3] == "-t=":
|
||||
tmpArg=""
|
||||
if argument[:3] == "-t=":
|
||||
tmpArg=argument[3:]
|
||||
else:
|
||||
tmpArg=argument[9:]
|
||||
elif argument.GetOptionName() == "compilator":
|
||||
if compilator!=argument.GetArg():
|
||||
debug.debug("change compilator ==> " + argument.GetArg())
|
||||
compilator=argument.GetArg()
|
||||
#remove previous target
|
||||
target = None
|
||||
elif argument.GetOptionName() == "target":
|
||||
# No check input ==> this will be verify automaticly chen the target will be loaded
|
||||
if targetName!=tmpArg:
|
||||
debug.debug("change target ==> " + tmpArg + " & reset mode : gcc&release")
|
||||
targetName=tmpArg
|
||||
if targetName!=argument.GetArg():
|
||||
targetName=argument.GetArg()
|
||||
debug.debug("change target ==> " + targetName + " & reset mode : gcc&release")
|
||||
#reset properties by defauult:
|
||||
compilator="gcc"
|
||||
mode="release"
|
||||
generatePackage=True
|
||||
#remove previous target
|
||||
target = None
|
||||
elif argument[:7] == "--mode=" \
|
||||
or argument[:3] == "-m=":
|
||||
tmpArg=""
|
||||
if argument[:3] == "-m=":
|
||||
tmpArg=argument[3:]
|
||||
else:
|
||||
tmpArg=argument[11:]
|
||||
if "debug"==tmpArg or "release"==tmpArg:
|
||||
if mode!=tmpArg:
|
||||
debug.debug("change mode ==> " + tmpArg)
|
||||
mode = tmpArg
|
||||
#remove previous target
|
||||
target = None
|
||||
else:
|
||||
debug.error("Set --mode/-m: '" + tmpArg + "' but only availlable : [debug/release]")
|
||||
elif argument.GetOptionName() == "mode":
|
||||
if mode!=argument.GetArg():
|
||||
mode = argument.GetArg()
|
||||
debug.debug("change mode ==> " + mode)
|
||||
#remove previous target
|
||||
target = None
|
||||
else:
|
||||
#load the target if needed :
|
||||
if target == None:
|
||||
target = lutinTarget.TargetLoad(targetName, compilator, mode, generatePackage)
|
||||
target.Build(argument)
|
||||
target.Build(argument.GetOptionName())
|
||||
actionDone=True
|
||||
# if no action done : we do "all" ...
|
||||
if actionDone==False:
|
||||
|
248
lutinArg.py
Normal file
248
lutinArg.py
Normal file
@ -0,0 +1,248 @@
|
||||
#!/usr/bin/python
|
||||
import sys
|
||||
import lutinDebug as debug
|
||||
|
||||
class argElement:
|
||||
def __init__(self, option, value=""):
|
||||
self.m_option = option;
|
||||
self.m_arg = value;
|
||||
|
||||
def GetOptionName(self):
|
||||
return self.m_option
|
||||
|
||||
def GetArg(self):
|
||||
return self.m_arg
|
||||
|
||||
def Display(self):
|
||||
if len(self.m_arg)==0:
|
||||
debug.info("element : " + self.m_option)
|
||||
else:
|
||||
debug.info("element : " + self.m_option + ":" + self.m_arg)
|
||||
|
||||
|
||||
class argDefine:
|
||||
def __init__(self,
|
||||
smallOption="", # like v for -v
|
||||
bigOption="", # like verbose for --verbose
|
||||
list=[], # ["val", "description"]
|
||||
desc="",
|
||||
haveParam=False):
|
||||
self.m_optionSmall = smallOption;
|
||||
self.m_optionBig = bigOption;
|
||||
self.m_list = list;
|
||||
if len(self.m_list)!=0:
|
||||
self.m_haveParam = True
|
||||
else:
|
||||
if True==haveParam:
|
||||
self.m_haveParam = True
|
||||
else:
|
||||
self.m_haveParam = False
|
||||
self.m_description = desc;
|
||||
|
||||
def GetOptionSmall(self):
|
||||
return self.m_optionSmall
|
||||
|
||||
def GetOptionBig(self):
|
||||
return self.m_optionBig
|
||||
|
||||
def NeedParameters(self):
|
||||
return self.m_haveParam
|
||||
|
||||
def GetPorperties(self):
|
||||
return ""
|
||||
|
||||
def CheckAvaillable(self, argument):
|
||||
if len(self.m_list)==0:
|
||||
return True
|
||||
for element,desc in self.m_list:
|
||||
if element == argument:
|
||||
return True
|
||||
return False
|
||||
|
||||
def Display(self):
|
||||
if self.m_optionSmall != "" and self.m_optionBig != "":
|
||||
print(" -" + self.m_optionSmall + " / --" + self.m_optionBig)
|
||||
elif self.m_optionSmall != "":
|
||||
print(" -" + self.m_optionSmall)
|
||||
elif self.m_optionSmall != "":
|
||||
print(" --" + self.m_optionBig)
|
||||
else:
|
||||
print(" ???? ==> internal error ...")
|
||||
if self.m_description != "":
|
||||
print(" " + self.m_description)
|
||||
if len(self.m_list)!=0:
|
||||
hasDescriptiveElement=False
|
||||
for val,desc in self.m_list:
|
||||
if desc!="":
|
||||
hasDescriptiveElement=True
|
||||
break;
|
||||
if hasDescriptiveElement==True:
|
||||
for val,desc in self.m_list:
|
||||
print(" " + val + " : " + desc)
|
||||
else:
|
||||
tmpElementPrint = ""
|
||||
for val,desc in self.m_list:
|
||||
if len(tmpElementPrint)!=0:
|
||||
tmpElementPrint += " / "
|
||||
tmpElementPrint += val
|
||||
print(" { " + tmpElementPrint + " }")
|
||||
|
||||
def Parse(self, argList, currentID):
|
||||
return currentID;
|
||||
|
||||
|
||||
class argSection:
|
||||
def __init__(self,
|
||||
sectionName="",
|
||||
desc=""):
|
||||
self.m_section = sectionName;
|
||||
self.m_description = desc;
|
||||
|
||||
def GetOptionSmall(self):
|
||||
return ""
|
||||
|
||||
def GetOptionBig(self):
|
||||
return ""
|
||||
|
||||
def GetPorperties(self):
|
||||
return " [" + self.m_section + "]"
|
||||
|
||||
def Display(self):
|
||||
print(" [" + self.m_section + "] : " + self.m_description)
|
||||
|
||||
def Parse(self, argList, currentID):
|
||||
return currentID;
|
||||
|
||||
|
||||
class lutinArg:
|
||||
def __init__(self):
|
||||
self.m_listProperties = []
|
||||
|
||||
def Add(self, argument):
|
||||
self.m_listProperties.append(argument) #argDefine(smallOption, bigOption, haveParameter, parameterList, description));
|
||||
|
||||
def AddSection(self, sectionName, sectionDesc):
|
||||
self.m_listProperties.append(argSection(sectionName, sectionDesc))
|
||||
|
||||
def Parse(self):
|
||||
listArgument = [] # composed of list element
|
||||
NotParseNextElement=False
|
||||
for iii in range(1, len(sys.argv)):
|
||||
# special case of parameter in some elements
|
||||
if NotParseNextElement==True:
|
||||
NotParseNextElement = False
|
||||
continue
|
||||
debug.verbose("parse [" + str(iii) + "]=" + sys.argv[iii])
|
||||
argument = sys.argv[iii]
|
||||
optionList = argument.split("=")
|
||||
debug.verbose(str(optionList))
|
||||
if type(optionList) == type(str()):
|
||||
option = optionList
|
||||
else:
|
||||
option = optionList[0]
|
||||
optionParam = argument[len(option)+1:]
|
||||
debug.verbose(option)
|
||||
argumentFound=False;
|
||||
if option[:2]=="--":
|
||||
# big argument
|
||||
for prop in self.m_listProperties:
|
||||
if prop.GetOptionBig()=="":
|
||||
continue
|
||||
if prop.GetOptionBig() == option[2:2+len(prop.GetOptionBig())]:
|
||||
# find it
|
||||
debug.verbose("find argument 2 : " + option[2:2+len(prop.GetOptionBig())])
|
||||
if prop.NeedParameters()==True:
|
||||
internalSub = option[2+len(prop.GetOptionBig()):]
|
||||
if len(internalSub)!=0:
|
||||
if len(optionParam)!=0:
|
||||
# wrong argument ...
|
||||
debug.warning("maybe wrong argument for : '" + prop.GetOptionBig() + "' cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
continue
|
||||
optionParam = internalSub
|
||||
if len(optionParam)==0:
|
||||
#Get the next parameters
|
||||
if len(sys.argv) > iii+1:
|
||||
optionParam = sys.argv[iii+1]
|
||||
NotParseNextElement=True
|
||||
else :
|
||||
# missing arguments
|
||||
debug.warning("parsing argument error : '" + prop.GetOptionBig() + "' Missing : subParameters ... cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
exit(-1)
|
||||
if prop.CheckAvaillable(optionParam)==False:
|
||||
debug.warning("argument error : '" + prop.GetOptionBig() + "' SubParameters not availlable ... cmdLine='" + argument + "' option='" + optionParam + "'")
|
||||
prop.Display()
|
||||
exit(-1)
|
||||
listArgument.append(argElement(prop.GetOptionBig(),optionParam))
|
||||
argumentFound = True
|
||||
else:
|
||||
if len(optionParam)!=0:
|
||||
debug.warning("parsing argument error : '" + prop.GetOptionBig() + "' need no subParameters : '" + optionParam + "' cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
listArgument.append(argElement(prop.GetOptionBig()))
|
||||
argumentFound = True
|
||||
break;
|
||||
if False==argumentFound:
|
||||
debug.error("UNKNOW argument : '" + argument + "'")
|
||||
elif option[:1]=="-":
|
||||
# small argument
|
||||
for prop in self.m_listProperties:
|
||||
if prop.GetOptionSmall()=="":
|
||||
continue
|
||||
if prop.GetOptionSmall() == option[1:1+len(prop.GetOptionSmall())]:
|
||||
# find it
|
||||
debug.verbose("find argument 1 : " + option[1:1+len(prop.GetOptionSmall())])
|
||||
if prop.NeedParameters()==True:
|
||||
internalSub = option[1+len(prop.GetOptionSmall()):]
|
||||
if len(internalSub)!=0:
|
||||
if len(optionParam)!=0:
|
||||
# wrong argument ...
|
||||
debug.warning("maybe wrong argument for : '" + prop.GetOptionBig() + "' cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
continue
|
||||
optionParam = internalSub
|
||||
if len(optionParam)==0:
|
||||
#Get the next parameters
|
||||
if len(sys.argv) > iii+1:
|
||||
optionParam = sys.argv[iii+1]
|
||||
NotParseNextElement=True
|
||||
else :
|
||||
# missing arguments
|
||||
debug.warning("parsing argument error : '" + prop.GetOptionBig() + "' Missing : subParameters cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
exit(-1)
|
||||
if prop.CheckAvaillable(optionParam)==False:
|
||||
debug.warning("argument error : '" + prop.GetOptionBig() + "' SubParameters not availlable ... cmdLine='" + argument + "' option='" + optionParam + "'")
|
||||
prop.Display()
|
||||
exit(-1)
|
||||
listArgument.append(argElement(prop.GetOptionBig(),optionParam))
|
||||
argumentFound = True
|
||||
else:
|
||||
if len(optionParam)!=0:
|
||||
debug.warning("parsing argument error : '" + prop.GetOptionBig() + "' need no subParameters : '" + optionParam + "' cmdLine='" + argument + "'")
|
||||
prop.Display()
|
||||
listArgument.append(argElement(prop.GetOptionBig()))
|
||||
argumentFound = True
|
||||
break;
|
||||
|
||||
if argumentFound==False:
|
||||
#unknow element ... ==> just add in the list ...
|
||||
debug.verbose("unknow argument : " + argument)
|
||||
listArgument.append(argElement(argument, ""))
|
||||
|
||||
#for argument in listArgument:
|
||||
# argument.Display()
|
||||
#exit(0)
|
||||
return listArgument;
|
||||
|
||||
|
||||
|
||||
def Display(self):
|
||||
print "usage:"
|
||||
listOfPropertiesArg = "";
|
||||
for element in self.m_listProperties :
|
||||
listOfPropertiesArg += element.GetPorperties()
|
||||
print " " + sys.argv[0] + listOfPropertiesArg + " ..."
|
||||
for element in self.m_listProperties :
|
||||
element.Display()
|
Loading…
x
Reference in New Issue
Block a user