-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.py
executable file
·60 lines (43 loc) · 1.63 KB
/
cli.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
#!/usr/bin/env python3
"""
This module gives simple command line access to the functions.
Start it without parameters to run a simple game.
Start it with one of the functions in stat.py to run simulations and get stats.
"""
import logging
import sys
from mau_mau import rules, sim, stats
log = logging.getLogger()
_rulesOfTheGame = rules.MauMau()
def play_simple_game(players=3):
log.setLevel(level=logging.DEBUG)
playedGame = sim.play_game(_rulesOfTheGame, players)
log.info("And the winner is %s", playedGame.table.winner.name)
def play_interactive_game():
log.setLevel(level=logging.DEBUG)
playedGame = sim.play_game(_rulesOfTheGame, ['Eric', 'John', 'human'])
log.info("And the winner is %s", playedGame.table.winner.name)
def get_function_from_name(name):
if not name:
return play_simple_game
try:
return getattr(stats, name)
except AttributeError:
return play_interactive_game
def simple_parse_args(argv):
"""For more sophisticated stuff use argparse or a cli tool like plumbum"""
return (None if len(argv) == 1 else argv[1],
argv[2:] if len(argv) > 2 else [])
def main():
try:
fmt = '%(name)-20s%(lineno)-3s %(funcName)-17s: %(message)s'.format()
logging.basicConfig(format=fmt, level=logging.INFO)
functionName, args = simple_parse_args(sys.argv)
functionObject = get_function_from_name(functionName)
log.info("%s(%s) ...", functionObject.__name__, ", ".join(args))
functionObject(*args)
return 0
except KeyboardInterrupt:
print("\n... bye!")
if __name__ == '__main__':
sys.exit(main())