Skip to content

Commit 6f5e3dd

Browse files
committed
Implement the basic program.
1 parent 655d823 commit 6f5e3dd

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,50 @@
55

66
parser.add_argument("-w",
77
"--words",
8-
dest="words_count",
8+
dest="show_words",
99
action="store_true",
1010
help="print word count")
1111

1212
parser.add_argument("-l",
1313
"--lines",
14-
dest="lines_count",
14+
dest="show_lines",
1515
action="store_true",
1616
help="print line count")
1717

1818
parser.add_argument("-c",
1919
"--bytes",
20-
dest="byte_count",
20+
dest="show_bytes",
2121
action="store_true",
2222
help="print byte count")
2323

2424
parser.add_argument("path")
2525

2626
args = parser.parse_args()
27+
28+
if not args.show_lines and not args.show_words and not args.show_bytes:
29+
args.show_lines = True
30+
args.show_words = True
31+
args.show_bytes = True
32+
33+
with open(args.path, "rb") as file:
34+
content = file.read()
35+
36+
word_count = str(len(content.split()))
37+
line_count = str(content.count(b"\n"))
38+
byte_count = str(len(content))
39+
40+
outputs = []
41+
42+
if args.show_lines:
43+
outputs.append(line_count)
44+
45+
if args.show_words:
46+
outputs.append(word_count)
47+
48+
if args.show_bytes:
49+
outputs.append(byte_count)
50+
51+
outputs.append(args.path)
52+
53+
output = "\t".join(outputs)
54+
print(output)

0 commit comments

Comments
 (0)