This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
RE: Option parsing in gdb python
- From: Robert Lupton the Good <rhl at astro dot princeton dot edu>
- To: gdb at sourceware dot org
- Date: Mon, 24 Oct 2011 17:05:51 -0400
- Subject: RE: Option parsing in gdb python
- References: <1319403653.11425.ezmlm@sourceware.org>
Paul Koning wrote:
> Since argparse is the successor to optparse and is easier to use and more extensible, would you consider doing this in argparse instead?
I can do this (and did; read on, Dear Reader). Does gdb python have a position on python 2.6 support? If so, and if gdb accepts this parser, we should provide both and wrap the imports in try blocks
try:
import argparse
…
except ImportError:
pass
etc.
OK, using argparse here goes:
parser = GdbArgumentParser("show image")
parser.add_argument("-a", "--all", action="store_true", help="Display the whole image/mask")
parser.add_argument("image", help="Expression giving image to show")
parser.add_argument("width", help="Width of patch to print", default=1, nargs="?")
opts = parser.parse_args(args)
R
import argparse
class GdbArgumentParser(argparse.ArgumentParser):
def parse_args(self, args=None, namespace=None):
if args:
args = gdb.string_to_argv(args)
return argparse.ArgumentParser.parse_args(self, args, namespace)
def exit(self, status=0, msg=None):
raise gdb.GdbError(msg)