Пример #1
0
    def add_opt(self, name, npar, deflist=[], acplist=[], req=0, setpar=0,  \
                helpstr = "", okdash=1):
        """add an option to the current OptionList

                name    : option name, to be provided on command line
                npar    : number of parameters
                              > 0 --> require exactly that number
                              < 0 --> require at least the positive number
                deflist : default parmeter list (required, for now)
                acplist : list of acceptable values
                req     : flag: is this required?
                setpar  : flag: set option parlist from deflist
                okdash  : flag: if set, params are allowed to start with '-'
        """
        
        com = BASE.comopt(name, npar, deflist, acplist, helpstr)
        com.required = req
        com.okdash = okdash
        if setpar: com.parlist = com.deflist
        self.olist.append(com)
Пример #2
0
    def add_opt(self, name, npar, deflist=[], acplist=[], req=0, setpar=0,  \
                helpstr = "", okdash=1):
        """add an option to the current OptionList

                name    : option name, to be provided on command line
                npar    : number of parameters
                              > 0 --> require exactly that number
                              < 0 --> require at least the positive number
                deflist : default parmeter list (required, for now)
                acplist : list of acceptable values
                req     : flag: is this required?
                setpar  : flag: set option parlist from deflist
                okdash  : flag: if set, params are allowed to start with '-'
        """

        com = BASE.comopt(name, npar, deflist, acplist, helpstr)
        com.required = req
        com.okdash = okdash
        if setpar: com.parlist = com.deflist
        self.olist.append(com)
Пример #3
0
def read_options(argv, oplist, verb = -1):
    """Input an OptionList element, containing a list of options, required
       or not, and return an OptionList of options as they are found.

       If verb is not passed, apply that of oplist.

       return: an OptionList element, or None on a terminal error
       note: options may occur more than once
    """

    OL = OptionList("read_options")

    if verb < 0: verb = oplist.verb

    alen = len(argv)
    if alen == 0: return OL

    # prepare a dictionary counting uses of each user option
    namelist = {}
    for co in oplist.olist:
        if co.name in namelist:   # complain if input list contains repeats
            print "** RO warning: option '%s' appears more than once"%co.name
        namelist[co.name] = 0
    if verb > 1 : print "-d namelist: ", namelist

    # parse the input arguments:
    #   for each arg, verify arg is option, then process params
    #   so ac increments by 1+num_params each time
    ac = 1
    while ac < alen:
        # -optlist_* : global options to be ignored
        if argv[ac] in [ '-optlist_verbose', '-optlist_no_show_count' ]:
            if oplist.verb > 1: print "-- found optlist opt '%s'" % argv[ac]
            ac += 1
            continue

        com = oplist.find_opt(argv[ac])
        if com:
            namelist[argv[ac]] += 1     # increment dictionary count
            if verb > 2: print "+d found option '%s'" % com.name
            if verb > 3: print "-d remaining args: %s" % argv[ac:-1]

            # create new return option
            newopt = BASE.comopt(com.name, com.n_exp, com.deflist)
            newopt.i_name = ac          # current index into argv
            newopt.acceptlist = com.acceptlist
            newopt.required = com.required 
            ac += 1                     # now point to next argument

            # create parlist of potential parameters
            if newopt.n_exp > 0:    # try to insert that number of args
                if newopt.n_exp <= alen - ac:
                    if verb > 2: print "+d adding %d params" % newopt.n_exp
                    parlist = argv[ac:ac+newopt.n_exp]
                else:   # too few args
                    print "** error: arg #%d (%s) requires %d params" % \
                          (ac-1, newopt.name, newopt.n_exp)
                    return None
            elif newopt.n_exp < 0:  # grab everything, and truncate later
                if verb > 2: print "+d start with all %d params" % (alen-ac)
                parlist = argv[ac:]
            else: parlist = []      # n_exp == 0

            # truncate parlist if it contains an option
            for pc in range(len(parlist)):
                if parlist[pc] in namelist: # then we have pc 'good' params
                    parlist = parlist[:pc]
                    if verb > 1: print "-d truncate %s after %d of %d" % \
                                       (newopt.name, pc, len(parlist))
                    break;

            # now check parlist against acceptlist
            if newopt.acceptlist:
                for par in parlist:
                    # check against repr(list element), since par is a string
                    # (search slowly for older versions of python)
                    found = 0
                    for accpar in newopt.acceptlist:
                        if par == str(accpar): found = 1
                    if not found:  # panic into error!  aaas yoooou wiiiiish...
                        print "** option %s: param '%s' is not in: %s" % \
                              (newopt.name, par, newopt.acceptlist)
                        return None  # what else can we do?

            # so do we still have enough parameters?
            if newopt.n_exp < 0: nreq = abs(newopt.n_exp)
            else:                nreq = newopt.n_exp
            if len(parlist) < nreq:
                print "** error: arg #%d (%s) requires %d params, found %d" % \
                      (ac-1, newopt.name, nreq, len(parlist))
                return None

            # we have a full parlist, possibly check for dashes now
            if not com.okdash:
               for par in parlist:
                  if not par: continue  # check for empty param?  too anal?
                  if par[0] == '-':
                     print '** option %s has illegal dashed parameter: %s' \
                           % (newopt.name, par)
                     print '   --> maybe parameter is a mis-typed option?'
                     return None

            # success!  insert the remaining list
            newopt.parlist = parlist
            newopt.n_found = len(parlist)

        else:   # we seem to be done with expected arguments
            # there should not be any options in this final list
            for arg in argv[ac:]:
                if arg in namelist:
                    print "** error: option %s follows unknown arg #%d (%s)" % \
                          (arg, ac, argv[ac])
                    return None

            if not oplist.trailers :   # then trailers are not allowed
                print "** error: unknown trailing arguments : %s" % argv[ac:]
                return None

            # insert remaining args as trailers
            newopt = BASE.comopt('trailers', -1, [])
            newopt.n_found = alen - ac
            newopt.parlist = argv[ac:]
            if verb > 2: print "-- found trailing args: %s" % newopt.parlist

        OL.olist.append(newopt) # insert newopt into our return list
        ac += newopt.n_found    # and increment the argument counter

    # now we have processed all of argv
    # any unused comopt that has a deflist can be used (else error)
            
    for co in oplist.olist:
        if namelist[co.name] == 0:  # may still be okay
            if co.required: 
                print "** error: missing option %s" % co.name
                return None
            elif len(co.deflist) > 0:  # use it
                newopt = BASE.comopt(co.name, len(co.deflist), co.deflist)
                newopt.parlist = newopt.deflist
                # leave n_found at -1, so calling function knows
                OL.olist.append(newopt) # insert newopt into our return list
                if verb > 2: print "++ applying default opt '%s', args: %s" % \
                                   (co.name, newopt.deflist)

    if verb > 1 : OL.show("-d all found options: ")
    if verb > 3 : print "-d final optlist with counts: ", namelist

    return OL
Пример #4
0
def read_options(argv, oplist, verb=-1):
    """Input an OptionList element, containing a list of options, required
       or not, and return an OptionList of options as they are found.

       If verb is not passed, apply that of oplist.

       return: an OptionList element, or None on a terminal error
       note: options may occur more than once
    """

    OL = OptionList("read_options")

    if verb < 0: verb = oplist.verb

    alen = len(argv)
    if alen == 0: return OL

    # prepare a dictionary counting uses of each user option
    namelist = {}
    for co in oplist.olist:
        if co.name in namelist:  # complain if input list contains repeats
            print("** RO warning: option '%s' appears more than once" %
                  co.name)
        namelist[co.name] = 0
    if verb > 1: print("-d namelist: ", namelist)

    # parse the input arguments:
    #   for each arg, verify arg is option, then process params
    #   so ac increments by 1+num_params each time
    ac = 1
    while ac < alen:
        # -optlist_* : global options to be ignored
        if argv[ac] in ['-optlist_verbose', '-optlist_no_show_count']:
            if oplist.verb > 1: print("-- found optlist opt '%s'" % argv[ac])
            ac += 1
            continue

        com = oplist.find_opt(argv[ac])
        if com:
            namelist[argv[ac]] += 1  # increment dictionary count
            if verb > 2: print("+d found option '%s'" % com.name)
            if verb > 3: print("-d remaining args: %s" % argv[ac:-1])

            # create new return option
            newopt = BASE.comopt(com.name, com.n_exp, com.deflist)
            newopt.i_name = ac  # current index into argv
            newopt.acceptlist = com.acceptlist
            newopt.required = com.required
            ac += 1  # now point to next argument

            # create parlist of potential parameters
            if newopt.n_exp > 0:  # try to insert that number of args
                if newopt.n_exp <= alen - ac:
                    if verb > 2: print("+d adding %d params" % newopt.n_exp)
                    parlist = argv[ac:ac + newopt.n_exp]
                else:  # too few args
                    print("** error: arg #%d (%s) requires %d params" % \
                          (ac-1, newopt.name, newopt.n_exp))
                    return None
            elif newopt.n_exp < 0:  # grab everything, and truncate later
                if verb > 2: print("+d start with all %d params" % (alen - ac))
                parlist = argv[ac:]
            else:
                parlist = []  # n_exp == 0

            # truncate parlist if it contains an option
            for pc in range(len(parlist)):
                if parlist[pc] in namelist:  # then we have pc 'good' params
                    parlist = parlist[:pc]
                    if verb > 1:                        print("-d truncate %s after %d of %d" % \
                              (newopt.name, pc, len(parlist)))
                    break

            # now check parlist against acceptlist
            if newopt.acceptlist:
                for par in parlist:
                    # check against repr(list element), since par is a string
                    # (search slowly for older versions of python)
                    found = 0
                    for accpar in newopt.acceptlist:
                        if par == str(accpar): found = 1
                    if not found:  # panic into error!  aaas yoooou wiiiiish...
                        print("** option %s: param '%s' is not in: %s" % \
                              (newopt.name, par, newopt.acceptlist))
                        return None  # what else can we do?

            # so do we still have enough parameters?
            if newopt.n_exp < 0: nreq = abs(newopt.n_exp)
            else: nreq = newopt.n_exp
            if len(parlist) < nreq:
                print("** error: arg #%d (%s) requires %d params, found %d" % \
                      (ac-1, newopt.name, nreq, len(parlist)))
                return None

            # we have a full parlist, possibly check for dashes now
            if not com.okdash:
                for par in parlist:
                    if not par: continue  # check for empty param?  too anal?
                    if par[0] == '-':
                        print('** option %s has illegal dashed parameter: %s' \
                              % (newopt.name, par))
                        print('   --> maybe parameter is a mis-typed option?')
                        return None

            # success!  insert the remaining list
            newopt.parlist = parlist
            newopt.n_found = len(parlist)

        else:  # we seem to be done with expected arguments
            # there should not be any options in this final list
            for arg in argv[ac:]:
                if arg in namelist:
                    print("** error: option %s follows unknown arg #%d (%s)" % \
                          (arg, ac, argv[ac]))
                    return None

            if not oplist.trailers:  # then trailers are not allowed
                print("** error: unknown trailing arguments : %s" % argv[ac:])
                return None

            # insert remaining args as trailers
            newopt = BASE.comopt('trailers', -1, [])
            newopt.n_found = alen - ac
            newopt.parlist = argv[ac:]
            OL.trailers = 1  # flag to calling function
            if verb > 2: print("-- found trailing args: %s" % newopt.parlist)

        OL.olist.append(newopt)  # insert newopt into our return list
        ac += newopt.n_found  # and increment the argument counter

    # now we have processed all of argv
    # any unused comopt that has a deflist can be used (else error)

    for co in oplist.olist:
        if namelist[co.name] == 0:  # may still be okay
            if co.required:
                print("** error: missing option %s" % co.name)
                return None
            elif len(co.deflist) > 0:  # use it
                newopt = BASE.comopt(co.name, len(co.deflist), co.deflist)
                newopt.parlist = newopt.deflist
                # leave n_found at -1, so calling function knows
                OL.olist.append(newopt)  # insert newopt into our return list
                if verb > 2:                    print("++ applying default opt '%s', args: %s" % \
                          (co.name, newopt.deflist))

    if verb > 1: OL.show("-d all found options: ")
    if verb > 3: print("-d final optlist with counts: ", namelist)

    # check for terminal options in oplist
    if oplist.show_argv_array != '':
        OL.show_as_array("-- show_argv_array: found options",
                         atype=oplist.show_argv_array)
        sys.exit(0)

    return OL
Пример #5
0
#!/usr/bin/env python
import os, sys, string
import afni_base
from pydoc import help

test = 1

#define rules for options
oplist = []
op = afni_base.comopt('-areas', -2, \
   ["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
Пример #6
0
#!/usr/bin/env python
import os, sys, string
import afni_base
from pydoc import help

test = 1

#define rules for options
oplist = []
op = afni_base.comopt('-areas', -2, \
   ["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():