Optional argument for boolean command-line options

This commit is contained in:
Unrud 2021-11-14 23:30:59 +01:00
parent 08e789d993
commit 98b49ac2b6
2 changed files with 8 additions and 7 deletions

View File

@ -101,14 +101,12 @@ def run() -> None:
del kwargs["type"]
opposite_args = list(kwargs.pop("opposite_aliases", ()))
opposite_args.append("--no%s" % long_name[1:])
kwargs["action"] = "store_const"
kwargs["const"] = "True"
group.add_argument(*args, **kwargs)
group.add_argument(*args, nargs="?", const="True", **kwargs)
# Opposite argument
kwargs["const"] = "False"
kwargs["help"] = "do not %s (opposite of %s)" % (
kwargs["help"], long_name)
group.add_argument(*opposite_args, **kwargs)
group.add_argument(*opposite_args, action="store_const",
const="False", **kwargs)
else:
del kwargs["type"]
group.add_argument(*args, **kwargs)

View File

@ -187,7 +187,7 @@ class TestBaseServerRequests(BaseTest):
self.thread.start()
self.get("/", check=302)
def test_command_line_interface(self) -> None:
def test_command_line_interface(self, with_bool_options=False) -> None:
self.configuration.update({"headers": {"Test-Server": "test"}})
config_args = []
for section in self.configuration.sections():
@ -197,7 +197,7 @@ class TestBaseServerRequests(BaseTest):
if option.startswith("_"):
continue
long_name = "--%s-%s" % (section, option.replace("_", "-"))
if config.DEFAULT_CONFIG_SCHEMA.get(
if with_bool_options and config.DEFAULT_CONFIG_SCHEMA.get(
section, {}).get(option, {}).get("type") == bool:
if not cast(bool, self.configuration.get(section, option)):
long_name = "--no%s" % long_name[1:]
@ -224,6 +224,9 @@ class TestBaseServerRequests(BaseTest):
if os.name == "posix":
assert p.returncode == 0
def test_command_line_interface_with_bool_options(self) -> None:
self.test_command_line_interface(with_bool_options=True)
def test_wsgi_server(self) -> None:
config_path = os.path.join(self.colpath, "config")
parser = RawConfigParser()