processCommand now handles comments and empty lines, and accepts a string as param instead of an array of words

This commit is contained in:
David Vierra 2010-12-07 22:42:54 -10:00
parent 73815c8e74
commit 0d03e7bbf5

24
mce.py
View File

@ -1005,7 +1005,7 @@ class mce(object):
commandsFromFile = commandFile.readlines() commandsFromFile = commandFile.readlines()
for commandFromFile in commandsFromFile: for commandFromFile in commandsFromFile:
print commandFromFile print commandFromFile
self.processCommand(commandFromFile.split()) self.processCommand(commandFromFile)
self._save("") self._save("")
def _quit(self, command): def _quit(self, command):
@ -1193,7 +1193,7 @@ class mce(object):
if len(sys.argv): if len(sys.argv):
#process one command from command line #process one command from command line
try: try:
self.processCommand(sys.argv) self.processCommand(" ".join(sys.argv))
except UsageError: except UsageError:
self.printUsageAndQuit(); self.printUsageAndQuit();
self._save([]); self._save([]);
@ -1205,14 +1205,8 @@ class mce(object):
while True: while True:
try: try:
command = raw_input("{0}> ".format(self.level.displayName)) command = raw_input("{0}> ".format(self.level.displayName))
command = command.strip();
print print
self.processCommand(command)
if len(command) == 0: continue
if command[0] == "#": continue
commandWords = command.split()
self.processCommand(commandWords)
except EOFError, e: except EOFError, e:
print "End of file. Saving automatically." print "End of file. Saving automatically."
@ -1228,7 +1222,15 @@ class mce(object):
def processCommand(self, command): def processCommand(self, command):
keyword = command.pop(0).lower() command = command.strip();
if len(command) == 0: return
if command[0] == "#": return
commandWords = command.split()
keyword = commandWords.pop(0).lower()
if not keyword in self.commands: if not keyword in self.commands:
matches = filter(lambda x:x.startswith(keyword), self.commands); matches = filter(lambda x:x.startswith(keyword), self.commands);
if len(matches) == 1: if len(matches) == 1:
@ -1244,7 +1246,7 @@ class mce(object):
func = getattr(self, "_" + keyword) func = getattr(self, "_" + keyword)
try: try:
func(command) func(commandWords)
except PlayerNotFound, e: except PlayerNotFound, e:
print "Cannot find player {0}".format(e.args[0]) print "Cannot find player {0}".format(e.args[0])
self._player([]) self._player([])