1  #!/usr/bin/python
 2 # Filename: cat.py
 3 
 4 import sys
 5 
 6 def readfile(filename):
 7     '''Print a file to the standard output.'''
 8     f = file(filename)
 9     while True:
10         line = f.readline()
11         if len(line) == 0:
12             break
13         print line, # notice comma
14     f.close()
15 
16 # Script starts from here
17 if len(sys.argv) < 2:
18     print 'No action specified.'
19     sys.exit()
20 
21 if sys.argv[1].startswith('--'):
22     option = sys.argv[1][2:]
23     # fetch sys.argv[1] but without the first two characters
24     if option == 'version':
25         print 'Version 1.2'
26     elif option == 'help':
27         print '''\
28 This program prints files to the standard output.
29 Any number of files can be specified.
30 Options include:
31   --version : Prints the version number
32   --help    : Display this help'''
33     else:
34         print 'Unknown option.'
35     sys.exit()
36 else:
37     for filename in sys.argv[1:]:
38         readfile(filename) 
View Code

相关文章: