argparse

26 views
Skip to first unread message

Pavel Zlatovratsky

unread,
Nov 10, 2013, 1:01:13 PM11/10/13
to pypng...@googlegroups.com
Module 'optparse' used to parse CLI options is marked as deprecated since python 2.7 and replaced with 'argparse' module.
'Argparse' is also available as separate module for older versions of python https://pypi.python.org/pypi/argparse/1.2.1

I do not intend replace optparse with argparse right now. But I think it will be nice to describe argparse as reqirement and use it at least in new utils.

David Jones

unread,
Nov 11, 2013, 3:44:29 AM11/11/13
to pypng...@googlegroups.com
On 10 November 2013 18:01, Pavel Zlatovratsky
<pavelzla...@gmail.com> wrote:
> Module 'optparse' used to parse CLI options is marked as deprecated since
> python 2.7 and replaced with 'argparse' module.
> 'Argparse' is also available as separate module for older versions of python
> https://pypi.python.org/pypi/argparse/1.2.1

That's good to know.

> I do not intend replace optparse with argparse right now. But I think it
> will be nice to describe argparse as reqirement and use it at least in new
> utils.

I've never really seen the point of optparse: It is clumsy and doesn't
provide much more benefit than getopt. The same seems to be true of
its replacement argparse.

If I was replacing the option parsing code I'd probably replace it
with getopt or a hand-coded parser.

Option parsing has always been in parts of pypng that I'd regarded as
somewhat optional (or at least non-core) which means that it may be
more acceptable to restrict to higher Python versions or use extra
packages. If I was going to use a 3rd party module for option parsing
it would today's favourite: docopt (which by the way is really fun and
I highly recommend it).

drj

Pavel Zlatovratsky

unread,
Nov 11, 2013, 5:37:00 AM11/11/13
to pypng...@googlegroups.com
I've never really seen the point of optparse: It is clumsy and doesn't
provide much more benefit than getopt. The same seems to be true of
its replacement argparse.

If I was replacing the option parsing code I'd probably replace it
with getopt or a hand-coded parser.

 There are two things about argparse that take my attention (against optparse):
1) positional arguments
2) strict types incl. file type. (and file type works with '-' as stdin/stdout)

I hope these features can help against manual (and platform-specific) configuration like this:
https://github.com/drj11/pypng/blob/master/code/png.py#L2682

If I was going to use a 3rd party module for option parsing
it would today's favourite: docopt (which by the way is really fun and
I highly recommend it).
drj
 
docopt is nice, yes. But it's less compatible than argparse (docopt is tested with Python 2.5, 2.6, 2.7, 3.1, 3.2.) and I miss file-type argument.

David

unread,
Apr 13, 2023, 7:23:21 AM4/13/23
to PyPNG Users
On Monday, 11 November 2013 at 08:44:29 UTC David wrote:

If I was replacing the option parsing code I'd probably replace it
with getopt or a hand-coded parser.

 
10 years on, and the command line tools in PyPNG do in fact mostly use argparse (and also, the command line functionality that was in png.py itself has been removed to priforgepng).

That's mostly due to argparse now being a built-in module, but also the improved documentation and blogs.

Cheers,
 drj
Reply all
Reply to author
Forward
0 new messages