Commit aae4efa7 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Throw exception when an argument could not be found.

parent 3c7ef04b
import Command from Command import *
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
import TodoList import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException from Utils import convert_todo_number, InvalidTodoNumberException
class AppendCommand(Command.Command): class AppendCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
p_out=lambda a: None, p_out=lambda a: None,
p_err=lambda a: None, p_err=lambda a: None,
...@@ -11,20 +11,18 @@ class AppendCommand(Command.Command): ...@@ -11,20 +11,18 @@ class AppendCommand(Command.Command):
super(AppendCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None) super(AppendCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None)
def execute(self): def execute(self):
number = self.argument(0) try:
if number: number = convert_todo_number(self.argument(0))
try: text = " ".join(self.args[1:])
number = convert_todo_number(number)
text = " ".join(self.args[1:])
if text: if text:
todo = self.todolist.todo(number) todo = self.todolist.todo(number)
self.todolist.append(todo, text) self.todolist.append(todo, text)
self.out(pretty_print(todo, [self.todolist.pp_number()])) self.out(pretty_print(todo, [self.todolist.pp_number()]))
else: else:
self.error(self.usage())
except InvalidTodoNumberException:
self.error(self.usage()) self.error(self.usage())
except TodoList.InvalidTodoException: except (InvalidCommandArgument, InvalidTodoNumberException):
self.error("Invalid todo number given.") self.error(self.usage())
except TodoList.InvalidTodoException:
self.error("Invalid todo number given.")
class InvalidCommandArgument(Exception):
pass
class Command(object): class Command(object):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
p_out=lambda a: None, p_out=lambda a: None,
...@@ -37,13 +40,13 @@ class Command(object): ...@@ -37,13 +40,13 @@ class Command(object):
""" """
return False return False
def argument(self, p_number): def argument(self, p_number, p_error=None):
""" Retrieves a value from the argument list at the given position. """ """ Retrieves a value from the argument list at the given position. """
value = None value = None
try: try:
value = self.args[p_number] value = self.args[p_number]
except IndexError: except IndexError:
self.error(self.usage()) raise InvalidCommandArgument
return value return value
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment