File: dice.py

package info (click to toggle)
aioconsole 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 332 kB
  • sloc: python: 2,022; makefile: 6
file content (31 lines) | stat: -rw-r--r-- 718 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Run a CLI to simulate dice throws."""

import random
import asyncio
import argparse

from aioconsole import AsynchronousCli


async def dice(reader, writer, faces):
    for _ in range(3):
        await asyncio.sleep(0.33)
        writer.write(".")
        await writer.drain()
    writer.write("\n")
    return random.randint(1, faces)


def main():
    parser = argparse.ArgumentParser(description="Throw a dice.")
    parser.add_argument(
        "--faces", "-f", metavar="N", type=int, default=6, help="Number of faces"
    )
    cli = AsynchronousCli({"dice": (dice, parser)}, prog="dice")

    loop = asyncio.get_event_loop()
    loop.run_until_complete(cli.interact())


if __name__ == "__main__":
    main()