public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* RE: Option parsing in gdb python
       [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-27 19:32   ` Tom Tromey
  0 siblings, 2 replies; 7+ messages in thread
From: Robert Lupton the Good @ 2011-10-24 21:28 UTC (permalink / raw)
  To: gdb

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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Option parsing in gdb python
  2011-10-24 21:28 ` Option parsing in gdb python 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
  1 sibling, 2 replies; 7+ messages in thread
From: Joel Brobecker @ 2011-10-24 21:35 UTC (permalink / raw)
  To: Robert Lupton the Good; +Cc: gdb

The part that I don't get at the moment is how this is all going
to be useful in general. Can you give some example of how you want
to use this class?

PS: As far as I know, I think that our position is that we support
versions 2.4 to 2.7.

-- 
Joel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Option parsing in gdb python
  2011-10-24 21:35   ` Joel Brobecker
@ 2011-10-25  2:58     ` Robert Lupton the Good
  2011-10-27 17:01     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Robert Lupton the Good @ 2011-10-25  2:58 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: gdb

> The part that I don't get at the moment is how this is all going to be useful in general. Can you give some example of how you want to use this class?

Oh, sorry.  I have an image class, and I want to support
	show image img
but I also want to specify the format width, the part of the image to show, etc.  So

	show image -a img		# print it all
	show image -o 10 20 img 5 5	# print the 5x5 subimage starting at (10, 20)
	show image -o 10 20 -c img 5 5	# print the 5x5 subimage centred at (10, 20)
	show image -f 4 -a img		# print it all, using a field width of 4 chars

That last one looks like:
> (gdb) show image -f 4 -a idImage
>      0 1 2 3 4 5 6 7 8 9 10 11 
> 7    0x0 0x0 0x0 0x3 0x0 0x0 0x0 0x0 0x2 0x2 0x0 0x0 
> 6    0x0 0x0 0x3 0x3 0x3 0x0 0x0 0x2 0x2 0x2 0x2 0x0 
> 5    0x0 0x0 0x0 0x3 0x0 0x0 0x2 0x2 0x2 0x2 0x2 0x2 
> 4    0x0 0x0 0x0 0x0 0x1 0x0 0x0 0x2 0x2 0x0 0x2 0x0 
> 3    0x0 0x0 0x0 0x1 0x1 0x1 0x0 0x0 0x0 0x0 0x0 0x0 
> 2    0x0 0x0 0x1 0x1 0x1 0x1 0x1 0x0 0x0 0x0 0x0 0x0 
> 1    0x0 0x0 0x0 0x1 0x1 0x1 0x0 0x0 0x0 0x0 0x0 0x0 
> 0    0x0 0x0 0x0 0x0 0x1 0x0 0x0 0x0 0x0 0x0 0x0 0x0 

I also have similar needs for eigen::Matrix, and ...

So I call GdbArgumentParse in the invoke method of a subclass of gdb.Command.

					R

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Option parsing in gdb python
  2011-10-24 21:35   ` Joel Brobecker
  2011-10-25  2:58     ` Robert Lupton the Good
@ 2011-10-27 17:01     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2011-10-27 17:01 UTC (permalink / raw)
  To: Joel Brobecker; +Cc: Robert Lupton the Good, gdb

>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:

Joel> PS: As far as I know, I think that our position is that we support
Joel> versions 2.4 to 2.7.

Yes, or at least that is the current concrete expression of the rule "we
support whatever people care enough about to submit patches to keep
working".

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Option parsing in gdb python
  2011-10-24 21:28 ` Option parsing in gdb python Robert Lupton the Good
  2011-10-24 21:35   ` Joel Brobecker
@ 2011-10-27 19:32   ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2011-10-27 19:32 UTC (permalink / raw)
  To: Robert Lupton the Good; +Cc: gdb

>>>>> "Robert" == Robert Lupton the Good <rhl@astro.princeton.edu> writes:

Robert> I can do this (and did; read on, Dear Reader).  Does gdb python
Robert> have a position on python 2.6 support?  If so, and if gdb
Robert> accepts this parser, we should provide both and wrap the imports
Robert> in try blocks

I think something like this would be worthwhile.

The bar is reasonably high for getting stuff into gdb.
You need copyright assignment papers for non-trivial additions; plus
documentation and a test case.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Option parsing in gdb python
  2011-10-22  1:22 Robert Lupton the Good
@ 2011-10-23 21:00 ` Paul_Koning
  0 siblings, 0 replies; 7+ messages in thread
From: Paul_Koning @ 2011-10-23 21:00 UTC (permalink / raw)
  To: rhl, gdb

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

	paul

-----Original Message-----
From: gdb-owner@sourceware.org [mailto:gdb-owner@sourceware.org] On Behalf Of Robert Lupton the Good
Sent: Friday, October 21, 2011 9:19 PM
To: gdb@sourceware.org
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

					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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Option parsing in gdb python
@ 2011-10-22  1:22 Robert Lupton the Good
  2011-10-23 21:00 ` Paul_Koning
  0 siblings, 1 reply; 7+ messages in thread
From: Robert Lupton the Good @ 2011-10-22  1:22 UTC (permalink / raw)
  To: gdb

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)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-10-27 17:01 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1319403653.11425.ezmlm@sourceware.org>
2011-10-24 21:28 ` Option parsing in gdb python 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
2011-10-22  1:22 Robert Lupton the Good
2011-10-23 21:00 ` Paul_Koning

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).