Skip to content

Commit

Permalink
refactor(cli): use wrapper callback instead of spreading return
Browse files Browse the repository at this point in the history
  • Loading branch information
loqusion committed May 6, 2024
1 parent b169d08 commit b29e1da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 43 deletions.
8 changes: 2 additions & 6 deletions src/hyprshade/cli/toggle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
ContextObject,
MergedVarOption,
ShaderParamType,
optional_param,
optional_argument,
variables_option,
)


@click.command(short_help="Toggle screen shader")
@click.argument(
"shader",
type=ShaderParamType(),
**optional_param("SHADER"),
)
@optional_argument("shader", type=ShaderParamType(), metavar="SHADER")
@click.option(
"--fallback",
metavar="SHADER",
Expand Down
32 changes: 19 additions & 13 deletions src/hyprshade/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,28 @@ def validate_optional_param(
return None if len(value) == 0 else value[0]


def optional_param(
metavar: str | None = None, *, callback: Callable | None = None
) -> dict[str, Any]:
def optional_argument(
*param_decls: str,
cls: type[click.Argument] | None = None,
metavar: str,
**attrs: Any,
) -> Callable[[FC], FC]:
def merged_callback(
ctx: click.Context, param: click.Argument, value: tuple[T, ...]
):
value2 = validate_optional_param(ctx, param, value)
if callback is not None:
return callback(ctx, param, value2)
return value2

return {
"metavar": metavar,
"nargs": -1,
"callback": merged_callback,
}
optional_value = validate_optional_param(ctx, param, value)
if (callback := attrs.get("callback")) is not None: # pragma: no cover
return callback(ctx, param, optional_value)
return optional_value

return click.argument(
*param_decls,
cls=cls,
metavar=metavar,
**attrs,
nargs=-1,
callback=merged_callback,
)


class ShaderParamType(click.ParamType):
Expand Down
24 changes: 0 additions & 24 deletions tests/cli/utils/test_click_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,3 @@ def test_single(self):
def test_rejects_multiple(self):
with pytest.raises(click.UsageError):
utils.validate_optional_param(None, None, ("foo", "bar"))


class TestOptionalParam:
def test_basic(self):
o = utils.optional_param("FOO")
assert o["metavar"] == "FOO"
assert o["nargs"] == -1
assert o["callback"] is not None

def test_callback(self):
o = utils.optional_param("FOO")
callback = o["callback"]
assert callback(None, None, ("foo",)) == "foo"
assert callback(None, None, ()) is None

def test_given_callback(self):
o = utils.optional_param("FOO", callback=lambda *_: "bar")
callback = o["callback"]
assert callback(None, None, ("foo",)) == "bar"

def test_callback_synergy(self):
o = utils.optional_param("FOO", callback=lambda _1, _2, x: x + "bar")
callback = o["callback"]
assert callback(None, None, ("foo",)) == "foobar"

0 comments on commit b29e1da

Please sign in to comment.