Reputation: 21
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()?
Upvotes: 2
Views: 1680
Reputation: 9
def process_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', dest='filename', type=string, action='store')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = process_args()
Upvotes: 0
Reputation: 309929
You can give argparse.ArgumentParser
a list of parent parsers:
parent = argparse.ArgumentParser(add_help=False)
parent.add_argument('-f','--filename')
parent.add_argument('-q','--quiet',action='store_false',dest='verbose')
parser = argparse.ArgumentParser(parents=[parent])
...
namespace = parser.parse_args()
Upvotes: 2