Skip to content

Commit f2ad18a

Browse files
committed
Add -l -w -c flags to wc
1 parent 65425b4 commit f2ad18a

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@
22

33
args = sys.argv[1:]
44

5-
for path in args:
5+
option = "all"
6+
paths = []
7+
8+
for arg in args:
9+
if arg == "-l":
10+
option = "l"
11+
elif arg == "-w":
12+
option = "w"
13+
elif arg == "-c":
14+
option = "c"
15+
else:
16+
paths.append(arg)
17+
18+
for path in paths:
619
with open(path, "r") as file:
720
content = file.read()
821

9-
lines = content.split("\n")
10-
words = content.split(" ")
11-
chars = content
22+
lines = len(content.split("\n"))
23+
words = len(content.split(" "))
24+
chars = len(content)
1225

13-
print(len(lines), len(words), len(chars), path)
26+
if option == "l":
27+
print(lines, path)
28+
elif option == "w":
29+
print(words, path)
30+
elif option == "c":
31+
print(chars, path)
32+
else:
33+
print(lines, words, chars, path)

0 commit comments

Comments
 (0)