From: Robert Lupton the Good <rhl@astro.princeton.edu>
To: gdb@sourceware.org
Subject: Option parsing in gdb python
Date: Sat, 22 Oct 2011 01:22:00 -0000 [thread overview]
Message-ID: <D769F2A3-0B3A-4485-A989-6C415BA49CA3@astro.princeton.edu> (raw)
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
R
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
self.set_usage(usage)
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)
next reply other threads:[~2011-10-22 1:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-22 1:22 Robert Lupton the Good [this message]
2011-10-23 21:00 ` Paul_Koning
[not found] <1319403653.11425.ezmlm@sourceware.org>
2011-10-24 21:28 ` Robert Lupton the Good
2011-10-24 21:35 ` Joel Brobecker
2011-10-25 2:58 ` Robert Lupton the Good
2011-10-27 17:01 ` Tom Tromey
2011-10-27 19:32 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=D769F2A3-0B3A-4485-A989-6C415BA49CA3@astro.princeton.edu \
--to=rhl@astro.princeton.edu \
--cc=gdb@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).