This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Option parsing in gdb python

Since argparse is the successor to optparse and is easier to use and more extensible, would you consider doing this in argparse instead?


-----Original Message-----
From: [] On Behalf Of Robert Lupton the Good
Sent: Friday, October 21, 2011 9:19 PM
Subject: Option parsing in gdb python

I just spent a few minutes fiddling with optparse (deprecated in python 2.7, but I don't think I care) to make it usable from gdb python (the problem is that by default it uses sys.argv[0] for help messages, and exits on error).  With my subclass you can say e.g.

            parser = GdbOptionParser("show image [opts] [nx [ny]]")
            parser.add_option("-a", "--all", action="store_true", help="Display the whole image/mask")
            parser.add_option("-w", "--width", type="int", default=8, help="Field width for pixels")

            (opts, args) =  parser.parse_args(args)

and things "just work".  I'd be happy to donate the code to fsf/gdb


import gdb
import optparse

class GdbOptionParser(optparse.OptionParser):
    def __init__(self, usage, *args, **kwargs):
        optparse.OptionParser.__init__(self, *args, **kwargs) # OptionParser is an old-style class

    def parse_args(self, args, values=None):
        """Call optparse.OptionParser.parse_args after running gdb.string_to_argv on args"""
        return optparse.OptionParser.parse_args(self, gdb.string_to_argv(args), values)

    def exit(self, status=0, msg=None):
        raise gdb.GdbError(msg)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]