-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.py
120 lines (109 loc) · 3.21 KB
/
cat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
#cat-like program
import sys
import re
import os
class Argparse:
available_options = ['-n', '-h', '-e', '-t']
def __init__(self, arg_list):
self.options = []
self.filenames = []
self.invalid_options = []
self.line_numbers = False
self.eol_indicator = False
self.tab_character = False
self.read_stdin = False
option = re.compile('^-.*')
for i in arg_list[1:]:
if option.match(i):
self.options.append(i)
else:
self.filenames.append(i)
for i in self.options:
if i not in self.available_options:
self.invalid_options.append(i)
if self.invalid_options:
print("invalid option: ")
for i in self.invalid_options:
print(i)
print('try cat.py -h for more information')
sys.exit()
if '-h' in self.options:
print(
"""cat-like program to display content of files.Reads from stdin if no file given.
Available options:
-n display line numbers
-e put $ at the end of each line
-t replace TAB characters with ^I
-h display this help text"""
)
sys.exit()
if "-n" in self.options:
self.line_numbers = True
if "-e" in self.options:
self.eol_indicator = True
if "-t" in self.options:
self.tab_character = True
if not self.filenames:
self.read_stdin = True
def cat(arg):
if not arg.read_stdin:
for k in arg.filenames:
try:
f = open(k, "rt")
except FileNotFoundError:
print('Error, No such file as: "', i, '"')
sys.exit(1)
do_cat(f, arg)
f.close()
else:
while True:
try:
text = []
if not os.isatty(0):
for line in sys.stdin:
text.append(line)
else:
text.append(input())
do_cat(text, arg)
if len(text) != 1:
sys.exit(0)
print('')
except KeyboardInterrupt:
print('')
sys.exit(1)
def do_cat(file, arg):
if not arg.read_stdin:
content = file.readlines()
else:
content = file
for i, j in enumerate(content):
if arg.tab_character == False:
line = j
if arg.tab_character:
line = ""
for l in j:
if l == "\t":
line += "^I"
else:
line += l
if arg.line_numbers:
sys.stdout.write(str(i+1))
sys.stdout.write(' ')
if arg.eol_indicator == False:
sys.stdout.write(line)
if arg.eol_indicator:
line_eol = ''
for k in line:
if k != '\n':
line_eol += k
if os.isatty(0):
line_eol += '$'
else:
line_eol += '$\n'
sys.stdout.write(line_eol)
def main():
#print(sys.argv)
arg = Argparse(sys.argv)
cat(arg)
main()