-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll.py
62 lines (53 loc) · 1.55 KB
/
roll.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! python3
"""
usage: roll [-h] [-V] [-n] [-v] expression [expression ...]
Command Line Interface for the xdice library
positional arguments:
expression mathematical expression(s) containing dice <n>d<s> patterns
optional arguments:
-h, --help show this help message and exit
-V, --version print the xdice version string and exit
-n, --num_only print numeric result only
-v, --verbose print a verbose result
"""
import argparse
import xdice
def main():
parser = argparse.ArgumentParser(
prog="roll", description="Command Line Interface for the xdice library"
)
parser.add_argument(
"expression",
nargs="+",
help="mathematical expression(s) containing dice <n>d<s> patterns",
)
parser.add_argument(
"-V",
"--version",
action="store_true",
help="print the xdice version string and exit",
)
parser.add_argument(
"-n",
"--num_only",
action="store_true",
help="print numeric result only",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="print a verbose result",
)
args = parser.parse_args()
if args.version:
print("XDice {}".format(xdice.__VERSION__))
return
for expr in args.expression:
ps = xdice.roll(expr)
if args.num_only:
print(ps)
else:
print("{}\t({})".format(ps, ps.format(args.verbose)))
if __name__ == "__main__":
main()