Commit ddf3eed0 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add convenience functions for handling user-supplied arguments.

parent f4fadceb
...@@ -27,6 +27,24 @@ def error(p_message, p_exit=True): ...@@ -27,6 +27,24 @@ def error(p_message, p_exit=True):
if p_exit: if p_exit:
exit(1) exit(1)
def argument(number):
""" Retrieves a value from the argument list. """
try:
value = sys.argv[number]
except IndexError:
usage()
return value
def convert_number(number):
""" Converts a string number to an integer. """
try:
number = int(number)
except ValueError:
error("Invalid todo number given.")
return number
class Application(object): class Application(object):
def __init__(self): def __init__(self):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
...@@ -40,60 +58,38 @@ class Application(object): ...@@ -40,60 +58,38 @@ class Application(object):
def add(self): def add(self):
""" Adds a todo item to the list. """ """ Adds a todo item to the list. """
try: self.todolist.add(argument(2))
self.todolist.add(sys.argv[2]) self.print_todo(self.todolist.count())
self.dirty = True
self.print_todo(self.todolist.count())
self.dirty = True
except IndexError:
error("No todo text was given.")
def append(self): def append(self):
""" Appends a text to a todo item. """ """ Appends a text to a todo item. """
number = convert_number(argument(2))
text = argument(3)
try: self.todolist.append(number, text)
number = sys.argv[2]
text = sys.argv[3]
except IndexError:
usage()
try: self.print_todo(number)
number = int(number) self.dirty = True
self.todolist.append(number, text)
self.print_todo(number) self.dirty = True
self.dirty = True
except ValueError:
error("Invalid todo number given.")
def do(self): def do(self):
try: number = convert_number(argument(2))
number = sys.argv[2] todo = self.todolist.todo(number)
except IndexError:
usage()
try:
number = int(number)
self.todolist.todo(number).set_completed()
if todo:
self.print_todo(number) self.print_todo(number)
self.dirty = True self.dirty = True
except IndexError:
usage()
except ValueError:
error("Invalid todo number given.")
def pri(self): def pri(self):
try: number = convert_number(argument(2))
number = sys.argv[2] priority = argument(3)
priority = sys.argv[3]
except IndexError:
usage()
if re.match('^[A-Z]$', priority): if re.match('^[A-Z]$', priority):
try: todo = self.todolist.todo(number)
number = int(number)
todo = self.todolist.todo(number) if todo:
old_priority = todo.priority() old_priority = todo.priority()
todo.set_priority(priority) todo.set_priority(priority)
...@@ -101,10 +97,6 @@ class Application(object): ...@@ -101,10 +97,6 @@ class Application(object):
% (old_priority, priority) % (old_priority, priority)
self.print_todo(number) self.print_todo(number)
self.dirty = True self.dirty = True
except AttributeError:
error("Invalid todo number given.")
except ValueError:
error("Invalid todo number given.")
else: else:
error("Invalid priority given.") error("Invalid priority given.")
......
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