Deepak Panickal | 9b35cf5 | 2014-07-01 17:57:19 +0000 | [diff] [blame] | 1 | """ Utility module handle program args and give help |
| 2 | |
| 3 | -------------------------------------------------------------------------- |
| 4 | File: utilsArgsParse.py |
| 5 | |
| 6 | Overview: Python module to parse and validate program parameters |
| 7 | against those required by the program whether mandatory |
| 8 | or optional. |
| 9 | Also give help information on arguments required by the |
| 10 | program. |
| 11 | |
Deepak Panickal | 9b35cf5 | 2014-07-01 17:57:19 +0000 | [diff] [blame] | 12 | Gotchas: None. |
| 13 | |
| 14 | Copyright: None. |
| 15 | -------------------------------------------------------------------------- |
| 16 | |
| 17 | """ |
| 18 | |
| 19 | # Python modules: |
| 20 | import sys # Provide argument parsing |
| 21 | import getopt # Parse command line arguments |
| 22 | |
| 23 | # Third party modules: |
| 24 | |
| 25 | # In-house modules: |
| 26 | import utilsOsType # Determine the OS type this script is running on |
| 27 | |
| 28 | # Instantiations: |
| 29 | |
| 30 | # User facing text: |
| 31 | strMsgErrorInvalidParameters = "Invalid parameters entered, -h for help. \nYou entered:\n"; |
| 32 | strMsgErrorInvalidNoParams = "No parameters entered, -h for help\n"; |
| 33 | strMsgErrorNumberParameters = "Number of parameters entered incorrect, %d parameters required. You entered:\n"; |
| 34 | strMsgArgFileNotImplemented = "Sorry the --argFile is not implemented"; |
| 35 | |
| 36 | #++--------------------------------------------------------------------------- |
| 37 | # Details: Validate the arguments passed in against the mandatory and |
| 38 | # optional arguments specified. The argument format for the parameters |
| 39 | # is required to work with the module getopt function getopt(). |
| 40 | # Parameter vDictArgReq specifies which parameters are mandatory and |
| 41 | # which are optional. The format is for example: |
| 42 | # dictArgReq = { "-h": "o", # o = optional, m = mandatory |
| 43 | # "-m": "m", |
| 44 | # "--targetDir": "m", |
| 45 | # "--cfgBldDir": "o" }; |
| 46 | # Args: vArgv - (R) List of arguments and values. |
| 47 | # vstrListArgs - (R) List of small arguments. |
| 48 | # vListLongArgs - (R) List of long arguments. |
| 49 | # vDictArgReq - (R) Map of arguments required. |
| 50 | # vstrHelpInfo - (R) Formatted help text. |
| 51 | # Returns: Int - 0 success. |
| 52 | # 1 success display information, do nothing else. |
| 53 | # -1 error invalid parameters. |
| 54 | # -2 error incorrect number of mandatory parameters. |
| 55 | # Dict - Map of arguments names to argument values |
| 56 | # Str - Error message. |
| 57 | # Throws: None. |
| 58 | #-- |
| 59 | def parse( vArgv, vstrListArgs, vListLongArgs, vDictArgReq, vstrHelpInfo ): |
| 60 | dictArgs = {}; |
| 61 | dictDummy = {}; |
| 62 | strDummy = ""; |
| 63 | |
| 64 | # Validate parameters above and error on not recognised |
| 65 | try: |
Ismail Pazarbasi | cc7d7f5 | 2014-11-18 21:46:06 +0000 | [diff] [blame] | 66 | dictOptsNeeded, dictArgsLeftOver = getopt.getopt( vArgv, |
Deepak Panickal | 9b35cf5 | 2014-07-01 17:57:19 +0000 | [diff] [blame] | 67 | vstrListArgs, |
| 68 | vListLongArgs ); |
| 69 | except getopt.GetoptError: |
| 70 | strMsg = strMsgErrorInvalidParameters; |
| 71 | strMsg += str( vArgv ); |
| 72 | return (-1, dictDummy, strMsg); |
| 73 | |
| 74 | if len( dictOptsNeeded ) == 0: |
| 75 | strMsg = strMsgErrorInvalidNoParams; |
| 76 | return (-1, dictDummy, strMsg); |
| 77 | |
| 78 | # Look for help -h before anything else |
| 79 | for opt, arg in dictOptsNeeded: |
| 80 | if opt == '-h': |
| 81 | return (1, dictDummy, vstrHelpInfo ); |
| 82 | |
| 83 | # Look for the --argFile if found ignore other command line arguments |
| 84 | for opt, arg in dictOptsNeeded: |
| 85 | if opt == '--argsFile': |
| 86 | return (1, dictDummy, strMsgArgFileNotImplemented); |
| 87 | |
| 88 | # Count the number of mandatory args required (if any one found) |
| 89 | countMandatory = 0; |
Zachary Turner | 6532c6a1 | 2015-10-06 21:11:28 +0000 | [diff] [blame] | 90 | for opt, man in vDictArgReq.items(): |
Deepak Panickal | 9b35cf5 | 2014-07-01 17:57:19 +0000 | [diff] [blame] | 91 | if man == "m": |
| 92 | countMandatory = countMandatory + 1; |
| 93 | |
| 94 | # Extract short args |
| 95 | listArgs = []; |
| 96 | for arg in vstrListArgs: |
| 97 | if (arg == '-h') or (arg == ':'): |
| 98 | continue; |
| 99 | listArgs.append( arg ); |
| 100 | |
| 101 | # Append to arg dictionary the option and its value |
| 102 | bFoundNoInputValue = False; |
| 103 | countMandatoryOpts = 0; |
| 104 | for opt, val in dictOptsNeeded: |
| 105 | match = 0; |
| 106 | for arg in listArgs: |
| 107 | argg = "-" + arg; |
| 108 | if opt == argg: |
| 109 | if "m" == vDictArgReq[ opt ]: |
| 110 | countMandatoryOpts = countMandatoryOpts + 1; |
| 111 | dictArgs[ opt ] = val; |
| 112 | match = 1; |
| 113 | break; |
| 114 | if match == 0: |
| 115 | for arg in vListLongArgs: |
| 116 | argg = "--" + arg[:arg.__len__() - 1]; |
| 117 | if opt == argg: |
| 118 | if "m" == vDictArgReq[ opt ]: |
| 119 | countMandatoryOpts = countMandatoryOpts + 1; |
| 120 | dictArgs[ opt ] = val; |
| 121 | if val.__len__() == 0: |
| 122 | bFoundNoInputValue = True; |
| 123 | break; |
| 124 | |
| 125 | # Do any of the long arguments not have a value attached |
| 126 | if bFoundNoInputValue: |
| 127 | strMsg = strMsgErrorInvalidParameters; |
| 128 | strMsg += str( vArgv ); |
| 129 | return (-1, dictDummy, strMsg); |
| 130 | |
| 131 | # Debug only |
| 132 | #print countMandatoryOpts |
| 133 | #print countMandatory |
| 134 | |
| 135 | # Do we have the exact number of mandatory arguments |
| 136 | if (countMandatoryOpts > 0) and (countMandatory != countMandatoryOpts): |
| 137 | strMsg = strMsgErrorNumberParameters % countMandatory; |
| 138 | strMsg += str( vArgv ); |
| 139 | return (-2, dictDummy, strMsg); |
| 140 | |
| 141 | return (0, dictArgs, strDummy); |