2014-07-02 01:57:19 +08:00
|
|
|
""" Utility module handle program args and give help
|
|
|
|
|
2015-11-06 07:57:33 +08:00
|
|
|
--------------------------------------------------------------------------
|
|
|
|
File: utilsArgsParse.py
|
|
|
|
|
|
|
|
Overview: Python module to parse and validate program parameters
|
|
|
|
against those required by the program whether mandatory
|
|
|
|
or optional.
|
|
|
|
Also give help information on arguments required by the
|
|
|
|
program.
|
|
|
|
|
|
|
|
Gotchas: None.
|
|
|
|
|
|
|
|
Copyright: None.
|
|
|
|
--------------------------------------------------------------------------
|
|
|
|
|
2014-07-02 01:57:19 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Python modules:
|
2016-09-07 04:57:50 +08:00
|
|
|
import getopt # Parse command line arguments
|
2014-07-02 01:57:19 +08:00
|
|
|
|
|
|
|
# Third party modules:
|
|
|
|
|
|
|
|
# In-house modules:
|
|
|
|
|
|
|
|
# Instantiations:
|
|
|
|
|
|
|
|
# User facing text:
|
2015-11-06 07:57:33 +08:00
|
|
|
strMsgErrorInvalidParameters = "Invalid parameters entered, -h for help. \nYou entered:\n"
|
|
|
|
strMsgErrorInvalidNoParams = "No parameters entered, -h for help\n"
|
|
|
|
strMsgErrorNumberParameters = "Number of parameters entered incorrect, %d parameters required. You entered:\n"
|
|
|
|
strMsgArgFileNotImplemented = "Sorry the --argFile is not implemented"
|
2014-07-02 01:57:19 +08:00
|
|
|
|
|
|
|
#++---------------------------------------------------------------------------
|
2015-11-06 07:57:33 +08:00
|
|
|
# Details: Validate the arguments passed in against the mandatory and
|
|
|
|
# optional arguments specified. The argument format for the parameters
|
|
|
|
# is required to work with the module getopt function getopt().
|
|
|
|
# Parameter vDictArgReq specifies which parameters are mandatory and
|
|
|
|
# which are optional. The format is for example:
|
|
|
|
# dictArgReq = {"-h": "o", # o = optional, m = mandatory
|
|
|
|
# "-m": "m",
|
|
|
|
# "--targetDir": "m",
|
|
|
|
# "--cfgBldDir": "o" }
|
|
|
|
# Args: vArgv - (R) List of arguments and values.
|
|
|
|
# vstrListArgs - (R) List of small arguments.
|
|
|
|
# vListLongArgs - (R) List of long arguments.
|
|
|
|
# vDictArgReq - (R) Map of arguments required.
|
|
|
|
# vstrHelpInfo - (R) Formatted help text.
|
|
|
|
# Returns: Int - 0 success.
|
|
|
|
# 1 success display information, do nothing else.
|
|
|
|
# -1 error invalid parameters.
|
|
|
|
# -2 error incorrect number of mandatory parameters.
|
|
|
|
# Dict - Map of arguments names to argument values
|
|
|
|
# Str - Error message.
|
|
|
|
# Throws: None.
|
2014-07-02 01:57:19 +08:00
|
|
|
#--
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2015-11-06 07:57:33 +08:00
|
|
|
def parse(vArgv, vstrListArgs, vListLongArgs, vDictArgReq, vstrHelpInfo):
|
|
|
|
dictArgs = {}
|
|
|
|
dictDummy = {}
|
|
|
|
strDummy = ""
|
|
|
|
|
|
|
|
# Validate parameters above and error on not recognised
|
|
|
|
try:
|
|
|
|
dictOptsNeeded, dictArgsLeftOver = getopt.getopt(vArgv,
|
|
|
|
vstrListArgs,
|
|
|
|
vListLongArgs)
|
|
|
|
except getopt.GetoptError:
|
|
|
|
strMsg = strMsgErrorInvalidParameters
|
|
|
|
strMsg += str(vArgv)
|
|
|
|
return (-1, dictDummy, strMsg)
|
|
|
|
|
|
|
|
if len(dictOptsNeeded) == 0:
|
|
|
|
strMsg = strMsgErrorInvalidNoParams
|
|
|
|
return (-1, dictDummy, strMsg)
|
|
|
|
|
|
|
|
# Look for help -h before anything else
|
|
|
|
for opt, arg in dictOptsNeeded:
|
|
|
|
if opt == '-h':
|
|
|
|
return (1, dictDummy, vstrHelpInfo)
|
|
|
|
|
|
|
|
# Look for the --argFile if found ignore other command line arguments
|
|
|
|
for opt, arg in dictOptsNeeded:
|
|
|
|
if opt == '--argsFile':
|
|
|
|
return (1, dictDummy, strMsgArgFileNotImplemented)
|
|
|
|
|
|
|
|
# Count the number of mandatory args required (if any one found)
|
|
|
|
countMandatory = 0
|
|
|
|
for opt, man in list(vDictArgReq.items()):
|
|
|
|
if man == "m":
|
|
|
|
countMandatory = countMandatory + 1
|
|
|
|
|
|
|
|
# Extract short args
|
|
|
|
listArgs = []
|
|
|
|
for arg in vstrListArgs:
|
|
|
|
if (arg == '-h') or (arg == ':'):
|
|
|
|
continue
|
|
|
|
listArgs.append(arg)
|
|
|
|
|
|
|
|
# Append to arg dictionary the option and its value
|
|
|
|
bFoundNoInputValue = False
|
|
|
|
countMandatoryOpts = 0
|
|
|
|
for opt, val in dictOptsNeeded:
|
|
|
|
match = 0
|
|
|
|
for arg in listArgs:
|
|
|
|
argg = "-" + arg
|
|
|
|
if opt == argg:
|
|
|
|
if "m" == vDictArgReq[opt]:
|
|
|
|
countMandatoryOpts = countMandatoryOpts + 1
|
|
|
|
dictArgs[opt] = val
|
|
|
|
match = 1
|
|
|
|
break
|
|
|
|
if match == 0:
|
|
|
|
for arg in vListLongArgs:
|
2017-02-04 08:20:24 +08:00
|
|
|
argg = "--" + arg.rstrip('=')
|
2015-11-06 07:57:33 +08:00
|
|
|
if opt == argg:
|
|
|
|
if "m" == vDictArgReq[opt]:
|
|
|
|
countMandatoryOpts = countMandatoryOpts + 1
|
|
|
|
dictArgs[opt] = val
|
2017-02-04 08:20:24 +08:00
|
|
|
if arg[-1:] == '=' and val.__len__() == 0:
|
2015-11-06 07:57:33 +08:00
|
|
|
bFoundNoInputValue = True
|
|
|
|
break
|
|
|
|
|
|
|
|
# Do any of the long arguments not have a value attached
|
|
|
|
if bFoundNoInputValue:
|
|
|
|
strMsg = strMsgErrorInvalidParameters
|
|
|
|
strMsg += str(vArgv)
|
|
|
|
return (-1, dictDummy, strMsg)
|
|
|
|
|
|
|
|
# Debug only
|
2016-09-07 04:57:50 +08:00
|
|
|
# print countMandatoryOpts
|
|
|
|
# print countMandatory
|
2015-11-06 07:57:33 +08:00
|
|
|
|
|
|
|
# Do we have the exact number of mandatory arguments
|
|
|
|
if (countMandatoryOpts > 0) and (countMandatory != countMandatoryOpts):
|
|
|
|
strMsg = strMsgErrorNumberParameters % countMandatory
|
|
|
|
strMsg += str(vArgv)
|
|
|
|
return (-2, dictDummy, strMsg)
|
|
|
|
|
|
|
|
return (0, dictArgs, strDummy)
|