1
|
|
|
import cmd |
2
|
|
|
import os |
3
|
|
|
import argparse |
4
|
|
|
from lib.main import SpeedOfPi |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CLI(cmd.Cmd): |
8
|
|
|
def __init__(self): |
9
|
|
|
self.game = SpeedOfPi() |
10
|
|
|
self.update_available = None |
11
|
|
|
cmd.Cmd.__init__(self) |
12
|
|
|
os.system('clear') |
13
|
|
|
self.prompt = "-->>" |
14
|
|
|
self.intro = "SpeedOfPi Command line interface" |
15
|
|
|
|
16
|
|
|
def do_exit(self, args): |
17
|
|
|
"""Exits from the console""" |
18
|
|
|
return -1 |
19
|
|
|
|
20
|
|
|
def do_shell(self, args): |
21
|
|
|
"""Pass command to a system shell when line begins with '!'""" |
22
|
|
|
os.system(args) |
23
|
|
|
|
24
|
|
|
def postloop(self): |
25
|
|
|
"""Take care of any unfinished business. |
26
|
|
|
Despite the claims in the Cmd |
27
|
|
|
documentaion, Cmd.postloop() is not a stub. |
28
|
|
|
""" |
29
|
|
|
cmd.Cmd.postloop(self) # Clean up command completion |
30
|
|
|
print("Exiting...") |
31
|
|
|
|
32
|
|
|
def emptyline(self): |
33
|
|
|
"""Do nothing on empty input line""" |
34
|
|
|
pass |
35
|
|
|
|
36
|
|
|
def default(self, line): |
37
|
|
|
"""Called on an input line when the command prefix is not recognized.""" |
38
|
|
|
print("Unable to find '{command}' for help on commands type: help".format(command=line)) |
39
|
|
|
|
40
|
|
|
def do_clear(self, args): |
41
|
|
|
"""clears the screen""" |
42
|
|
|
os.system('clear') |
43
|
|
|
print(self.intro) |
44
|
|
|
|
45
|
|
|
def do_play(self, args): |
46
|
|
|
self.game.single_player() |
47
|
|
|
|
48
|
|
|
def do_update(self, args): |
49
|
|
|
"""check for updates""" |
50
|
|
|
if not self.update_available: |
51
|
|
|
print("Checking for updates") |
52
|
|
|
if not self.update_check(): |
53
|
|
|
return print("No updates available") |
54
|
|
|
|
55
|
|
|
if not args: |
56
|
|
|
self.game.update() |
57
|
|
|
print("Updating to latest stable version") |
58
|
|
|
else: |
59
|
|
|
parser = argparse.ArgumentParser() |
60
|
|
|
parser.add_argument(dest="branch") |
61
|
|
|
|
62
|
|
|
output, unknownArgs = parser.parse_known_args(args.split()) |
63
|
|
|
self.game.update(output.branch) |
64
|
|
|
print("Updating to latest beta version") |
65
|
|
|
|
66
|
|
|
def update_check(self, branch=False): |
67
|
|
|
self.update_available = self.game.update_available(branch) |
68
|
|
|
return self.update_available |
69
|
|
|
|
70
|
|
|
if __name__ == '__main__': |
71
|
|
|
cli = CLI() |
72
|
|
|
cli.cmdloop() |
73
|
|
|
|
74
|
|
|
|