This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Michal.Pomorski
Recipients Michal.Pomorski
Date 2010-11-24.21:47:39
SpamBayes Score 8.014927e-08
Marked as misclassified No
Message-id <1290635267.13.0.585990858581.issue10523@psf.upfronthosting.co.za>
In-reply-to
Content
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:
                new_arg_strings.append(arg_string)

            # replace arguments referencing files with the file content
            else:
                try:
                    args_file = open(arg_string[1:])
                    try:
                        arg_strings = args_file.read().strip().split()
                        arg_strings = self._read_args_from_files(arg_strings)
                        
                        new_arg_strings.extend(arg_strings)
                    finally:
                        args_file.close()
                except IOError:
                    err = _sys.exc_info()[1]
                    self.error(str(err))

        # return the modified argument list
        return new_arg_strings
History
Date User Action Args
2010-11-24 21:47:47Michal.Pomorskisetrecipients: + Michal.Pomorski
2010-11-24 21:47:47Michal.Pomorskisetmessageid: <1290635267.13.0.585990858581.issue10523@psf.upfronthosting.co.za>
2010-11-24 21:47:39Michal.Pomorskilinkissue10523 messages
2010-11-24 21:47:39Michal.Pomorskicreate