-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
48 lines (40 loc) · 1.22 KB
/
main.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
import api as a
def command_failed(command, has_given_help_hint):
print("")
if command == "":
print("Not a valid command!")
else:
print(command + " is not a valid command!")
if not has_given_help_hint:
print('Enter commands to see a list of available commands.')
print("")
def main():
loop = True
has_given_help_hint = False
# This is an infinite loop, the application is quit using quit()
while loop:
print("Enter a command: ")
user_input = input("--> ")
args = user_input.split()
command = ''
try:
# Take the first argument as the command you
# wish to use, then remove it from the array
command = args[0]
args.pop(0)
# Try to call the command from the api
try:
func = a.commands[command]
func(args)
# If the command doesn't exist, give a help message
# Note: "has_given_help_hint" is there to make sure
# help message is only shown once per execution
except KeyError:
command_failed(command, has_given_help_hint)
has_given_help_hint = True
except IndexError or KeyError:
command_failed(command, has_given_help_hint)
has_given_help_hint = True
# On this script is the main program instead of a module call main()
if __name__ == '__main__':
main()