optparse make_option() equivalent in argparse

92 views
Skip to first unread message

abhishek bhat

unread,
Jan 17, 2013, 3:24:02 PM1/17/13
to argpars...@googlegroups.com
From the OPTPARSE library reference:

option_list
= [ make_option("-f", "--filename", action="store", type="string", dest="filename"), make_option("-q", "--quiet", action="store_false", dest="verbose"), ]

parser = OptionParser(option_list=option_list) ___________________________________________________________________________

Like the above example, I want to make a option list using make_option and pass it to a decorator which instantiates the parser and adds the arguments.

How can this be achieved in argparse? Is there a way to populate the parser other than parse_args()?


Josh English

unread,
Mar 29, 2013, 7:40:24 PM3/29/13
to argpars...@googlegroups.com
OptParse's make_option is just a synonym for the Option class, and the OptionParser constructor takes a list.  

You could probably create a function like so:

def make_arg_parser(arguments):
    parser = argparse.ArgumentParser()
    for arg in arguments:
        parser.add_argument(arg)

Your arguments constructor could be a tuple of tuples, with no make_option equivalent.

But I think ArgParser's strength is creating subparsers and attaching them to parents so a set of options can be re-used.

Josh

Reply all
Reply to author
Forward
0 new messages