move cli config parameter to before subcommand

This commit is contained in:
gmega 2024-12-02 09:20:06 -03:00
parent 3a319aa823
commit 3e1eee7870
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
3 changed files with 15 additions and 11 deletions

View File

@ -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()

View File

@ -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"]

View File

@ -33,4 +33,4 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
bittorrent-benchmarks = "benchmarks.cli:main"
bittorrent-benchmarks = "benchmarks.cli:app"