Просмотр исходного кода

Optional argument for boolean command-line options

Unrud 4 лет назад
Родитель
Сommit
98b49ac2b6
2 измененных файлов с 8 добавлено и 7 удалено
  1. 3 5
      radicale/__main__.py
  2. 5 2
      radicale/tests/test_server.py

+ 3 - 5
radicale/__main__.py

@@ -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)

+ 5 - 2
radicale/tests/test_server.py

@@ -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()