python - Converting optparse to argparse -


using optparse, have:

opts, args = parser.parse_args(sys.argv[1:]) 

which feeding function accepts opts:

func(opts,sys.argv) 

i'm trying use argparse, formatting argparse different:

args = parser.parse_args(sys.argv[1:]) 

which doesn't allow me feed opts function.

i wondering if there's way use argparse while maintaining opts feed function.

i'm using python 2.7.

you can use argparse in similar way optparse. the documentation can see need prefix arguments either - or -- , becomes optional parameter.

here's example documentation:

parser = argparse.argumentparser() parser.add_argument('-f', '--foo') parser.add_argument('bar') args = parser.parse_args 

additionally, can change prefix chars if program needs have options prefixed different charaters:

parser = argparse.argumentparser(prefix_chars='+') parser.add_argument('+f', '++foo') parser.add_argument('bar') args = parser.parse_args 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -