[DEV] add basic argument parser to simplify argument checking and displaying help

This commit is contained in:
Edouard DUPIN 2013-05-24 21:25:03 +02:00
parent d699557711
commit a181c5bdde
2 changed files with 295 additions and 116 deletions

163
lutin.py
View File

@ -8,41 +8,31 @@ import lutinDebug as debug
import lutinEnv import lutinEnv
import lutinModule import lutinModule
import lutinMultiprocess 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 Display the help of this makefile
""" """
def usage(): def usage():
print "usage:" # generic argument displayed :
print " " + sys.argv[0] + " [options] [cible/properties] ..." mylutinArg.Display()
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"
print " all" print " all"
print " Build all (only for the current selected board) (bynary and packages)" print " Build all (only for the current selected board) (bynary and packages)"
print " clean" print " clean"
@ -58,55 +48,24 @@ def usage():
# preparse the argument to get the erbose element for debug mode # preparse the argument to get the erbose element for debug mode
def parseGenericArg(argument,active): def parseGenericArg(argument,active):
if argument == "-h" \ if argument.GetOptionName() == "help":
or argument == "--help":
#display help #display help
if active==False: if active==False:
usage() usage()
return True return True
elif argument[:3] == "-j=" \ elif argument.GetOptionName()=="jobs":
or argument[:2] == "-j" \
or argument[:7] == "--jobs=":
if active==True: if active==True:
val = "1" lutinMultiprocess.SetCoreNumber(int(argument.GetArg()))
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))
return True return True
elif argument[:3] == "-v=" \ elif argument.GetOptionName() == "verbose":
or argument[:2] == "-v" \
or argument[:10] == "--verbose=" \
or argument[:9] == "--verbose":
if active==True: if active==True:
val = "5" debug.SetLevel(int(argument.GetArg()))
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))
return True return True
elif argument == "-c" \ elif argument.GetOptionName() == "color":
or argument == "--color":
if active==True: if active==True:
debug.EnableColor() debug.EnableColor()
return True return True
elif argument == "-f" \ elif argument.GetOptionName() == "force":
or argument == "--force":
if active==True: if active==True:
lutinEnv.SetForceMode(True) lutinEnv.SetForceMode(True)
return True return True
@ -114,8 +73,7 @@ def parseGenericArg(argument,active):
# parse default unique argument: # parse default unique argument:
if __name__ == "__main__": if __name__ == "__main__":
sys.path.append(os.path.dirname(__file__) + "/corePython/" ) for argument in localArgument:
for argument in sys.argv:
parseGenericArg(argument, True) parseGenericArg(argument, True)
# now import other standard module (must be done here and not before ... # now import other standard module (must be done here and not before ...
@ -139,66 +97,39 @@ def Start():
target = None target = None
actionDone=False actionDone=False
# parse all argument # parse all argument
for argument in sys.argv[1:]: for argument in localArgument:
if True==parseGenericArg(argument, False): if True==parseGenericArg(argument, False):
None # nothing to do ... None # nothing to do ...
elif argument == "--package" \ elif argument.GetOptionName() == "package":
or argument == "-p":
generatePackage=False generatePackage=False
elif argument[:13] == "--compilator=" \ elif argument.GetOptionName() == "compilator":
or argument[:3] == "-C=": if compilator!=argument.GetArg():
tmpArg="" debug.debug("change compilator ==> " + argument.GetArg())
if argument[:3] == "-C=": compilator=argument.GetArg()
tmpArg=argument[3:] #remove previous target
else: target = None
tmpArg=argument[13:] elif argument.GetOptionName() == "target":
# 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:]
# No check input ==> this will be verify automaticly chen the target will be loaded # No check input ==> this will be verify automaticly chen the target will be loaded
if targetName!=tmpArg: if targetName!=argument.GetArg():
debug.debug("change target ==> " + tmpArg + " & reset mode : gcc&release") targetName=argument.GetArg()
targetName=tmpArg debug.debug("change target ==> " + targetName + " & reset mode : gcc&release")
#reset properties by defauult: #reset properties by defauult:
compilator="gcc" compilator="gcc"
mode="release" mode="release"
generatePackage=True generatePackage=True
#remove previous target #remove previous target
target = None target = None
elif argument[:7] == "--mode=" \ elif argument.GetOptionName() == "mode":
or argument[:3] == "-m=": if mode!=argument.GetArg():
tmpArg="" mode = argument.GetArg()
if argument[:3] == "-m=": debug.debug("change mode ==> " + mode)
tmpArg=argument[3:] #remove previous target
else: target = None
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]")
else: else:
#load the target if needed : #load the target if needed :
if target == None: if target == None:
target = lutinTarget.TargetLoad(targetName, compilator, mode, generatePackage) target = lutinTarget.TargetLoad(targetName, compilator, mode, generatePackage)
target.Build(argument) target.Build(argument.GetOptionName())
actionDone=True actionDone=True
# if no action done : we do "all" ... # if no action done : we do "all" ...
if actionDone==False: if actionDone==False:

248
lutinArg.py Normal file
View 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()