# HG changeset patch
# User Steve Losh <steve@stevelosh.com>
# Date 1251508211 14400
# Node ID 8cc8f8b24bb37859562ad80c90ba3eb2f438e638
# Parent  b346af17863f16e0cc6266d722ede5c3cf9f49b5
Make the errors friendlier.

diff -r b346af17863f -r 8cc8f8b24bb3 t.py
--- a/t.py	Fri Aug 28 21:05:56 2009 -0400
+++ b/t.py	Fri Aug 28 21:10:11 2009 -0400
@@ -2,7 +2,7 @@
 
 from __future__ import with_statement
 
-import os, re, hashlib, operator
+import os, re, sys, hashlib, operator
 from optparse import OptionParser
 
 
@@ -12,11 +12,15 @@
 
 class AmbiguousPrefix(Exception):
     """Raised when trying to use a prefix that could identify multiple tasks."""
-    pass
+    def __init__(self, prefix):
+        self.prefix = prefix
+    
 
 class UnknownPrefix(Exception):
     """Raised when trying to use a prefix that does not match any tasks."""
-    pass
+    def __init__(self, prefix):
+        self.prefix = prefix
+    
 
 
 def _hash(s):
@@ -103,9 +107,9 @@
         if len(matched) == 1:
             return self.tasks[matched[0]]
         elif len(matched) == 0:
-            raise UnknownPrefix
+            raise UnknownPrefix(prefix)
         else:
-            raise AmbiguousPrefix
+            raise AmbiguousPrefix(prefix)
     
     def add_task(self, text):
         """Add a new, unfinished task with the given summary text."""
@@ -204,20 +208,25 @@
     td = TaskDict(taskdir=options.taskdir, name=options.name)
     text = ' '.join(args).strip()
     
-    if options.finish:
-        td.finish_task(options.finish)
-        td.write()
-    elif options.delete_finished:
-        td.delete_finished()
-        td.write()
-    elif options.edit:
-        td.edit_task(options.edit, text)
-        td.write()
-    elif text:
-        td.add_task(text)
-        td.write()
-    else:
-        td.print_list(verbose=options.verbose)
+    try:
+        if options.finish:
+            td.finish_task(options.finish)
+            td.write()
+        elif options.delete_finished:
+            td.delete_finished()
+            td.write()
+        elif options.edit:
+            td.edit_task(options.edit, text)
+            td.write()
+        elif text:
+            td.add_task(text)
+            td.write()
+        else:
+            td.print_list(verbose=options.verbose)
+    except AmbiguousPrefix, e:
+        sys.stderr.write('The ID "%s" matches more than one task.' % e.prefix)
+    except UnknownPrefix, e:
+        sys.stderr.write('The ID "%s" does not match any task.' % e.prefix)
 
 
 if __name__ == '__main__':