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
Post a Comment