diff --git a/broker/commands.py b/broker/commands.py index d4c73cf..15330b7 100644 --- a/broker/commands.py +++ b/broker/commands.py @@ -229,14 +229,7 @@ def checkout(ctx, background, nick, count, args_file, provider_labels, **kwargs) if provider_labels: broker_args["provider_labels"] = parse_labels(provider_labels) - # if additional arguments were passed, include them in the broker args - # strip leading -- characters - broker_args.update( - { - (key[2:] if key.startswith("--") else key): val - for key, val in zip(ctx.args[::2], ctx.args[1::2]) - } - ) + broker_args.update(helpers.kwargs_from_click_ctx(ctx)) if background: helpers.fork_broker() Broker(**broker_args).checkout() @@ -375,14 +368,9 @@ def execute(ctx, background, nick, output_format, artifacts, args_file, provider broker_args["args_file"] = args_file if provider_labels: broker_args["provider_labels"] = parse_labels(provider_labels) - # if additional arguments were passed, include them in the broker args - # strip leading -- characters - broker_args.update( - { - (key[2:] if key.startswith("--") else key): val - for key, val in zip(ctx.args[::2], ctx.args[1::2]) - } - ) + + broker_args.update(helpers.kwargs_from_click_ctx(ctx)) + if background: helpers.fork_broker() result = Broker(**broker_args).execute() diff --git a/broker/helpers.py b/broker/helpers.py index 6cf7b43..44a30b4 100644 --- a/broker/helpers.py +++ b/broker/helpers.py @@ -1,4 +1,5 @@ """Miscellaneous helpers live here.""" + import collections from collections import UserDict, namedtuple from collections.abc import MutableMapping @@ -267,6 +268,24 @@ def yaml_format(in_struct): return yaml.dump(in_struct, default_flow_style=False, sort_keys=False) +def kwargs_from_click_ctx(ctx): + """Convert a Click context object to a dictionary of keyword arguments.""" + # if users use `=` to note arg=value assignment, then we need to split it + _args = [] + for arg in ctx.args: + if "=" in arg: + _args.extend(arg.split("=")) + else: + _args.append(arg) + ctx.args = _args + # if additional arguments were passed, include them in the broker args + # strip leading -- characters + return { + (key[2:] if key.startswith("--") else key): val + for key, val in zip(ctx.args[::2], ctx.args[1::2]) + } + + class Emitter: """Class that provides a simple interface to emit messages to a json-formatted file. diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 85c6a04..d11291b 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -163,3 +163,12 @@ def test_dict_from_paths_nested(): paths = {"person_name": "person/name", "person_zip": "person/address/zip"} result = helpers.dict_from_paths(source_dict, paths) assert result == {"person_name": "John", "person_zip": "12345"} + + +def test_kwargs_from_click_ctx(): + """Test that we can extract kwargs from a mixed-style click context object""" + class ctx: + args = ["--arg1", "value1", "--arg2=value2", "--some-flag"] + + kwargs = helpers.kwargs_from_click_ctx(ctx) + assert kwargs == {"arg1": "value1", "arg2": "value2"}