Repository: larq/zookeeper

Issue:
Flag syntax for boolean params
At the moment, CLI params are passed with arguments of the form `key=value`, meaning that boolean parameters are set with e.g. `use_bias=True` or `use_bias=False`.

It would be nice if we could have the option of passing boolean arguments with `--use-bias` or `--no-use-bias` to set that parameter to `True` and `False` respectively.

Discussion:

Patch:
diff --git a/zookeeper/core/cli.py b/zookeeper/core/cli.py
index 5769b13..f2ab714 100644
--- a/zookeeper/core/cli.py
+++ b/zookeeper/core/cli.py
@@ -7,6 +7,14 @@ from zookeeper.core.utils import convert_to_snake_case, parse_value_from_string
 
 class ConfigParam(click.ParamType):
     def convert(self, str_value, param, ctx):
+        # Allow the syntax `--name_of_key` to set the boolean flag to `True`,
+        # and likewise `--no-name_of_key` to set the boolean flag to `False`.
+        if re.match("^--[\\w.]+$", str_value):
+            return str_value[2:], True
+        if re.match("^--no-[\\w.]+$", str_value):
+            return str_value[5:], False
+
+        # Otherwise, expect the syntax `name_of_key=value`.
         try:
             key, value = str_value.split("=")
             # Make sure the key is alpha-numeric (possibly with full stops).
diff --git a/zookeeper/core/task.py b/zookeeper/core/task.py
index daf090b..cd1c35f 100644
--- a/zookeeper/core/task.py
+++ b/zookeeper/core/task.py
@@ -44,9 +44,9 @@ def task(cls):
             "the @task decorator is applied to."
         )
 
-    @cli.command(cls.__name__)
-    @click.argument("config", type=ConfigParam(), nargs=-1)
+    @cli.command(cls.__name__, context_settings=dict(ignore_unknown_options=True))
     @click.option("-i", "--interactive", is_flag=True, default=False)
+    @click.argument("config", type=ConfigParam(), nargs=-1)
     def command(config, interactive):
         config = {k: v for k, v in config}
         task_instance = cls()


Test patch:
diff --git a/zookeeper/core/cli_test.py b/zookeeper/core/cli_test.py
index a5ff3d8..b365162 100644
--- a/zookeeper/core/cli_test.py
+++ b/zookeeper/core/cli_test.py
@@ -8,9 +8,10 @@ from zookeeper.core.task import task
 class TestTask:
     a: int
     b: str = "foo"
+    c: bool = False
 
     def run(self):
-        print(self.a, self.b)
+        print(self.a, self.b, self.c)
 
 
 runner = testing.CliRunner(mix_stderr=False)
@@ -30,7 +31,7 @@ def test_pass_param_values():
     # We should be able to pass parameter values through the CLI.
     result = runner.invoke(cli, ["test_task", "a=5"])
     assert result.exit_code == 0
-    assert result.output == "5 foo\n"
+    assert result.output == "5 foo False\n"
 
 
 def test_param_key_valid_characters():
@@ -51,9 +52,9 @@ def test_param_key_invalid_characters():
 
 def test_override_param_values():
     # We should be able to override existing parameter values through the CLI.
-    result = runner.invoke(cli, ["test_task", "a=5", "b=bar"])
+    result = runner.invoke(cli, ["test_task", "a=5", "b=bar", "c=True"])
     assert result.exit_code == 0
-    assert result.output == "5 bar\n"
+    assert result.output == "5 bar True\n"
 
 
 def test_override_param_complex_string():
@@ -62,4 +63,14 @@ def test_override_param_complex_string():
         cli, ["test_task", "a=5", "b=https://some-path/foo/bar@somewhere"]
     )
     assert result.exit_code == 0
-    assert result.output == "5 https://some-path/foo/bar@somewhere\n"
+    assert result.output == "5 https://some-path/foo/bar@somewhere False\n"
+
+
+def test_boolean_flag_syntax():
+    # We should be able to use a shorthand for setting boolean flags.
+    result = runner.invoke(cli, ["test_task", "a=5", "--c"])
+    assert result.exit_code == 0
+    assert result.output == "5 foo True\n"
+    result = runner.invoke(cli, ["test_task", "a=5", "--no-c"])
+    assert result.exit_code == 0
+    assert result.output == "5 foo False\n"