473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

When argparse will be in the python standard installation

Hi,

I feel argparse has some useful things that optparse doesn't have. But
I can't find it argparse in python library reference. I'm wondering
when it will be available in the python standard installation.

Thanks,
Peng

Jan 3 '07 #1
9 2997
Pe*******@gmail.com wrote:
I feel argparse has some useful things that optparse doesn't have. But
I can't find it argparse in python library reference. I'm wondering
when it will be available in the python standard installation.
there's already two different option parsing modules in the standard
library. maybe improving one of the existing ones might be a better
idea than adding a third one?

</F>

Jan 3 '07 #2
Pe*******@gmail.com schrieb:
I feel argparse has some useful things that optparse doesn't have. But
I can't find it argparse in python library reference. I'm wondering
when it will be available in the python standard installation.
On its own, never. Somebody has to contribute it to Python, and that
somebody has to be the author.

However, it would be better if missing features where provided in
existing libraries, rather than introducing new libraries to replace
the existing ones (i.e. I would likely reject a patch to add yet another
argument parsing library - we already have getopt and optparse).

Regards,
Martin
Jan 3 '07 #3
Martin v. Löwis wrote:
Pe*******@gmail.com schrieb:
>I feel argparse has some useful things that optparse doesn't have. But
I can't find it argparse in python library reference. I'm wondering
when it will be available in the python standard installation.

On its own, never. Somebody has to contribute it to Python, and that
somebody has to be the author.
As the author of argparse, I'm happy to contribute it, though I've been
thinking that it would be more appropriate for the Python 3.0 line than
the Python 2.X line.
However, it would be better if missing features where provided in
existing libraries, rather than introducing new libraries to replace
the existing ones (i.e. I would likely reject a patch to add yet another
argument parsing library - we already have getopt and optparse).
That was my original intention, and argparse started out using the
optparse code. But optparse is quite difficult to extend, and in the
end, when I found that I had monkey-patched or rewritten just about
everything in optparse, I decided to drop the optparse code entirely.

If someone has an idea how to include argparse features into optparse,
I'm certainly all for it. But I tried and failed to do this myself, so I
don't know how to go about it.

STeVe
Jan 3 '07 #4
Steven Bethard schrieb:
If someone has an idea how to include argparse features into optparse,
I'm certainly all for it. But I tried and failed to do this myself, so I
don't know how to go about it.
It's not necessary that the implementation is retained, only that the
interface is preserved. So if you can come up with an implementation
that supports all optparse applications, and adds the additional
features, your implementation could replace the current optparse.
If you need to make incompatible changes, it would be best if you
could produce a list of such changes, so that optparse users can
review them to find out whether they are affected.

Regards,
Martin
Jan 4 '07 #5
Martin v. Löwis wrote:
Steven Bethard schrieb:
>If someone has an idea how to include argparse features into optparse,
I'm certainly all for it. But I tried and failed to do this myself, so I
don't know how to go about it.

It's not necessary that the implementation is retained, only that the
interface is preserved. So if you can come up with an implementation
that supports all optparse applications, and adds the additional
features, your implementation could replace the current optparse.
If you need to make incompatible changes, it would be best if you
could produce a list of such changes, so that optparse users can
review them to find out whether they are affected.
FWIW, here's a short list of issues that could be easily addressed:

* alias ArgumentParser to OptionParser
* alias add_argument to add_option
* alias Values to Namespace
* alias OptionError and OptionValueError to ArgumentError
* alias add_help= keyword argument of ArgumentParser to add_help_option=
* alias namespace= keyword argument of parse_args to options=
* add the has_option, get_option and remove_option methods
* add the set_conflict_handler method
* add the destroy method
* add the set_usage method
* add the string names for types (e.g. "string", "str", "int", etc.) as
aliases to the type objects (argparse already has a type registry for
exactly these kinds of things)
Some slightly harder issues:

* ArgumentParser.parse_args returns a single namespace object, not an
(options, args) tuple, since argparse handles positional arguments.
This could probably be addressed by adding an __iter__ method to the
Namespace object which would return (self, [])

* argparse uses standard string formatting specifiers, e.g. %(default)s
and %(prog)s instead of optparse's %default and %prog. It could
probably be hacked to support both though.

* argparse doesn't support {enable,disable}_interspersed_args() because
their primary use case was for handling sub-parsers, which argparse
handles through the add_subparsers method. It could probably be hacked
to work though I guess.
And the issues that I don't see any good way to address:

* argparse makes the default value for a "store_true" action False, and
the default value for a "store_false" action True. This is what users
expect, but different from optparse where the default is always None.

* the ArgumentParser constructor doesn't accept the option_list=,
option_class= or formatter= keyword arguments. Since argparse uses
Action subclasses instead of a single Option class, the former two
don't make much sense. And formatter= has been replaced with
formatter_class= where the API of the formatter was dramatically
changed. (That said, the formatter API is undocumented in both
optparse and argparse.)

* the choices argument is now checked *after* arguments have been
type-converted. This is intentional, so that you can specify, say
xrange(100) instead of ["0", "1", "2", "3", ... "99"]. There is also
no "choices" type anymore since any action can also specify their
choices.

* argparse doesn't support ``callback`` actions because the same effects
are better handled by defining a simple type-checking function and
passing it as the type= keyword argument, or by subclassing
argparse.Action and passing this as the action= keyword argument.
I could probably add callback actions by creating an appropriate
Action subclass and registering it. However, any code relying on
parser.{largs,rargs,values} would break because the parsing algorithm
is completely different in argparse.

* The `Extending optparse`_ section in the docs is pretty much
completely inapplicable. One of the major goals of argparse was to get
rid of the need to hack class attributes like TYPES, STORE_ACTIONS,
etc. Instead, these things are handled by defining simple
string-to-object functions that are passed to type= or by defining
appropriate subclasses of argparse.Action which are passed to action=.
Trying to support these sorts of things would be nearly impossible.
I guess I don't know how to proceed from here. I'm reluctant to start
adding the code necessary to support even the easily solved issues when
the issues that I don't know how to solve seem like they could be deal
breakers.

STeVe

... _Extending optparse:
http://docs.python.org/lib/optparse-...-optparse.html
Jan 4 '07 #6
Steven Bethard <st************@gmail.comwrites:
Martin v. Löwis wrote:
Steven Bethard schrieb:
If someone has an idea how to include argparse features into optparse,
I'm certainly all for it. But I tried and failed to do this myself, so I
don't know how to go about it.
It's not necessary that the implementation is retained, only that the
interface is preserved. So if you can come up with an implementation
that supports all optparse applications, and adds the additional
features, your implementation could replace the current optparse.
If you need to make incompatible changes, it would be best if you
could produce a list of such changes, so that optparse users can
review them to find out whether they are affected.

FWIW, here's a short list of issues that could be easily addressed:

* alias ArgumentParser to OptionParser
[...snip long list of proposed aliases and additions...]
Some slightly harder issues:
[...]

You propose here to attempt to merge optparse and argparse into a
single interface (single class, by the sound of it).

I stopped reading after a bit, but at least up to that point, all of
these issues could (and certainly should, if argparse is added to the
stdlib, even in Python 3) be addressed by instead providing both
OptionParser and an OptsAndArgsParser class (or whatever you'd call
the latter). OptionParser would then support exactly the current
optparse.OptionParser interface. OptsAndArgsProcessor would support
your new interface. Presumably most of the actual implementation code
would be shared.
John
Jan 4 '07 #7
Steven Bethard schrieb:
If someone has an idea how to include argparse features into optparse,
I'm certainly all for it. But I tried and failed to do this myself, so
I don't know how to go about it.
Martin v. Löwis wrote:
It's not necessary that the implementation is retained, only that the
interface is preserved.
[snip]
If you need to make incompatible changes, it would be best if you
could produce a list of such changes
Steven Bethard writes:
FWIW, here's a short list of issues that could be easily addressed:

* alias ArgumentParser to OptionParser
[snip]

John J. Lee wrote:
all of these issues could ... be addressed by instead providing both
OptionParser and an OptsAndArgsParser class (or whatever you'd call
the latter).
You're essentially proposing to move argparse.ArgumentParser to the
optparse module. (The ArgumentParser class handles both optional and
positional command-line args -- it *is* your OptsAndArgsProcessor).

That's certainly an easy option for me =) but does it address Martin and
Fredrik's concerns that there are already too many argument parsing
libraries in the stdlib?
Presumably most of the actual implementation code
would be shared.
Well, the "harder issues" I listed in the previous post were basically
the reasons that sharing the implementation code would be difficult. I
don't know how to do it without breaking OptionParser in backwards
incompatible ways. (The documented ways of extending optparse make this
particularly difficult since they make public a number of internal
optparse details.)

STeVe
Jan 4 '07 #8
Steven Bethard schrieb:
* alias ArgumentParser to OptionParser
* alias add_argument to add_option
* alias Values to Namespace
* alias OptionError and OptionValueError to ArgumentError
* alias add_help= keyword argument of ArgumentParser to add_help_option=
* alias namespace= keyword argument of parse_args to options=
Not sure what you mean by "alias": I don't think the argparse names
should be retained (unless you feel they describe things better).
You don't need to aim for argparse compatibility: users of argparse
can continue to use the current (or future) version of argparse as
a separate module.
* ArgumentParser.parse_args returns a single namespace object, not an
(options, args) tuple, since argparse handles positional arguments.
This could probably be addressed by adding an __iter__ method to the
Namespace object which would return (self, [])
I find that I'm not that familiar with command line arguments :-)

To understand that difference, I would have to see examples of both,
and why it is better to return a single result.

In any case, if the argparse interface is more powerful, and the
old interface cannot be emulated transparently, a new interface could
be added, with the old one deprecated and phased out (preferably with
some conversion guideline). It would be possible to drop the old one
in Py3k, I guess.
* argparse uses standard string formatting specifiers, e.g. %(default)s
and %(prog)s instead of optparse's %default and %prog. It could
probably be hacked to support both though.
That would be good.
* argparse makes the default value for a "store_true" action False, and
the default value for a "store_false" action True. This is what users
expect, but different from optparse where the default is always None.
Could we deprecate "store_false" with no default? It may be that users
expect to see True and False, but how can you find out whether the
option was given or not afterwards?

Alternatively, could the actions become keyword arguments (with the
action argument only preserved for backwards compatibility)? E.g.

parser.add_option("-v", store_true_into="verbose")
parser.add_option("-q", store_false_into="verbose")
(same for store, append, count, callback)
* the choices argument is now checked *after* arguments have been
type-converted. This is intentional, so that you can specify, say
xrange(100) instead of ["0", "1", "2", "3", ... "99"]. There is also
no "choices" type anymore since any action can also specify their
choices.
As an incompatible change, this could be warned-about if the type is not
string, yet the first choice is.

Alternatively, we could require users that pass converted values to
specify converted_choices=True in 2.6, and warn all users that don't
pass a converted_choices flag, and then flip the default in 2.7 and 3.0.
Then current users had a quick fix to silence the warning, and new
users could drop the extra flag in the future.
I could probably add callback actions by creating an appropriate
Action subclass and registering it. However, any code relying on
parser.{largs,rargs,values} would break because the parsing algorithm
is completely different in argparse.
If you can find a way to make callbacks work in the "common case",
this flag could be deprecated and removed.
I guess I don't know how to proceed from here. I'm reluctant to start
adding the code necessary to support even the easily solved issues when
the issues that I don't know how to solve seem like they could be deal
breakers.
This asks for a PEP. The views above are mine only, others may feel
differently.

Regards,
Martin
Jan 5 '07 #9
[Thanks for looking through all these Martin!]

Martin v. Löwis wrote:
Steven Bethard schrieb:
>* alias ArgumentParser to OptionParser
* alias add_argument to add_option
* alias Values to Namespace
* alias OptionError and OptionValueError to ArgumentError
* alias add_help= keyword argument of ArgumentParser to add_help_option=
* alias namespace= keyword argument of parse_args to options=

Not sure what you mean by "alias": I don't think the argparse names
should be retained (unless you feel they describe things better).
They do describe things a bit better. Given a command line like::

prog --opt1 o --opt2 pos1 pos2 pos3

optparse can only parse out the optional command line arguments,
``--opt1 o --opt2``, hence it uses names like ``add_option``. On the
other hand, argparse can parse the entire command line (including the
positional arguments ``pos1 pos2 pos3``) so it uses ``add_argument``
instead of ``add_option``.
>* ArgumentParser.parse_args returns a single namespace object, not an
(options, args) tuple, since argparse handles positional arguments.
This could probably be addressed by adding an __iter__ method to the
Namespace object which would return (self, [])

I find that I'm not that familiar with command line arguments :-)

To understand that difference, I would have to see examples of both,
and why it is better to return a single result.
A brief example to illustrate the differences. Here is a program that
reads the command-line args and writes the sum to a log file specified
by --log. With optparse, this would look something like::

parser = optparse.OptionParser()
parser.add_option('--log')
options, args = parser.parse_args()
if options.log is not None:
logfile = open(options.log)
else:
logfile = sys.stdout
try:
integers = [int(arg) for arg in args]
except ValueError:
parser.error('positional arguments must be integers')
logfile.write('%s\n' % sum(integers))
logfile.close()

Since argparse handles positional arguments as well as optional
arguments (and since it accepts type='outfile') the argparse version is
much more concise::

parser = argparse.ArgumentParser()
parser.add_argument('integers', type=int, nargs='+')
parser.add_argument('--log', type='outfile', default=sys.stdout)
args = parser.parse_args()
args.log.write('%s\n' % sum(args.integers))
args.log.close()

So basically, since argparse parses the entire command line (instead of
just the optional parts) there is no concept of "remaining command line
arguments" -- *all* command line arguments have become attributes of the
namespace returned by parse_args(). Thus, while optparse has to return
both a namespace and the unparsed portion of the command line, argparse
only has to return the namespace.
>* argparse makes the default value for a "store_true" action False, and
the default value for a "store_false" action True. This is what users
expect, but different from optparse where the default is always None.

Could we deprecate "store_false" with no default?
That's probably reasonable. In Python 3.0, the default could then
become the appropriate one.
It may be that users expect to see True and False, but how can you find
out whether the option was given or not afterwards?
For both optparse and argparse, with "store_false", you know the option
was not given if the stored value is something other than ``False``, and
with "store_true", you know the option was not given if the stored value
is something other than ``True``.

With optparse, that "something other" is always ``None`` which is
unfortunate because it makes the following code fail::

parser = optparse.OptionParser()
parser.add_option('--not-foo', action='store_false')
options, args = parser.parse_args()
if options.not_foo:
# do something

The ``do something`` block will execute every time because
``options.not_foo`` is either ``False`` or ``None``, both of which
indicate that the if-statement should not be executed.

In argparse, the code above works fine because the default is ``True``
instead of ``None``.
Alternatively, could the actions become keyword arguments (with the
action argument only preserved for backwards compatibility)? E.g.

parser.add_option("-v", store_true_into="verbose")
parser.add_option("-q", store_false_into="verbose")
(same for store, append, count, callback)
It looks like you're trying to merge the ``dest=`` and ``action=``
functionality, but I'm not sure I understand where this is going...
>* the choices argument is now checked *after* arguments have been
type-converted. This is intentional, so that you can specify, say
xrange(100) instead of ["0", "1", "2", "3", ... "99"]. There is also
no "choices" type anymore since any action can also specify their
choices.

As an incompatible change, this could be warned-about if the type is not
string, yet the first choice is.

Alternatively, we could require users that pass converted values to
specify converted_choices=True in 2.6, and warn all users that don't
pass a converted_choices flag, and then flip the default in 2.7 and 3.0.
Then current users had a quick fix to silence the warning, and new
users could drop the extra flag in the future.
That seems reasonable.
> I could probably add callback actions by creating an appropriate
Action subclass and registering it. However, any code relying on
parser.{largs,rargs,values} would break because the parsing algorithm
is completely different in argparse.

If you can find a way to make callbacks work in the "common case",
this flag could be deprecated and removed.
Sounds good.
>I guess I don't know how to proceed from here. I'm reluctant to start
adding the code necessary to support even the easily solved issues when
the issues that I don't know how to solve seem like they could be deal
breakers.

This asks for a PEP. The views above are mine only, others may feel
differently.
Ok. I'll try to write a PEP up this week.

Thanks again for all your useful feedback!

STeVe
Jan 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Pjer Holton | last post by:
If I were to build a Windows application that is a true standard Windows application in every conceivable way and that adheres to the MS Windows standards as much as possible (installation, GUI,...
1
by: Holger Joukl | last post by:
Hi there, 2 questions regarding build/installation issues: 1. In the python 2.3.3 setup.py script, the detect_modules method of class PyBuildExt contains the following code: 253 if...
2
by: gry | last post by:
Under cygwin, the python executable is installed as python2.4.exe with a symbolic link to python.exe. This is fine as long as one is operating only withing the cygwin world. But I execute...
6
by: Engineer | last post by:
I'm looking for a Python interpreter written in BASIC, preferably Visual Basic, and one written in VBA would be best of all. An alternative would be a Python-2-Basic compiler. Unfortunately I...
9
by: Andy Leszczynski | last post by:
Hi, I run Mandrake 10.0 with python 2.3 installed by default. I want to keep it as it is but need another, very customized Python installation based of 2.3 as well. I would prefer to have it the...
0
by: Terry Tang | last post by:
Hi There, We are extending Python interpreter to support special functions of our tools. What we did is to compile Python's source code (which is got from the an installation on a Linux...
1
by: Daniel Nogradi | last post by:
It might be slightly off topic here but couldn't find a more suitable place for this question: I'm trying to build the python binding for ming -- http://ming.sf.net -- but for some reason the...
9
by: eric | last post by:
Hi all, I want to setup simple python web server and I want it to just unzip and run, without any installation steps (have no right to do it). I've tried to write by myself, however, I find I...
13
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.