Skip to content

Commit 7f4673d

Browse files
authored
Merge pull request #5644 from sinscary/fix_invalid_val_error
Show appropriate error message
2 parents d77e329 + ad78315 commit 7f4673d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

news/5644.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show a better error message when a configuration option has an invalid value.

src/pip/_internal/baseparser.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,14 @@ def _update_defaults(self, defaults):
192192
continue
193193

194194
if option.action in ('store_true', 'store_false', 'count'):
195-
val = strtobool(val)
195+
try:
196+
val = strtobool(val)
197+
except ValueError:
198+
error_msg = invalid_config_error_message(
199+
option.action, key, val
200+
)
201+
self.error(error_msg)
202+
196203
elif option.action == 'append':
197204
val = val.split()
198205
val = [self.check_default(option, key, v) for v in val]
@@ -238,3 +245,16 @@ def get_default_values(self):
238245
def error(self, msg):
239246
self.print_usage(sys.stderr)
240247
self.exit(2, "%s\n" % msg)
248+
249+
250+
def invalid_config_error_message(action, key, val):
251+
"""Returns a better error message when invalid configuration option
252+
is provided."""
253+
if action in ('store_true', 'store_false'):
254+
return ("{0} is not a valid value for {1} option, "
255+
"please specify a boolean value like yes/no, "
256+
"true/false or 1/0 instead.").format(val, key)
257+
258+
return ("{0} is not a valid value for {1} option, "
259+
"please specify a numerical value like 1/0 "
260+
"instead.").format(val, key)

0 commit comments

Comments
 (0)