diff options
author | Guido van Rossum <guido@python.org> | 1995-09-18 21:49:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1995-09-18 21:49:24 (GMT) |
commit | 54c1510cb7eeda3135cd4ad23aebef8d13db7bc1 (patch) | |
tree | 14e73d1959763c9f33255be9b5435901e4be669f /Lib | |
parent | a1e7e6289301d981eff8357ed5b1f8ba95a9fc71 (diff) | |
download | cpython-54c1510cb7eeda3135cd4ad23aebef8d13db7bc1.zip cpython-54c1510cb7eeda3135cd4ad23aebef8d13db7bc1.tar.gz cpython-54c1510cb7eeda3135cd4ad23aebef8d13db7bc1.tar.bz2 |
improved test/main program
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/base64.py | 5 | ||||
-rwxr-xr-x | Lib/quopri.py | 48 |
2 files changed, 43 insertions, 10 deletions
diff --git a/Lib/base64.py b/Lib/base64.py index 55f01ce..90b5273 100755 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -106,18 +106,19 @@ def decodestring(s): decode(f, g) return g.getvalue() -# Small test program, reads stdin, writes stdout. -# no arg: encode, any arg: decode. +# Small test program def test(): import sys, getopt try: opts, args = getopt.getopt(sys.argv[1:], 'deut') except getopt.error, msg: + sys.stdout = sys.stderr print msg print """usage: basd64 [-d] [-e] [-u] [-t] [file|-] -d, -u: decode -e: encode (default) -t: decode string 'Aladdin:open sesame'""" + sys.exit(2) func = encode for o, a in opts: if o == '-e': func = encode diff --git a/Lib/quopri.py b/Lib/quopri.py index 4c39228..55608d1 100755 --- a/Lib/quopri.py +++ b/Lib/quopri.py @@ -89,13 +89,45 @@ def unhex(s): def test(): import sys - if sys.argv[1:]: - if sys.argv[1] == '-t': # Quote tabs - encode(sys.stdin, sys.stdout, 1) - else: - decode(sys.stdin, sys.stdout) - else: - encode(sys.stdin, sys.stdout, 0) + import getopt + try: + opts, args = getopt.getopt(sys.argv[1:], 'td') + except getopt.error, msg: + sys.stdout = sys.stderr + print msg + print "usage: quopri [-t | -d] [file] ..." + print "-t: quote tabs" + print "-d: decode; default encode" + sys.exit(2) + deco = 0 + tabs = 0 + for o, a in opts: + if o == '-t': tabs = 1 + if o == '-d': deco = 1 + if tabs and deco: + sys.stdout = sys.stderr + print "-t and -d are mutually exclusive" + sys.exit(2) + if not args: args = ['-'] + sts = 0 + for file in args: + if file == '-': + fp = sys.stdin + else: + try: + fp = open(file) + except IOError, msg: + sys.stderr.write("%s: can't open (%s)\n" % (file, msg)) + sts = 1 + continue + if deco: + decode(fp, sys.stdout) + else: + encode(fp, sys.stdout, tabs) + if fp is not sys.stdin: + fp.close() + if sts: + sys.exit(sts) if __name__ == '__main__': - main() + test() |