Skip to content

Commit

Permalink
Allow specifying verbose/quite level via config file and env var
Browse files Browse the repository at this point in the history
  • Loading branch information
McSinyx committed Jul 14, 2020
1 parent 328e4c0 commit c810ee9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
4 changes: 4 additions & 0 deletions news/8578.bugfix
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 16 additions & 18 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)

0 comments on commit c810ee9

Please sign in to comment.