On 2009-09-30 15:17 PM, Andrew McNabb wrote:
On Wed, Sep 30, 2009 at 02:22:53PM -0400, Barry Warsaw wrote:

Right.  I've made it kind of work in Mailman 3, but it would be nice for
argparse to support this out of the box.  Note that I think you want two
forms:

foo help subcommand
foo subcommand --help

to basically print the same help text.  This is the way bzr does it for
example and it works great.

In some commands, options as well as subcommands can change subsequent
parsing.  The iptables command is a good example of a command-line
program that follows this practice.  From the man page:

   after [a module name is specified], various extra command line options
   become available, depending on the specific module.  You can specify
   multiple extended match modules in one line, and you can use the -h or
   --help options after the module has been specified to receive help
   specific to that module.

In the case of iptables, module names are specified as options, not as
subcommands.

From my cursory reading of the documentation, it looks like argparse can
only add subparsers for subcommands.  Is there any way to add subparsers
based on options instead (as iptables does)?

I have not done so, but I suspect so. The implementation of .add_subparsers() adds to the positional argument list, but one could be written to append to the option list.

Also, is it possible to add these subparsers dynamically?  For example,
you would want to be able to load a module immediately after parsing the
name instead of having to keep a predetermined list of all module names.
I'm pretty sure that bzr dynamically loads modules this way.  Can
argparse help with this?

Not out-of-box, but it looks fairly straightforward to plug in. The subparser logic is mostly encapsulated in the _SubparsersAction class. You can register a new class for it:

    parser.register('action', 'parsers', MySubParsersAction)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to