[Python-Dev] PEP 389: argparse - new command line parsing module

Andrew McNabb amcnabb at mcnabbs.org
Wed Sep 30 22:17:35 CEST 2009

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

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

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?

Sorry for all of the questions.  I ask them because I have some
experience with adding the above features to optparse, and it was a lot
of work to get it right.  It also seems like there are a lot of programs
that need to load modules dynamically.  I would be really excited if
argparse made this easier than optparse did.

Andrew McNabb
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868

More information about the Python-Dev mailing list