[New-bugs-announce] [issue10523] argparse has problem parsing option files containing empty rows

Michal Pomorski report at bugs.python.org
Wed Nov 24 22:47:47 CET 2010

New submission from Michal Pomorski <misieck at gmail.com>:

When using the argument file option, i.e  @file_with_arguments the following problems arise:

1. argparse crashes when the file contains an empty line (even if it is the last line) - arg_string[0] is done when arg_string is empty. 
    This is caused by the use of splitlines() instead of strip().split() in the function _read_args_from_files(self, arg_strings)

2. options separated by spaces in one row are passed as a single long option, meaning each option has to be on its own line.
    This is caused by the new function 
   def convert_arg_line_to_args(self, arg_line):
        return [arg_line]
   which should be 
        return arg_line.split()

Both problems are caused by a modification in
    def _read_args_from_files(self, arg_strings)
The version from argparse 1.0.1 worked well and was correct, it should be sufficient to reverse the changes done from 1.0.1 to 1.1.

Here is the old implementation:

    def _read_args_from_files(self, arg_strings):
        # expand arguments referencing files
        new_arg_strings = []
        for arg_string in arg_strings:

            # for regular arguments, just add them back into the list
            if arg_string[0] not in self.fromfile_prefix_chars:

            # replace arguments referencing files with the file content
                    args_file = open(arg_string[1:])
                        arg_strings = args_file.read().strip().split()
                        arg_strings = self._read_args_from_files(arg_strings)
                except IOError:
                    err = _sys.exc_info()[1]

        # return the modified argument list
        return new_arg_strings

components: Library (Lib)
messages: 122314
nosy: Michal.Pomorski
priority: normal
severity: normal
status: open
title: argparse has problem parsing option files containing empty rows
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list