changeset 155:f03fe3d6d0c8

added functions to parse options
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Sep 2011 19:24:29 -0400
parents 668710d4c773
children 2eef5620c0b3
files python/utils.py
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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
 #########################