From 3e1eee7870b59273692b83610b12270563253f4c Mon Sep 17 00:00:00 2001 From: gmega Date: Mon, 2 Dec 2024 09:20:06 -0300 Subject: [PATCH] move cli config parameter to before subcommand --- benchmarks/cli.py | 22 +++++++++++-------- ...erfile => bittorrent-benchmarks.Dockerfile | 2 +- pyproject.toml | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) rename docker/bittorrent-benchmarks.Dockerfile => bittorrent-benchmarks.Dockerfile (70%) diff --git a/benchmarks/cli.py b/benchmarks/cli.py index f3a9949..e26d333 100644 --- a/benchmarks/cli.py +++ b/benchmarks/cli.py @@ -29,24 +29,24 @@ def _parse_config(config: Path): @app.command() -def list(config: Path): +def list(ctx: typer.Context): """ Lists the experiments available in CONFIG. """ - experiments = _parse_config(config) - print(f'Available experiments in {config}:') + experiments = ctx.obj + print(f'Available experiments are:') for experiment in experiments.keys(): print(f' - {experiment}') @app.command() -def run(config: Path, experiment: str): +def run(ctx: typer.Context, experiment: str): """ Runs the experiment with name EXPERIMENT. """ - experiments = _parse_config(config) + experiments = ctx.obj if experiment not in experiments: - print(f'Experiment {experiment} not found in {config}.') + print(f'Experiment {experiment} not found.') sys.exit(-1) experiments[experiment].build().run() @@ -60,10 +60,14 @@ def _init_logging(): ) -def main(): +@app.callback() +def main(ctx: typer.Context, config: Path): + if ctx.resilient_parsing: + return + + ctx.obj = _parse_config(config) _init_logging() - app() if __name__ == '__main__': - main() + app() diff --git a/docker/bittorrent-benchmarks.Dockerfile b/bittorrent-benchmarks.Dockerfile similarity index 70% rename from docker/bittorrent-benchmarks.Dockerfile rename to bittorrent-benchmarks.Dockerfile index 88e92cb..331de1b 100644 --- a/docker/bittorrent-benchmarks.Dockerfile +++ b/bittorrent-benchmarks.Dockerfile @@ -11,5 +11,5 @@ RUN poetry install --no-root COPY . . RUN poetry install -ENTRYPOINT ["/usr/local/bin/bittorrent-benchmarks", "run", "/opt/bittorrent-benchmarks/experiments.yaml"] +ENTRYPOINT ["/usr/local/bin/bittorrent-benchmarks", "/opt/bittorrent-benchmarks/experiments.yaml"] diff --git a/pyproject.toml b/pyproject.toml index 6b00b47..161e181 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,4 +33,4 @@ requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] -bittorrent-benchmarks = "benchmarks.cli:main" \ No newline at end of file +bittorrent-benchmarks = "benchmarks.cli:app" \ No newline at end of file