Refactor CommandParser

This commit is contained in:
Florian Bruhin 2014-01-29 06:18:01 +01:00
parent 34a0dca8bd
commit f5af700f1c

View File

@ -44,15 +44,13 @@ class CommandParser(QObject):
error = pyqtSignal(str) # Emitted if there's an error error = pyqtSignal(str) # Emitted if there's an error
def _parse(self, text): def _parse(self, text):
"""Parses a command"""
self.text = text self.text = text
parts = self.text.strip().split(maxsplit=1) parts = self.text.strip().split(maxsplit=1)
cmdstr = parts[0] cmdstr = parts[0]
try: try:
cmd = cmd_dict[cmdstr] cmd = cmd_dict[cmdstr]
except KeyError: except KeyError:
self.error.emit("{}: no such command".format(cmdstr)) raise NoSuchCommandError(cmdstr)
raise NoSuchCommandError
if len(parts) == 1: if len(parts) == 1:
args = [] args = []
@ -64,12 +62,7 @@ class CommandParser(QObject):
self.args = args self.args = args
def _check(self): def _check(self):
try: self.cmd.check(self.args)
self.cmd.check(self.args)
except ArgumentCountError:
self.error.emit("{}: invalid argument count".format(
self.cmd.mainname))
raise
def _run(self, count=None): def _run(self, count=None):
if count is not None: if count is not None:
@ -78,12 +71,27 @@ class CommandParser(QObject):
self.cmd.run(self.args) self.cmd.run(self.args)
def run(self, text, count=None, ignore_exc=True): def run(self, text, count=None, ignore_exc=True):
"""Parses a command from a line of text.
If ignore_exc is True, ignores exceptions and returns True/False
instead.
Raises NoSuchCommandError if a command wasn't found, and
ArgumentCountError if a command was called with the wrong count of
arguments.
"""
try: try:
self._parse(text) self._parse(text)
self._check() self._check()
except (ArgumentCountError, NoSuchCommandError): except ArgumentCountError:
if ignore_exc: if ignore_exc:
return self.error.emit("{}: invalid argument count".format(
self.cmd.mainname))
return False
else:
raise
except NoSuchCommandError as e:
if ignore_exc:
self.error.emit("{}: no such command".format(e))
return False
else: else:
raise raise
self._run(count=count) self._run(count=count)