Mercurial > hg > nsaunier > traffic-intelligence
comparison python/utils.py @ 155:f03fe3d6d0c8
added functions to parse options
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 08 Sep 2011 19:24:29 -0400 |
| parents | 74b1fc68d4df |
| children | 42142c5223ce |
comparison
equal
deleted
inserted
replaced
| 154:668710d4c773 | 155:f03fe3d6d0c8 |
|---|---|
| 7 __metaclass__ = type | 7 __metaclass__ = type |
| 8 | 8 |
| 9 commentChar = '#' | 9 commentChar = '#' |
| 10 | 10 |
| 11 delimiterChar = '%'; | 11 delimiterChar = '%'; |
| 12 | |
| 13 | |
| 14 ######################### | |
| 15 # CLI utils | |
| 16 ######################### | |
| 17 | |
| 18 def parseCLIOptions(helpMessage, options, cliArgs, optionalOptions=[]): | |
| 19 ''' Simple function to handle similar argument parsing | |
| 20 Returns the dictionary of options and their values | |
| 21 | |
| 22 * cliArgs are most likely directly sys.argv | |
| 23 (only the elements after the first one are considered) | |
| 24 | |
| 25 * options should be a list of strings for getopt options, | |
| 26 eg ['frame=','correspondences=','video='] | |
| 27 A value must be provided for each option, or the program quits''' | |
| 28 import sys, getopt | |
| 29 from numpy.core.fromnumeric import all | |
| 30 optionValues, args = getopt.getopt(cliArgs[1:], 'h', ['help']+options+optionalOptions) | |
| 31 optionValues = dict(optionValues) | |
| 32 | |
| 33 if '--help' in optionValues.keys() or '-h' in optionValues.keys(): | |
| 34 print(helpMessage) | |
| 35 sys.exit() | |
| 36 | |
| 37 missingArgument = [('--'+opt.replace('=','') in optionValues.keys()) for opt in options] | |
| 38 if not all(missingArgument): | |
| 39 print('Missing argument') | |
| 40 print(optionValues) | |
| 41 sys.exit() | |
| 42 | |
| 43 return optionValues | |
| 12 | 44 |
| 13 ######################### | 45 ######################### |
| 14 # simple statistics | 46 # simple statistics |
| 15 ######################### | 47 ######################### |
| 16 | 48 |
