using optparser

Michele Simionato michele.simionato at gmail.com
Sun Oct 17 06:24:02 CEST 2010


Accepting both options and positional arguments for the same purpose
does not look like a good idea to me.
Anyway, here is a solution using plac (http://pypi.python.org/pypi/
plac) assuming you can afford an external dependency:

import plac

@plac.annotations(
    personname=("person to be matched", 'option', 'i'),
    groupname=("group containing people to be tested against",
'option', 'f'),
    samplenumber=("how many samples to be used", 'option', 'n', int),
    cutoff=("some upperlimit", 'option', 't'))
def main(personname, groupname, samplenumber, cutoff, *args):
    # the implementation below does not allow mixing options and args
    # it works only if you give no options and the needed 4 arguments
    if (personname is None and groupname is None and samplenumber is
None
        and cutoff is None): # no options given, look at the arguments
        try:
            personname, groupname, samplenumber, cutoff = args
        except ValueError:
            plac.parser_from(main).error('Not enough arguments (got %d
of 4)'
                                         % len(args))
    return personname, groupname, samplenumber, cutoff

if __name__ == '__main__':
    import plac; print plac.call(main)




More information about the Python-list mailing list