["CA_N27_MPM", "area_6", "area_4a", "area_4p"]) oplist.append(op) op = afni_base.comopt('-areas_2', -2, \ ["CA_ZOUZOU", "areea_6", "areea_4a", "areea_4p"]) oplist.append(op) op = afni_base.comopt('-dsets', -1, []) oplist.append(op) op = afni_base.comopt('-sides', -1, ['left', 'right']) oplist.append(op) op = afni_base.comopt('-command', -1, []) oplist.append(op) opts = afni_base.getopts2(sys.argv, oplist) if opts == None: sys.exit() afni_base.show_opts2(opts) #find keys with -areas_ areas = [] for key in opts.keys(): if key.find('-areas') > -1: #here's one set of areas areas.append(key) #Now do the deed for ak in areas: op = opts[ak] #Get the areas_* option atlas = op.parlist[0] alist = op.parlist[1:] #the areas for side in opts['-sides'].parlist: for ar in alist: com = "rm -f %s_%s* >& /dev/null" % (ar, side)
def getopts2(argv,oplist): """ A function to parse command line arguments. to use it, you need to set up the options list. So, from a main you can do the following: oplist = [] # an option that needs no params oplist.append(afni_base.comopt('-dicom', 0, [])) # an option that needs 2 params, with 2 options, defaulting to 2 and 10.0 oplist.append(afni_base.comopt('-clust', 2, ['2', '10.0'])) # an option that needs an undetermined number of parameters # (-1 for 1 or more, -2 for 2 or more) oplist.append(afni_base.comopt('-dsets', -1, [])) once the list is made, you call getopts2 with argv and oplist opts = afni_base.getopts2(sys.argv, oplist) opts is a dictionary with the name of oplist elements as keys to get a quick look at it use: afni_base.show_opts2(opts) """ opts = {} if len(argv) == 0: return opts #Add the program name op = comopt('basename',0, []) opts['basename'] = op argv.remove( argv[0] ) #form a list of the known options optnames = [] for op in oplist: optnames.append(op.name) #find those options in oplist for op in oplist: if op.name in argv: op.n_found = 0 #found that argument op.iname = argv.index(op.name) #copy index into list argv.remove(op.name) #remove this option from list op.parlist = [] if op.n_exp < 0 or op.n_exp > 0: #parameters expected, get them while ((op.n_exp < 0 and op.iname < len(argv)) or \ (op.n_exp > 0 and len(op.parlist) < op.n_exp and len(argv) > 0))\ and argv[op.iname] not in optnames: if len(op.acceptlist): if argv[op.iname] not in op.acceptlist: print "Error: parameter value %s for %s is not " \ "acceptable\nChoose from %s" % \ (argv[op.iname], op.name, \ string.join(op.acceptlist, ' , ')) op.parlist.append(argv[op.iname]) #string added argv.remove(argv[op.iname]) #remove this string from list op.n_found = len(op.parlist) else : #No option in argv, just copy option op.parlist = op.deflist #Now copy results to dictionary opts[op.name] = op #a bit of redundancy, but I don't care if (op.test() == None): afni_base.show_opts2(opts) return None #Any remaining? for op in oplist: if op.name == 'loose': #Expecting loose params if op.n_exp < 0 or op.n_exp > 0: #parameters expected, get them op.parlist.extend(argv) #stick'em all in opts[op.name] = op if op.n_exp > 0 and len(op.parlist) != op.n_exp: print "Error: Expecting %d parameters\n" \ "Have %d on command line (%s).\n" % \ (op.n_exp, len(op.parlist), op.parlist) return None elif len(argv) > 0: print "Error: Expecting no loose parameters.\n" \ "Have %d loose parameters (or bad option) on " \ "command line (%s).\n" % (len(argv), argv) return None #go west young man return opts
oplist.append(op) op = afni_base.comopt('-areas_2', -2, \ ["CA_ZOUZOU", "areea_6", "areea_4a", "areea_4p"]) oplist.append(op) op = afni_base.comopt('-dsets', -1, []) oplist.append(op) op = afni_base.comopt('-sides', -1, ['left', 'right']) oplist.append(op) op = afni_base.comopt('-command', -1, []) oplist.append(op) opts = afni_base.getopts2(sys.argv, oplist) if opts == None: sys.exit() afni_base.show_opts2(opts) #find keys with -areas_ areas = [] for key in opts.keys(): if key.find('-areas') > -1: #here's one set of areas areas.append(key) #Now do the deed for ak in areas: op = opts[ak] #Get the areas_* option atlas = op.parlist[0] alist = op.parlist[1:] #the areas for side in opts['-sides'].parlist: for ar in alist: com = "rm -f %s_%s* >& /dev/null" % (ar, side)