From c810ee9c916b0b28db85130521441e61b4338d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Mon, 13 Jul 2020 11:39:03 +0700 Subject: [PATCH] Allow specifying verbose/quite level via config file and env var --- news/8578.bugfix | 4 ++++ src/pip/_internal/cli/parser.py | 34 ++++++++++++++++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 news/8578.bugfix diff --git a/news/8578.bugfix b/news/8578.bugfix new file mode 100644 index 00000000000..eb1d1469e67 --- /dev/null +++ b/news/8578.bugfix @@ -0,0 +1,4 @@ +Allow specifying verbosity and quite level via configuration files +and environment variables. Previously these options were treated as +boolean values when read from there while through CLI the level can be +specified. diff --git a/src/pip/_internal/cli/parser.py b/src/pip/_internal/cli/parser.py index 04e00b72132..b8c40a6c95e 100644 --- a/src/pip/_internal/cli/parser.py +++ b/src/pip/_internal/cli/parser.py @@ -11,6 +11,7 @@ import textwrap from distutils.util import strtobool +from pip._vendor.contextlib2 import suppress from pip._vendor.six import string_types from pip._internal.cli.status_codes import UNKNOWN_ERROR @@ -197,15 +198,25 @@ def _update_defaults(self, defaults): if option is None: continue - if option.action in ('store_true', 'store_false', 'count'): + if option.action in ('store_true', 'store_false'): try: val = strtobool(val) except ValueError: - error_msg = invalid_config_error_message( - option.action, key, val + self.error( + "{} is not a valid value for {} option, " + "please specify a boolean value like yes/no, " + "true/false or 1/0 instead.".format(val, key) + ) + elif option.action == 'count': + with suppress(ValueError): + val = int(strtobool(val)) + if not isinstance(val, int) or val < 0: + self.error( + "{} is not a valid value for {} option, " + "please specify either a nonnegative integer " + "or a boolean value like yes/no or false/true " + "which is equivalent to 1/0 instead.".format(val, key) ) - self.error(error_msg) - elif option.action == 'append': val = val.split() val = [self.check_default(option, key, v) for v in val] @@ -251,16 +262,3 @@ def get_default_values(self): def error(self, msg): self.print_usage(sys.stderr) self.exit(UNKNOWN_ERROR, "{}\n".format(msg)) - - -def invalid_config_error_message(action, key, val): - """Returns a better error message when invalid configuration option - is provided.""" - if action in ('store_true', 'store_false'): - return ("{0} is not a valid value for {1} option, " - "please specify a boolean value like yes/no, " - "true/false or 1/0 instead.").format(val, key) - - return ("{0} is not a valid value for {1} option, " - "please specify a numerical value like 1/0 " - "instead.").format(val, key)