Thursday, 15 August 2013

python - Can I pass on options from optparse to argparse? -



python - Can I pass on options from optparse to argparse? -

i wrapping class exposes optionparser through property named options_parser. wrapping class in 'runner' i've written utilize argparse. utilize argumentparser's parse_known_args() method parse wrapper's argument, , of remaining arguments pass on instance of wrapped class.

running ./wrapper.py --help not list options wrapped class. there convenient way add together optparse options wrapper's argparse argument?

if it's solely displaying options, 1 way can think of utilize format_help of optparse , set in epilog of argparse, example:

in [303]: foo = optionparser() in [304]: foo.add_option("-f", "--file", dest="filename",help="read info filename") in [305]: foo.add_option("-v", "--verbose",action="store_true", dest="verbose") in [311]: bar = argumentparser(epilog = foo.format_help(), formatter_class = rawtexthelpformatter) in [312]: bar.add_argument('integers', metavar='n', type=int, nargs='+',help='an integer accumulator') in [313]: bar.add_argument('--sum', dest='accumulate', action='store_const',const=sum, default=max,help='sum integers (default: find max)') in [314]: bar.print_help() usage: ipython [-h] [--sum] n [n ...] positional arguments: n integer accumulator optional arguments: -h, --help show help message , exit --sum sum integers (default: find max) usage: ipython [options] options: -h, --help show help message , exit -f filename, --file=filename read info filename -v, --verbose

you can of course of study format epilog want, including explanation 2 lists of options. might experiment different formatter, though default 1 doesn't work in case because strips newlines epilog. if desperate layout might seek create own formatter subclassing argparse.helpformatter, though i'd not recommend based on class docs:

"""formatter generating usage messages , argument help strings. name of class considered public api. methods provided class considered implementation detail. """

python argparse optparse

No comments:

Post a Comment