ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-dev
python-dev
Re: [Python-Dev] PEP 389: argparse - new command line parsing module
by Robert Kern other posts by this author
Sep 30 2009 2:02PM messages near this date
Re: [Python-Dev] PEP 389: argparse - new command line parsing module | Re: [Python-Dev] PEP 389: argparse - new command line parsing module
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@[...].org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-dev-ml%40maillist.acti
vestate.com
Thread:
Steven Bethard
Nick Coghlan
Vinay Sajip
Vinay Sajip
Brett Cannon
Guido van Rossum
Robert Kern
Barry Warsaw
Andrew McNabb
Steven Bethard
Andrew McNabb
Robert Kern
Vinay Sajip
Fred Drake
Yuvgoog Greenle
Neal Becker
Steven Bethard
Michael Foord
Floris Bruynooghe
Jon Ribbens
Steven Bethard
Floris Bruynooghe
Steven D'Aprano
Steven Bethard
Brett Cannon
martin
Brett Cannon
skip
Greg Ewing
Nick Coghlan
Barry Warsaw
Nick Coghlan
Barry Warsaw
Steven Bethard
Steven Bethard
Yuvgoog Greenle
Paul Moore
Greg Ewing
Paul Moore
Nick Coghlan
Steven Bethard
Glenn Linderman
Steven Bethard
Eric Smith
M H
Yuvgoog Greenle
Steven Bethard

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved