-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc_cli.py
179 lines (143 loc) · 5.02 KB
/
wc_cli.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
def print_wc_output(counts, filepath):
for count in counts:
spacing = " "*(8 - len(str(count)))
print(spacing + str(count), end="")
if(filepath != None):
print(" " + filepath)
else: print("")
def parse_arguments(argv):
argv.pop(0)
files = []
useDefaultOptions = True
options = {'c': False, 'm': False, 'l': False, 'w': False}
for arg in argv:
if (arg.startswith("-")):
useDefaultOptions = False
optionsToAdd = list(arg[1:])
for option in optionsToAdd:
if(option == 'c'): options['m'] = False
elif (option == 'm'): options['c'] = False
elif (option != 'w' and option != 'l'):
print("wc-cli: illegal option -- " + option)
print("usage: wc [-clmw] [file ...] ")
exit()
options[option] = True
else:
file = arg
files.append(file)
if(useDefaultOptions):
options = {'c': True, 'm': False, 'l': True, 'w': True}
return [options, files]
def read_binary_file(filepath, options):
try:
file = open(filepath, "rb")
line_count = 0
byte_count = 0
word_count = 0
line = file.readline()
while(line):
if(options['l']): line_count = line_count + 1
if(options['c']): byte_count = byte_count + len(line)
if(options['w']):
words = line.strip().split()
word_count = word_count + len(words)
line = file.readline()
file.close()
counts = []
if(options['l']): counts.append(line_count)
if(options['w']): counts.append(word_count)
if(options['c']): counts.append(byte_count)
print_wc_output(counts, filepath)
return counts
except Exception as e:
print("wc-cli: " + filepath + ": " + e.args[1])
return None
def read_text_file(filepath, options):
try:
file = open(filepath)
line = file.readline()
line_count = 0
char_count = 0
word_count = 0
while(line):
if(options['l']): line_count = line_count + 1
if(options['m']): char_count = char_count + len(line) + 1
if(options['w']):
words = line.strip().split()
word_count = word_count + len(words)
line = file.readline()
file.close()
counts = []
if(options['l']): counts.append(line_count)
if(options['w']): counts.append(word_count)
if(options['m']): counts.append(char_count)
print_wc_output(counts, filepath)
return counts
except Exception as e:
print("wc-cli: " + filepath + ": " + e.args[1])
return None
def read_binary_stdin(options):
try:
line_count = 0
byte_count = 0
word_count = 0
for line in sys.stdin.buffer:
if(options['l']): line_count = line_count + 1
if(options['c']): byte_count = byte_count + len(line)
if(options['w']):
words = line.strip().split()
word_count = word_count + len(words)
counts = []
if(options['l']): counts.append(line_count)
if(options['w']): counts.append(word_count)
if(options['c']): counts.append(byte_count)
print_wc_output(counts, None)
return counts
except Exception as e:
print("wc-cli: " + e.args[1])
return None
def read_text_stdin(options):
try:
line_count = 0
char_count = 0
word_count = 0
for line in sys.stdin:
if(options['l']): line_count = line_count + 1
if(options['m']): char_count = char_count + len(line)
if(options['w']):
words = line.strip().split()
word_count = word_count + len(words)
counts = []
if(options['l']): counts.append(line_count)
if(options['w']): counts.append(word_count)
if(options['m']): counts.append(char_count)
print_wc_output(counts, None)
return counts
except Exception as e:
print("wc-cli: " + e.args[1])
return None
[options, files] = parse_arguments(sys.argv)
if(len(files) == 0):
if(options['m']):
read_text_stdin(options)
else:
read_binary_stdin(options)
all_counts = []
for filepath in files:
counts = []
if(options['m']):
counts = read_text_file(filepath, options)
else:
counts = read_binary_file(filepath, options)
all_counts.append(counts)
if(len(all_counts) > 1):
all_counts = [counts for counts in all_counts if counts is not None]
counts_len = len(all_counts[0])
total_counts = []
for i in range(counts_len):
total_count = 0
for counts in all_counts:
total_count = total_count + counts[i]
total_counts.append(total_count)
print_wc_output(total_counts, "total")