[issue10523] argparse has problem parsing option files containing empty rows

Caelyn McAulay report at bugs.python.org
Tue Apr 15 01:27:52 CEST 2014


Caelyn McAulay added the comment:

The current behaviour takes empty lines and interprets them as empty strings. 

The attached demonstration script shows the error occurring. The first case is a simple example to illustrate what happens in the general case. The second case shows empty lines being interpreted as empty strings and assigned to arguments.

The third case, despite being very similar to the first, results in argparse exiting with an error message. Internally, what is happening is, after taking the 'foo' and 'baz' arguments and assigning them 'bar' and 'quux' respectively, it reads in an argument "", which it does not recognize. And produces the following error message:

"argparse_example.py: error: unrecognized arguments:"

The error message, in it's current form, is kind of opaque.

For the third case, if we move the blank line to between 'bar' and '-baz', the same error results, as again it tried to interpret the blank line as an argument. If we move the blank line to the start of the file, same thing again. 

If we move the blank line between '-foo' and 'bar', instead the error reads: "argparse_example.py: error: unrecognized arguments: bar" - which is at least somewhat comprehensible.

The question is, how should blank lines be handled? 

Should they be accepted as possible values for arguments? 

If they fall into spaces where arguments (versus values for arguments) are expected, should we skip them?

If the current handling is fine, I would propose updating the documentation to add the following after the paragraph that begins "Arguments read from a file ...":

"By default, blank lines are interpreted as empty strings. An empty string is not an acceptable argument; but it is an acceptable value for an argument."

And changing the way that the error from argparse is displayed so that it is more obvious what "argparse_example.py: error: unrecognized arguments:" means.

----------
nosy: +math_foo
Added file: http://bugs.python.org/file34860/argparse_example.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10523>
_______________________________________


More information about the Python-bugs-list mailing list