| 1 | """Handle command line arguments. |
| 2 | |
| 3 | This module contains functions to parse and access the arguments given |
| 4 | to the program on the command line. |
| 5 | """ |
| 6 | |
| 7 | import types |
| 8 | import string |
| 9 | import sys |
| 10 | |
| 11 | # Symbolic constants for the indexes into an argument specifier tuple. |
| 12 | NAME = 0 |
| 13 | MANDATORY = 1 |
| 14 | TYPE = 2 |
| 15 | HELP = 3 |
| 16 | DEFAULT = 4 |
| 17 | SPEC_LENGTH = 5 |
| 18 | |
| 19 | Bool = [] |
| 20 | |
| 21 | helpSpec = ( |
| 22 | ('help', 0, Bool, 'print help and exit'), |
| 23 | ) |
| 24 | |
| 25 | def parseArgs(title, argv, argSpecs, filesOK): |
| 26 | """Parse and check command line arguments. |
| 27 | |
| 28 | Scan the command line arguments in *argv* according to the argument |
| 29 | specifier *argSpecs*. Return **None** if there are no errors in |
| 30 | the arguments, otherwise return an error string describing the error. |
| 31 | |
| 32 | This function must be called to initialise this module. |
| 33 | |
| 34 | title -- The name of the program. This is used when returning |
| 35 | error messages or help text. |
| 36 | |
| 37 | argv -- A sequence containing the arguments given to the program. |
| 38 | Normally **sys.argv**. |
| 39 | |
| 40 | argSpecs -- A sequence of argument specifiers. Each specifier describes |
| 41 | a valid command line argument and consists of 4 or 5 items: |
| 42 | |
| 43 | - The argument name (without a leading minus sign **-**). |
| 44 | |
| 45 | - A boolean value, true if the argument is mandatory. |
| 46 | |
| 47 | - This should be **Args.Bool** if the argument has no option. |
| 48 | Otherwise it should be a string describing the option |
| 49 | required for this argument. This is used when printing help. |
| 50 | |
| 51 | - A short string describing the argument. |
| 52 | |
| 53 | - The default value of the argument. This should only be used |
| 54 | for non-mandatory arguments expecting an option. |
| 55 | |
| 56 | For example: |
| 57 | ( |
| 58 | ('foreground', 0, 'colour', 'colour of text', 'black'), |
| 59 | ('geometry', 0, 'spec', 'geometry of initial window'), |
| 60 | ('server', 1, 'ompserver', 'ompserver to connect to'), |
| 61 | ('silent', 0, Args.Bool, 'do not sound bell'), |
| 62 | ) |
| 63 | """ |
| 64 | |
| 65 | global programName |
| 66 | global _fileList |
| 67 | |
| 68 | errMsg = title + ' command line error: ' |
| 69 | programName = argv[0]; |
| 70 | |
| 71 | argSpecs = helpSpec + argSpecs |
| 72 | argSpecDic = {} |
| 73 | for spec in argSpecs: |
| 74 | arg = spec[NAME] |
| 75 | argSpecDic[arg] = spec |
| 76 | if len(spec) >= SPEC_LENGTH: |
| 77 | set(arg, spec[DEFAULT]) |
| 78 | elif spec[TYPE] is Bool: |
| 79 | set(arg, 0) |
| 80 | else: |
| 81 | set(arg, None) |
| 82 | |
| 83 | knownKeys = argSpecDic.keys() |
| 84 | |
| 85 | i = 1 |
| 86 | _fileList = [] |
| 87 | argc = len(argv) |
| 88 | while i < argc: |
| 89 | arg = argv[i] |
| 90 | key = arg[1:] |
| 91 | if key in knownKeys: |
| 92 | spec = argSpecDic[key] |
| 93 | if spec[TYPE] is Bool: |
| 94 | set(key, 1) |
| 95 | else: |
| 96 | i = i + 1 |
| 97 | if i >= argc: |
| 98 | return errMsg + 'missing argument to \'' + arg + '\' option.' |
| 99 | value = argv[i] |
| 100 | if len(spec) >= SPEC_LENGTH: |
| 101 | try: |
| 102 | if type(spec[DEFAULT]) == types.IntType: |
| 103 | typeStr = 'integer' |
| 104 | value = string.atoi(value) |
| 105 | elif type(spec[DEFAULT]) == types.FloatType: |
| 106 | typeStr = 'float' |
| 107 | value = string.atof(value) |
| 108 | except: |
| 109 | sys.exc_traceback = None # Clean up object references |
| 110 | return errMsg + 'cannot convert string \'' + value + \ |
| 111 | '\' to ' + typeStr + ' for option \'-' + key + '\'.' |
| 112 | set(key, value) |
| 113 | else: |
| 114 | _fileList.append(arg) |
| 115 | i = i + 1 |
| 116 | |
| 117 | if get('help'): |
| 118 | return _helpString(title, argSpecs) |
| 119 | |
| 120 | if not filesOK and len(_fileList) > 0: |
| 121 | if len(_fileList) == 1: |
| 122 | return errMsg + 'unknown option \'' + str(_fileList[0]) + '\'.' |
| 123 | else: |
| 124 | return errMsg + 'unknown options ' + str(_fileList) + '.' |
| 125 | |
| 126 | |
| 127 | _missing = [] |
| 128 | for spec in argSpecs: |
| 129 | if spec[MANDATORY] and get(spec[NAME]) is None: |
| 130 | _missing.append(spec[NAME]) |
| 131 | if len(_missing) == 1: |
| 132 | return errMsg + 'required argument \'-' + \ |
| 133 | str(_missing[0]) + '\' is missing.' |
| 134 | elif len(_missing) > 1: |
| 135 | return errMsg + 'required arguments ' + \ |
| 136 | str(map(lambda s: '-' + s, _missing)) + ' are missing.' |
| 137 | |
| 138 | return None |
| 139 | |
| 140 | def fileList(): |
| 141 | return _fileList |
| 142 | |
| 143 | def _helpString(title, argSpecs): |
| 144 | max = 0 |
| 145 | for spec in argSpecs: |
| 146 | if spec[TYPE] is Bool: |
| 147 | width = len(spec[NAME]) + 1 |
| 148 | else: |
| 149 | width = len(spec[NAME]) + 4 + len(spec[TYPE]) |
| 150 | if width > max: |
| 151 | max = width |
| 152 | |
| 153 | rtn = title + ' command line arguments:' |
| 154 | format = '\n %-' + str(max) + 's %s' |
| 155 | for mandatory in (1, 0): |
| 156 | needHeader = 1 |
| 157 | for spec in argSpecs: |
| 158 | if mandatory and spec[MANDATORY] or not mandatory and not spec[MANDATORY]: |
| 159 | if needHeader: |
| 160 | if mandatory: |
| 161 | rtn = rtn + '\n Mandatory arguments:' |
| 162 | else: |
| 163 | rtn = rtn + '\n Optional arguments (defaults in parentheses):' |
| 164 | needHeader = 0 |
| 165 | if spec[TYPE] is Bool: |
| 166 | arg = '-%s' % spec[NAME] |
| 167 | else: |
| 168 | arg = '-%s <%s>' % (spec[NAME], spec[TYPE]) |
| 169 | if len(spec) >= SPEC_LENGTH: |
| 170 | if type(spec[DEFAULT]) == types.StringType: |
| 171 | definition = spec[HELP] + ' (' + spec[DEFAULT] + ')' |
| 172 | else: |
| 173 | definition = spec[HELP] + ' (' + str(spec[DEFAULT]) + ')' |
| 174 | else: |
| 175 | definition = spec[HELP] |
| 176 | rtn = rtn + format % (arg, definition) |
| 177 | |
| 178 | return rtn |
| 179 | |
| 180 | def exists(key): |
| 181 | return configDict.has_key(key) |
| 182 | |
| 183 | def get(key): |
| 184 | return configDict[key] |
| 185 | |
| 186 | def set(key, value): |
| 187 | global configDict |
| 188 | |
| 189 | configDict[key] = value |
| 190 | |
| 191 | configDict = {} |