# HG changeset patch # User Nicolas Saunier # Date 1315524269 14400 # Node ID f03fe3d6d0c88f23e83c255ee5c4709dccd70bc3 # Parent 668710d4c77355fdac3a18667efdda9883749306 added functions to parse options diff -r 668710d4c773 -r f03fe3d6d0c8 python/utils.py --- a/python/utils.py Wed Sep 07 16:35:51 2011 -0400 +++ b/python/utils.py Thu Sep 08 19:24:29 2011 -0400 @@ -10,6 +10,38 @@ delimiterChar = '%'; + +######################### +# CLI utils +######################### + +def parseCLIOptions(helpMessage, options, cliArgs, optionalOptions=[]): + ''' Simple function to handle similar argument parsing + Returns the dictionary of options and their values + + * cliArgs are most likely directly sys.argv + (only the elements after the first one are considered) + + * options should be a list of strings for getopt options, + eg ['frame=','correspondences=','video='] + A value must be provided for each option, or the program quits''' + import sys, getopt + from numpy.core.fromnumeric import all + optionValues, args = getopt.getopt(cliArgs[1:], 'h', ['help']+options+optionalOptions) + optionValues = dict(optionValues) + + if '--help' in optionValues.keys() or '-h' in optionValues.keys(): + print(helpMessage) + sys.exit() + + missingArgument = [('--'+opt.replace('=','') in optionValues.keys()) for opt in options] + if not all(missingArgument): + print('Missing argument') + print(optionValues) + sys.exit() + + return optionValues + ######################### # simple statistics #########################