-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.py
executable file
·53 lines (42 loc) · 1.46 KB
/
parser.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
#!/usr/bin/python
import constants
from commands.command_words import CommandWords
class Parser(object):
"""
Parses user input, searching for registered commands.
"""
def __init__(self, commandWords):
"""
Initializes parser.
@param commandWords: List of commands.
"""
if not commandWords:
errorMsg = "Parser must be initialized with CommandWords object."
raise AssertionError(errorMsg)
self._commandWords = commandWords
def getNextCommand(self):
"""
Retrieves next command from user.
"""
userInput = raw_input(constants.COMMAND_PROMPT)
userInput = userInput.strip().lower()
while not self._commandRecognized(userInput):
print ("Command '%s' not recognized. Type 'help' for help."
% userInput)
print ""
userInput = raw_input(constants.COMMAND_PROMPT)
userInput = userInput.strip().lower()
command = self._commandWords.getCommand(userInput)
return command
def _commandRecognized(self, name):
"""
Helper method to determine if user
specified known command.
@param name: Command's name
@return: True if recognized, False otherwise.
"""
#Make sure name is not None
if not name:
return False
recognized = self._commandWords.isCommand(name)
return recognized