improved test/main program

This commit is contained in:
Guido van Rossum 1995-09-18 21:49:24 +00:00
parent a1e7e62893
commit 54c1510cb7
2 changed files with 43 additions and 10 deletions

View File

@ -106,18 +106,19 @@ def decodestring(s):
decode(f, g) decode(f, g)
return g.getvalue() return g.getvalue()
# Small test program, reads stdin, writes stdout. # Small test program
# no arg: encode, any arg: decode.
def test(): def test():
import sys, getopt import sys, getopt
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'deut') opts, args = getopt.getopt(sys.argv[1:], 'deut')
except getopt.error, msg: except getopt.error, msg:
sys.stdout = sys.stderr
print msg print msg
print """usage: basd64 [-d] [-e] [-u] [-t] [file|-] print """usage: basd64 [-d] [-e] [-u] [-t] [file|-]
-d, -u: decode -d, -u: decode
-e: encode (default) -e: encode (default)
-t: decode string 'Aladdin:open sesame'""" -t: decode string 'Aladdin:open sesame'"""
sys.exit(2)
func = encode func = encode
for o, a in opts: for o, a in opts:
if o == '-e': func = encode if o == '-e': func = encode

View File

@ -89,13 +89,45 @@ def unhex(s):
def test(): def test():
import sys import sys
if sys.argv[1:]: import getopt
if sys.argv[1] == '-t': # Quote tabs try:
encode(sys.stdin, sys.stdout, 1) 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: else:
decode(sys.stdin, sys.stdout) 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: else:
encode(sys.stdin, sys.stdout, 0) encode(fp, sys.stdout, tabs)
if fp is not sys.stdin:
fp.close()
if sts:
sys.exit(sts)
if __name__ == '__main__': if __name__ == '__main__':
main() test()