Fix command argument handling

This commit is contained in:
Florian Bruhin 2014-01-16 16:05:31 +01:00
parent 8f9bfb02c3
commit 1736eb20c8

View File

@ -15,14 +15,14 @@ class CommandParser(QObject):
def parse(self, text): def parse(self, text):
parts = text.strip().split() parts = text.strip().split()
cmd = parts[0] cmd = parts[0]
args = parts[1:] argv = parts[1:]
obj = cmd_dict[cmd] obj = cmd_dict[cmd]
try: try:
obj.check(args) obj.check(argv)
except TypeError: except TypeError:
# TODO # TODO
raise raise
obj.run(args) obj.run(argv)
class Command(QObject): class Command(QObject):
nargs = 0 nargs = 0
@ -34,27 +34,33 @@ class Command(QObject):
if cls.name: if cls.name:
cmd_dict[cls.name] = cls() cmd_dict[cls.name] = cls()
def check(self, *args): def check(self, argv):
if ((isinstance(self.nargs, int) and len(args) != self.nargs) or if ((isinstance(self.nargs, int) and len(argv) != self.nargs) or
(self.nargs == '?' and len(args) > 1) or (self.nargs == '?' and len(argv) > 1) or
(self.nargs == '+' and len(args) < 1)): (self.nargs == '+' and len(argv) < 1)):
raise TypeError("Invalid argument count!") raise TypeError("Invalid argument count!")
def run(self, *args): def run(self, argv):
if not self.signal: if not self.signal:
raise NotImplementedError raise NotImplementedError
self.signal.emit(*args) self.signal.emit()
class OpenCmd(Command): class OpenCmd(Command):
nargs = 1 nargs = 1
name = 'open' name = 'open'
signal = pyqtSignal(str) signal = pyqtSignal(str)
def run(self, argv):
self.signal.emit(argv[0])
class TabOpenCmd(Command): class TabOpenCmd(Command):
nargs = 1 nargs = 1
name = 'tabopen' name = 'tabopen'
signal = pyqtSignal(str) signal = pyqtSignal(str)
def run(self, argv):
self.signal.emit(argv[0])
class QuitCmd(Command): class QuitCmd(Command):
nargs = 0 nargs = 0
name = 'quit' name = 'quit'