Re: [Python-Dev] updated PEP 389: argparse

2009-10-25 Thread Nick Coghlan
Steven Bethard wrote:
 Sorry for the delay, but I've finally updated PEP 389, the argparse
 PEP, based on all the feedback from python-dev. The full PEP is below,
 but in short, the important changes are:
 
 * The getopt module will *not* be deprecated.
 * In Python 2, the -3 flag will cause deprecation warnings to be
 issued for optparse.
 * No casually visible deprecation warnings for optparse are expected
 until Jun 2013.
 * Support for {}-format strings will be added when one of the
 converters is approved. This can be done at any point though, so it
 shouldn't hold up inclusion of argparse.

The new version provides a very good summary of and response to the
feedback on the previous version of the PEP.

+1 on adding the module :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
---
___
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


[Python-Dev] updated PEP 389: argparse

2009-10-24 Thread Steven Bethard
Sorry for the delay, but I've finally updated PEP 389, the argparse
PEP, based on all the feedback from python-dev. The full PEP is below,
but in short, the important changes are:

* The getopt module will *not* be deprecated.
* In Python 2, the -3 flag will cause deprecation warnings to be
issued for optparse.
* No casually visible deprecation warnings for optparse are expected
until Jun 2013.
* Support for {}-format strings will be added when one of the
converters is approved. This can be done at any point though, so it
shouldn't hold up inclusion of argparse.

Steve

--
PEP: 389
Title: argparse - new command line parsing module
Version: $Revision: 75674 $
Last-Modified: $Date: 2009-10-24 12:01:49 -0700 (Sat, 24 Oct 2009) $
Author: Steven Bethard steven.beth...@gmail.com
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 25-Sep-2009
Python-Version: 2.7 and 3.2
Post-History: 27-Sep-2009, 24-Oct-2009


Abstract

This PEP proposes inclusion of the argparse [1]_ module in the Python
standard library in Python 2.7 and 3.2.


Motivation
==
The argparse module is a command line parsing library which provides
more functionality than the existing command line parsing modules in
the standard library, getopt [2]_ and optparse [3]_. It includes
support for positional arguments (not just options), subcommands,
required options, options syntaxes like /f and +rgb, zero-or-more
and one-or-more style arguments, and many other features the other
two lack.

The argparse module is also already a popular third-party replacement
for these modules. It is used in projects like IPython (the Scipy
Python shell) [4]_, is included in Debian testing and unstable [5]_,
and since 2007 has had various requests for its inclusion in the
standard library [6]_ [7]_ [8]_. This popularity suggests it may be
a valuable addition to the Python libraries.


Why aren't getopt and optparse enough?
==
One argument against adding argparse is that thare are already two
different option parsing modules in the standard library [9]_. The
following is a list of features provided by argparse but not present
in getopt or optparse:

* While it is true there are two *option* parsing libraries, there
  are no full command line parsing libraries -- both getopt and
  optparse support only options and have no support for positional
  arguments. The argparse module handles both, and as a result, is
  able to generate better help messages, avoiding redundancies like
  the ``usage=`` string usually required by optparse.

* The argparse module values practicality over purity. Thus, argparse
  allows required options and customization of which characters are
  used to identify options, while optparse explicitly states the
  phrase 'required option' is self-contradictory and that the option
  syntaxes ``-pf``, ``-file``, ``+f``, ``+rgb``, ``/f`` and ``/file``
  are not supported by optparse, and they never will be.

* The argparse module allows options to accept a variable number of
  arguments using ``nargs='?'``, ``nargs='*'`` or ``nargs='+'``. The
  optparse module provides an untested recipe for some part of this
  functionality [10]_ but admits that things get hairy when you want
  an option to take a variable number of arguments.

* The argparse module supports subcommands, where a main command
  line parser dispatches to other command line parsers depending on
  the command line arguments. This is a common pattern in command
  line interfaces, e.g. ``svn co`` and ``svn up``.


Why isn't the functionality just being added to optparse?
=
Clearly all the above features offer improvements over what is
available through optparse. A reasonable question then is why these
features are not simply provided as patches to optparse, instead of
introducing an entirely new module. In fact, the original development
of argparse intended to do just that, but because of various fairly
constraining design decisions of optparse, this wasn't really
possible. Some of the problems included:

* The optparse module exposes the internals of its parsing algorithm.
  In particular, ``parser.largs`` and ``parser.rargs`` are guaranteed
  to be available to callbacks [11]_. This makes it extremely
  difficult to improve the parsing algorithm as was necessary in
  argparse for proper handling of positional arguments and variable
  length arguments. For example, ``nargs='+'`` in argparse is matched
  using regular expressions and thus has no notion of things like
  ``parser.largs``.

* The optparse extension APIs are extremely complex. For example,
  just to use a simple custom string-to-object conversion function,
  you have to subclass ``Option``, hack class attributes, and then
  specify your custom option type to the parser, like this::

class MyOption(Option):
TYPES = Option.TYPES + 

Re: [Python-Dev] updated PEP 389: argparse

2009-10-24 Thread Ben Finney
Steven Bethard steven.beth...@gmail.com writes:

 Discussion: sys.err and sys.exit
 
 There were some concerns that argparse by default always writes to
 ``sys.err``
[…]

Unless, I'm missing something, this should replace ‘sys.err’ with
‘sys.stderr’ throughout.

-- 
 \ “Pinky, are you pondering what I'm pondering?” “I think so, |
  `\Brain, but why would anyone want to see Snow White and the |
_o__)   Seven Samurai?” —_Pinky and The Brain_ |
Ben Finney

___
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


Re: [Python-Dev] updated PEP 389: argparse

2009-10-24 Thread Steven Bethard
On Sat, Oct 24, 2009 at 4:34 PM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Steven Bethard steven.beth...@gmail.com writes:

 Discussion: sys.err and sys.exit
 
 There were some concerns that argparse by default always writes to
 ``sys.err``
 […]

 Unless, I'm missing something, this should replace ‘sys.err’ with
 ‘sys.stderr’ throughout.

Yep, that's a typo. Thanks for the catch. It's fixed in SVN and the website:

http://www.python.org/dev/peps/pep-0389/

Steve
-- 
Where did you get that preposterous hypothesis?
Did Steve tell you that?
--- The Hiphopopotamus
___
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