diff options
Diffstat (limited to 'Lib/argparse.py')
| -rw-r--r-- | Lib/argparse.py | 46 | 
1 files changed, 32 insertions, 14 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index bc2ba13..be276bb 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -490,7 +490,7 @@ class HelpFormatter(object):          action_width = help_position - self._current_indent - 2          action_header = self._format_action_invocation(action) -        # ho nelp; start on same line and add a final newline +        # no help; start on same line and add a final newline          if not action.help:              tup = self._current_indent, '', action_header              action_header = '%*s%s\n' % tup @@ -608,8 +608,7 @@ class HelpFormatter(object):              pass          else:              self._indent() -            for subaction in get_subactions(): -                yield subaction +            yield from get_subactions()              self._dedent()      def _split_lines(self, text, width): @@ -1040,7 +1039,8 @@ class _VersionAction(Action):              version = parser.version          formatter = parser._get_formatter()          formatter.add_text(version) -        parser.exit(message=formatter.format_help()) +        parser._print_message(formatter.format_help(), _sys.stdout) +        parser.exit()  class _SubParsersAction(Action): @@ -1122,7 +1122,14 @@ class _SubParsersAction(Action):          # parse all the remaining options into the namespace          # store any unrecognized options on the object, so that the top          # level parser can decide what to do with them -        namespace, arg_strings = parser.parse_known_args(arg_strings, namespace) + +        # In case this subparser defines new defaults, we parse them +        # in a new namespace object and then update the original +        # namespace for the relevant parts. +        subnamespace, arg_strings = parser.parse_known_args(arg_strings, None) +        for key, value in vars(subnamespace).items(): +            setattr(namespace, key, value) +          if arg_strings:              vars(namespace).setdefault(_UNRECOGNIZED_ARGS_ATTR, [])              getattr(namespace, _UNRECOGNIZED_ARGS_ATTR).extend(arg_strings) @@ -1143,11 +1150,17 @@ class FileType(object):              same values as the builtin open() function.          - bufsize -- The file's desired buffer size. Accepts the same values as              the builtin open() function. +        - encoding -- The file's encoding. Accepts the same values as the +            builtin open() function. +        - errors -- A string indicating how encoding and decoding errors are to +            be handled. Accepts the same value as the builtin open() function.      """ -    def __init__(self, mode='r', bufsize=-1): +    def __init__(self, mode='r', bufsize=-1, encoding=None, errors=None):          self._mode = mode          self._bufsize = bufsize +        self._encoding = encoding +        self._errors = errors      def __call__(self, string):          # the special argument "-" means sys.std{in,out} @@ -1162,14 +1175,18 @@ class FileType(object):          # all other arguments are used as file names          try: -            return open(string, self._mode, self._bufsize) -        except IOError as e: +            return open(string, self._mode, self._bufsize, self._encoding, +                        self._errors) +        except OSError as e:              message = _("can't open '%s': %s")              raise ArgumentTypeError(message % (string, e))      def __repr__(self):          args = self._mode, self._bufsize -        args_str = ', '.join(repr(arg) for arg in args if arg != -1) +        kwargs = [('encoding', self._encoding), ('errors', self._errors)] +        args_str = ', '.join([repr(arg) for arg in args if arg != -1] + +                             ['%s=%r' % (kw, arg) for kw, arg in kwargs +                              if arg is not None])          return '%s(%s)' % (type(self).__name__, args_str)  # =========================== @@ -1188,9 +1205,13 @@ class Namespace(_AttributeHolder):              setattr(self, name, kwargs[name])      def __eq__(self, other): +        if not isinstance(other, Namespace): +            return NotImplemented          return vars(self) == vars(other)      def __ne__(self, other): +        if not isinstance(other, Namespace): +            return NotImplemented          return not (self == other)      def __contains__(self, key): @@ -2003,17 +2024,14 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):              # replace arguments referencing files with the file content              else:                  try: -                    args_file = open(arg_string[1:]) -                    try: +                    with open(arg_string[1:]) as args_file:                          arg_strings = []                          for arg_line in args_file.read().splitlines():                              for arg in self.convert_arg_line_to_args(arg_line):                                  arg_strings.append(arg)                          arg_strings = self._read_args_from_files(arg_strings)                          new_arg_strings.extend(arg_strings) -                    finally: -                        args_file.close() -                except IOError: +                except OSError:                      err = _sys.exc_info()[1]                      self.error(str(err))  | 
