]> granicus.if.org Git - python/commitdiff
Rewrite the (test) main program so that when used as a script, it can
authorGuido van Rossum <guido@python.org>
Thu, 25 Jun 1998 02:39:00 +0000 (02:39 +0000)
committerGuido van Rossum <guido@python.org>
Thu, 25 Jun 1998 02:39:00 +0000 (02:39 +0000)
retrieve one or more URLs to stdout.  Use -t to run the self-test.

Lib/urllib.py

index b9317f28b32a61b009efc7727deff35305178634..e21c9a24e102d1efb36e4fa0dfe946cddcf6dd3f 100644 (file)
@@ -937,9 +937,7 @@ def test1():
 
 
 # Test program
-def test():
-       import sys
-       args = sys.argv[1:]
+def test(args=[]):
        if not args:
                args = [
                        '/etc/passwd',
@@ -970,7 +968,33 @@ def test():
        finally:
                urlcleanup()
 
+def main():
+       import getopt, sys
+       try:
+               opts, args = getopt.getopt(sys.argv[1:], "th")
+       except getopt.error, msg:
+               print msg
+               print "Use -h for help"
+               return
+       t = 0
+       for o, a in opts:
+               if o == '-t':
+                       t = t + 1
+               if o == '-h':
+                       print "Usage: python urllib.py [-t] [url ...]"
+                       print "-t runs self-test;",
+                       print "otherwise, contents of urls are printed"
+                       return
+       if t:
+               if t > 1:
+                       test1()
+               test(args)
+       else:
+               if not args:
+                       print "Use -h for help"
+               for url in args:
+                       print urlopen(url).read(),
+
 # Run test program when run as a script
 if __name__ == '__main__':
-       test1()
-       test()
+       main()