From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27044 invoked by alias); 22 Oct 2011 01:18:51 -0000 Received: (qmail 27031 invoked by uid 22791); 22 Oct 2011 01:18:51 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from kay.astro.Princeton.EDU (HELO mail.astro.princeton.edu) (128.112.24.221) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 22 Oct 2011 01:18:37 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.astro.princeton.edu (Postfix) with ESMTP id 6607B1BC0050A for ; Fri, 21 Oct 2011 21:18:36 -0400 (EDT) Received: from mail.astro.princeton.edu ([127.0.0.1]) by localhost (kay.astro.princeton.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id xPjQBGCLiklG for ; Fri, 21 Oct 2011 21:18:31 -0400 (EDT) X-Submitted: to mail.astro.princeton.edu (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: rhl) with ESMTP for ; Fri, 21 Oct 2011 21:18:31 -0400 (EDT) From: Robert Lupton the Good Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Option parsing in gdb python Date: Sat, 22 Oct 2011 01:22:00 -0000 Message-Id: To: gdb@sourceware.org Mime-Version: 1.0 (Apple Message framework v1251.1) Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2011-10/txt/msg00168.txt.bz2 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 erro= r). With my subclass you can say e.g. parser =3D GdbOptionParser("show image [opts] [nx [ny]]") parser.add_option("-a", "--all", action=3D"store_true", help=3D= "Display the whole image/mask") parser.add_option("-w", "--width", type=3D"int", default=3D8, h= elp=3D"Field width for pixels") (opts, args) =3D 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) # OptionParse= r is an old-style class self.set_usage(usage) def parse_args(self, args, values=3DNone): """Call optparse.OptionParser.parse_args after running gdb.string_t= o_argv on args""" return optparse.OptionParser.parse_args(self, gdb.string_to_argv(ar= gs), values) def exit(self, status=3D0, msg=3DNone): raise gdb.GdbError(msg)