Update to the latest version of argparse (#6786)

* Update to the latest version of argparse

* Re-add colified logic
This commit is contained in:
Adam J. Stewart 2017-12-30 19:08:35 -05:00 committed by Todd Gamblin
parent 010334bf22
commit 088c193a78

View file

@ -1,25 +1,6 @@
# argparse is (c) 2006-2009 Steven J. Bethard <steven.bethard@gmail.com>.
#
# The argparse module was contributed to Python as of Python 2.7 and thus
# was licensed under the Python license. Same license applies to all files in
# the argparse package project.
#
# For details about the Python License, please see doc/Python-License.txt.
#
# History
# -------
#
# Before (and including) argparse 1.1, the argparse package was licensed under
# Apache License v2.0.
#
# After argparse 1.1, all project files from the argparse project were deleted
# due to license compatibility issues between Apache License 2.0 and GNU GPL v2.
#
# The project repository then had a clean start with some files taken from
# Python 2.7.1, so definitely all files are under Python License now.
#
# Author: Steven J. Bethard <steven.bethard@gmail.com>. # Author: Steven J. Bethard <steven.bethard@gmail.com>.
# # Maintainer: Thomas Waldmann <tw@waldmann-edv.de>
"""Command-line parsing library """Command-line parsing library
This module is an optparse-inspired command-line parsing library that: This module is an optparse-inspired command-line parsing library that:
@ -81,7 +62,12 @@
still considered an implementation detail.) still considered an implementation detail.)
""" """
__version__ = '1.2.1' __version__ = '1.4.0' # we use our own version number independant of the
# one in stdlib and we release this on pypi.
__external_lib__ = True # to make sure the tests really test THIS lib,
# not the builtin one in Python stdlib
__all__ = [ __all__ = [
'ArgumentParser', 'ArgumentParser',
'ArgumentError', 'ArgumentError',
@ -1073,7 +1059,7 @@ def __init__(self, name, aliases, help):
metavar += ' (%s)' % ', '.join(aliases) metavar += ' (%s)' % ', '.join(aliases)
sup = super(_SubParsersAction._ChoicesPseudoAction, self) sup = super(_SubParsersAction._ChoicesPseudoAction, self)
sup.__init__(option_strings=[], dest=dest, help=help, sup.__init__(option_strings=[], dest=dest, help=help,
metavar=metavar) metavar=metavar)
def __init__(self, def __init__(self,
option_strings, option_strings,
@ -1134,9 +1120,8 @@ def __call__(self, parser, namespace, values, option_string=None):
try: try:
parser = self._name_parser_map[parser_name] parser = self._name_parser_map[parser_name]
except KeyError: except KeyError:
args = {'parser_name': parser_name, tup = parser_name, ', '.join(self._name_parser_map)
'choices': ', '.join(self._name_parser_map)} msg = _('unknown parser %r (choices: %s)' % tup)
msg = _('unknown parser %(parser_name)r (choices: %(choices)s)') % args
raise ArgumentError(self, msg) raise ArgumentError(self, msg)
# parse all the remaining options into the namespace # parse all the remaining options into the namespace
@ -1180,11 +1165,16 @@ def __call__(self, string):
msg = _('argument "-" with mode %r' % self._mode) msg = _('argument "-" with mode %r' % self._mode)
raise ValueError(msg) raise ValueError(msg)
# all other arguments are used as file names try:
if self._bufsize: # all other arguments are used as file names
return open(string, self._mode, self._bufsize) if self._bufsize:
else: return open(string, self._mode, self._bufsize)
return open(string, self._mode) else:
return open(string, self._mode)
except IOError:
err = _sys.exc_info()[1]
message = _("can't open '%s': %s")
raise ArgumentTypeError(message % (string, err))
def __repr__(self): def __repr__(self):
args = [self._mode, self._bufsize] args = [self._mode, self._bufsize]
@ -1720,21 +1710,6 @@ def _add_action(self, action):
self._positionals._add_action(action) self._positionals._add_action(action)
return action return action
def get_subparser(self, name):
"""Gets a subparser added with the supplied name.
This is an extension to the standard argparse API.
"""
subpasrsers_actions = [
action for action in self._actions
if isinstance(action, _SubParsersAction)]
for action in subpasrsers_actions:
for choice, subparser in action.choices.items():
if choice == name:
return subparser
return None
def _get_optional_actions(self): def _get_optional_actions(self):
return [action return [action
for action in self._actions for action in self._actions
@ -1769,10 +1744,7 @@ def parse_known_args(self, args=None, namespace=None):
if action.dest is not SUPPRESS: if action.dest is not SUPPRESS:
if not hasattr(namespace, action.dest): if not hasattr(namespace, action.dest):
if action.default is not SUPPRESS: if action.default is not SUPPRESS:
default = action.default setattr(namespace, action.dest, action.default)
if isinstance(action.default, basestring):
default = self._get_value(action, default)
setattr(namespace, action.dest, default)
# add any parser defaults that aren't present # add any parser defaults that aren't present
for dest in self._defaults: for dest in self._defaults:
@ -2000,12 +1972,23 @@ def consume_positionals(start_index):
if positionals: if positionals:
self.error(_('too few arguments')) self.error(_('too few arguments'))
# make sure all required actions were present # make sure all required actions were present, and convert defaults.
for action in self._actions: for action in self._actions:
if action.required: if action not in seen_actions:
if action not in seen_actions: if action.required:
name = _get_action_name(action) name = _get_action_name(action)
self.error(_('argument %s is required') % name) self.error(_('argument %s is required') % name)
else:
# Convert action default now instead of doing it before
# parsing arguments to avoid calling convert functions
# twice (which may fail) if the argument was given, but
# only if it was defined already in the namespace
if (action.default is not None and
isinstance(action.default, basestring) and
hasattr(namespace, action.dest) and
action.default is getattr(namespace, action.dest)):
setattr(namespace, action.dest,
self._get_value(action, action.default))
# make sure all required groups had one option present # make sure all required groups had one option present
for group in self._mutually_exclusive_groups: for group in self._mutually_exclusive_groups: