From 1868d62bb64653312d9ea239cfe665d3cbe8b8c3 Mon Sep 17 00:00:00 2001 From: Max Kutschka <2004maximilian@web.de> Date: Tue, 22 Oct 2024 13:48:31 +0200 Subject: [PATCH] feat: metricq_metric_option now allows multiple metrics --- examples/metricq_sink.py | 7 ++++--- examples/metricq_synchronous_source.py | 3 ++- metricq/cli/__init__.py | 2 ++ metricq/cli/wrapper.py | 16 ++++++++++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/examples/metricq_sink.py b/examples/metricq_sink.py index 847e3950..98226e49 100755 --- a/examples/metricq_sink.py +++ b/examples/metricq_sink.py @@ -34,6 +34,7 @@ import metricq from metricq import Metric from metricq.cli import metricq_command +from metricq.cli.wrapper import metricq_metric_option from metricq.logging import get_logger logger = get_logger() @@ -70,11 +71,11 @@ async def on_data( @metricq_command(default_token="sink-py-dummy") -@click.option("-m", "--metrics", multiple=True, required=True) -def source(server: str, token: str, metrics: list[Metric]) -> None: +@metricq_metric_option(multiple=True) +def source(server: str, token: str, metric: list[Metric]) -> None: # Initialize the DummySink class with a list of metrics given on the # command line. - sink = DummySink(metrics=metrics, token=token, url=server) + sink = DummySink(metrics=metric, token=token, url=server) # Run the sink. This call will block until the connection is closed. sink.run() diff --git a/examples/metricq_synchronous_source.py b/examples/metricq_synchronous_source.py index 32b8c7eb..930cb984 100755 --- a/examples/metricq_synchronous_source.py +++ b/examples/metricq_synchronous_source.py @@ -37,10 +37,11 @@ from metricq.cli import metricq_command from metricq.logging import get_logger +logger = get_logger() + @metricq_command(default_token="source-py-dummy") def synchronous_source(server: str, token: str) -> None: - logger = get_logger() ssource = SynchronousSource(token=token, url=server) ssource.declare_metrics( { diff --git a/metricq/cli/__init__.py b/metricq/cli/__init__.py index c393106e..2fdc524b 100644 --- a/metricq/cli/__init__.py +++ b/metricq/cli/__init__.py @@ -2,6 +2,7 @@ ChoiceParam, CommandLineChoice, DurationParam, + MetricParam, TemplateStringParam, TimestampParam, ) @@ -18,6 +19,7 @@ "DurationParam", "TemplateStringParam", "TimestampParam", + "MetricParam", "metricq_command", "metricq_metric_option", "metricq_server_option", diff --git a/metricq/cli/wrapper.py b/metricq/cli/wrapper.py index 31e9c30c..dc8097f6 100644 --- a/metricq/cli/wrapper.py +++ b/metricq/cli/wrapper.py @@ -41,19 +41,27 @@ def metricq_token_option(default: str) -> Callable[[FC], FC]: ) -def metricq_metric_option(default: Optional[str] = None) -> Callable[[FC], FC]: +def metricq_metric_option( + default: Optional[str] = None, multiple: bool = False +) -> Callable[[FC], FC]: + response_default = default if (default is None or not multiple) else [default] + help = "Use the -–metric / -m parameter to specify which metric the program should use." + if default: + help += "You can also specify multpile metrics." + return option( "--metric", + "-m", type=MetricParam(), metavar="METRIC", show_default=True, required=default is None, - default=default, - help="Use the -–metric parameter to specify which metric the program should use", + default=response_default, + multiple=multiple, + help=help, ) - def get_metric_command_logger() -> logging.Logger: logger = get_logger() logger.setLevel(logging.WARNING)