dnd-rolltable/rolltable/cli.py

42 lines
1020 B
Python
Raw Normal View History

2022-07-30 14:20:26 -07:00
from rolltable import tables
import typer
from rich import print
from rich.table import Table
2022-07-31 15:03:19 -07:00
from pathlib import Path
from typing import List
2022-07-30 14:20:26 -07:00
app = typer.Typer()
@app.command("roll-table")
def create(
2022-07-31 15:03:19 -07:00
sources: List[Path] = typer.Argument(
2022-07-30 14:20:26 -07:00
...,
2022-07-31 15:03:19 -07:00
help="Path to one or more yaml-formatted source file."),
2022-07-30 14:20:26 -07:00
frequency: str = typer.Option(
'default',
help='use the specified frequency from the source file'),
die: int = typer.Option(
20,
help='The size of the die for which to create a table'),
2022-07-31 15:03:19 -07:00
collapsed: bool = typer.Option(
2022-07-30 14:20:26 -07:00
True,
help='If True, collapse multiple die values with the same option.')
):
"""
CLI for creating roll tables.
"""
2022-07-31 15:03:19 -07:00
rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die)
rows = rt.rows if collapsed else rt.expanded_rows
table = Table(*rows[0])
for row in rows[1:]:
2022-07-30 14:20:26 -07:00
table.add_row(*row)
print(table)
if __name__ == '__main__':
app()