changeset 258:1a55ba6ceca1

Make run-example a bit more user friendly.
author Bryan O'Sullivan <bos@serpentine.com>
date Wed, 06 Jun 2007 08:11:08 -0700
parents d9d29e7cf5bd
children b42689a730a2
files en/examples/run-example
diffstat 1 files changed, 43 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/en/examples/run-example	Wed Jun 06 22:11:32 2007 +1000
+++ b/en/examples/run-example	Wed Jun 06 08:11:08 2007 -0700
@@ -323,10 +323,26 @@
             os.system('diff -u %s %s 1>&2' % (oldname, errname))
             return True
 
+def print_help(exit, msg=None):
+    if msg:
+        print >> sys.stderr, 'Error:', msg
+    print >> sys.stderr, 'Usage: run-example [options] [test...]'
+    print >> sys.stderr, 'Options:'
+    print >> sys.stderr, '  -a --all       run all tests in this directory'
+    print >> sys.stderr, '  -h --help      print this help message'
+    print >> sys.stderr, '  -v --verbose   display extra debug output'
+    sys.exit(exit)
+
 def main(path='.'):
-    opts, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
+    opts, args = getopt.getopt(sys.argv[1:], '?ahv',
+                               ['all', 'help', 'verbose'])
     verbose = False
+    run_all = False
     for o, a in opts:
+        if o in ('-h', '-?', '--help'):
+            print_help(0)
+        if o in ('-a', '--all'):
+            run_all = True
         if o in ('-v', '--verbose'):
             verbose = True
     errs = 0
@@ -344,26 +360,32 @@
             else:
                 print >> sys.stderr, '%s: not a file, or not executable' % a
                 errs += 1
-        return errs
-    names = os.listdir(path)
-    names.sort()
-    for name in names:
-        if name == 'run-example' or name.startswith('.'): continue
-        if name.endswith('.out') or name.endswith('~'): continue
-        if name.endswith('.run'): continue
-        pathname = os.path.join(path, name)
-        try:
-            st = os.lstat(pathname)
-        except OSError, err:
-            # could be an output file that was removed while we ran
-            if err.errno != errno.ENOENT:
-                raise
-            continue
-        if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
-            if example(pathname, verbose).run():
-                errs += 1
-    print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
+    elif run_all:
+        names = os.listdir(path)
+        names.sort()
+        for name in names:
+            if name == 'run-example' or name.startswith('.'): continue
+            if name.endswith('.out') or name.endswith('~'): continue
+            if name.endswith('.run'): continue
+            pathname = os.path.join(path, name)
+            try:
+                st = os.lstat(pathname)
+            except OSError, err:
+                # could be an output file that was removed while we ran
+                if err.errno != errno.ENOENT:
+                    raise
+                continue
+            if stat.S_ISREG(st.st_mode) and st.st_mode & 0111:
+                if example(pathname, verbose).run():
+                    errs += 1
+        print >> open(os.path.join(path, '.run'), 'w'), time.asctime()
+    else:
+        print_help(1, msg='no test names given, and --all not provided')
     return errs
 
 if __name__ == '__main__':
-    sys.exit(main())
+    try:
+        sys.exit(main())
+    except KeyboardInterrupt:
+        print >> sys.stderr, 'interrupted!'
+        sys.exit(1)