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"] del kwargs["type"]
opposite_args = list(kwargs.pop("opposite_aliases", ())) opposite_args = list(kwargs.pop("opposite_aliases", ()))
opposite_args.append("--no%s" % long_name[1:]) opposite_args.append("--no%s" % long_name[1:])
kwargs["action"] = "store_const" group.add_argument(*args, nargs="?", const="True", **kwargs)
kwargs["const"] = "True"
group.add_argument(*args, **kwargs)
# Opposite argument # Opposite argument
kwargs["const"] = "False"
kwargs["help"] = "do not %s (opposite of %s)" % ( kwargs["help"] = "do not %s (opposite of %s)" % (
kwargs["help"], long_name) kwargs["help"], long_name)
group.add_argument(*opposite_args, **kwargs) group.add_argument(*opposite_args, action="store_const",
const="False", **kwargs)
else: else:
del kwargs["type"] del kwargs["type"]
group.add_argument(*args, **kwargs) group.add_argument(*args, **kwargs)

View File

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