[Twisted-Python] twisted.python.usage.Options Questions

Justin Johnson justinjohnson at fastmail.fm
Mon May 19 09:07:15 MDT 2003


Looking at the code I saw that when a subcommand is passed in, the
following code in parseOptions resets the command to be the subcommand
and calls parseOptions again. 


      if len(args) and getattr(self, 'subCommands', None):
            sub, rest = args[0], args[1:]
            for (cmd, short, parser, doc) in self.subCommands:
                if sub == cmd or sub == short:
                    self.subCommand = cmd
                    self.subOptions = parser()
                    self.subOptions.parseOptions(rest)
                    break

Because of this, when we get to __str__, the method that is used to
display the usage info, we don't have anything we can look at to
determine if we should return __main__.__doc__ (if my.py --help was run)
or if we should return self.__doc__ (if my.py subcmd --help was run).

Would it make sense to change the code to always return self.__doc__, and
then the usage doc for the entire script could be stored in the base
Options class __doc__ (the one that defines subCommands)?  The following
patch changes this functionality, but of course it would break current
functionality.

Maybe there's a better way to do this, but I can't think of it.

Thanks.
-Justin


--- usage.py.orig       2003-05-19 08:09:55.000000000 -0500
+++ usage.py    2003-05-19 09:46:01.000000000 -0500
@@ -442,9 +442,8 @@
         if not (getattr(self, "longdesc", None) is None):
             longdesc = self.longdesc
         else:
-            import __main__
-            if getattr(__main__, '__doc__', None):
-                longdesc = __main__.__doc__
+            if getattr(self, '__doc__', None):
+                longdesc = self.__doc__
             else:
                 longdesc = ''




On Fri, 16 May 2003 16:52:45 -0400, "Jp Calderone" <exarkun at intarweb.us>
said:
> On Fri, May 16, 2003 at 12:41:35PM -0600, Justin Johnson wrote:
> > 
> > > > 1)  Passing in the --help option displays usage info the way I'd want it,
> > > > grabbing __doc__ from the top of the file and also displaying info about
> > > > my subcommands.  
> > > > 
> > > > [snip output]
> > > > 
> > > > Is there a way to do that?
> > > 
> > >   Hmm, I don't believe so.  Usage could probably be patched to print
> > > something slightly different for options with subcommands.
> > 
> > I was thinking it would make sense to grab the doc from the class that
> > handles the options for the particular subcommand, since it is the only
> > class/function that we already know the name of and that is already
> > associated with the subcommand.
> > [snip]
> >
> > Does that make sense?  Is that something you can do, or should I get
> > familiar with the code and submit a patch?
> > Thanks for your other suggestions as well.
> 
>   This is just what I was thinking.  If the change to make was obvious to
> me, I'd do it, but after a quick look I find that I don't entirely
> remember
> how this code works.  Could you come up with a patch for this?
> 
>   Jp
> 
> -- 
> "If you find a neighbor in need, you're responsible for serving that
> neighbor in need, you're responsible for loving a neighbor just like
> you'd
> like to love yourself." -- George W. Bush, Sept. 16, 2002
> -- 
>  up 14 days, 18:39, 3 users, load average: 0.02, 0.02, 0.00




More information about the Twisted-Python mailing list