Support --opt=value format in CLI config overrides

This commit is contained in:
Ines Montani 2020-07-28 13:43:15 +02:00
parent ae4d8a6ffd
commit 06a97a8766
2 changed files with 11 additions and 4 deletions

View File

@ -68,10 +68,13 @@ def parse_config_overrides(args: List[str]) -> Dict[str, Any]:
opt = opt.replace("--", "").replace("-", "_")
if "." not in opt:
msg.fail(f"{err}: can't override top-level section", exits=1)
if not args or args[0].startswith("--"): # flag with no value
value = "true"
if "=" in opt: # we have --opt=value
opt, value = opt.split("=", 1)
else:
value = args.pop(0)
if not args or args[0].startswith("--"): # flag with no value
value = "true"
else:
value = args.pop(0)
# Just like we do in the config, we're calling json.loads on the
# values. But since they come from the CLI, it'd be unintuitive to
# explicitly mark strings with escaped quotes. So we're working

View File

@ -298,9 +298,13 @@ def test_project_config_validation2(config, n_errors):
[
# fmt: off
(["--x.foo", "10"], {"x.foo": 10}),
(["--x.foo=10"], {"x.foo": 10}),
(["--x.foo", "bar"], {"x.foo": "bar"}),
(["--x.foo=bar"], {"x.foo": "bar"}),
(["--x.foo", "--x.bar", "baz"], {"x.foo": True, "x.bar": "baz"}),
(["--x.foo", "10.1", "--x.bar", "--x.baz", "false"], {"x.foo": 10.1, "x.bar": True, "x.baz": False})
(["--x.foo", "--x.bar=baz"], {"x.foo": True, "x.bar": "baz"}),
(["--x.foo", "10.1", "--x.bar", "--x.baz", "false"], {"x.foo": 10.1, "x.bar": True, "x.baz": False}),
(["--x.foo", "10.1", "--x.bar", "--x.baz=false"], {"x.foo": 10.1, "x.bar": True, "x.baz": False})
# fmt: on
],
)