Skip to content

Commit 860d37b

Browse files
committed
Use argparse and improve wc output formatting
1 parent 6ef3bd5 commit 860d37b

1 file changed

Lines changed: 53 additions & 40 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
import sys
1+
import argparse
22

3-
args = sys.argv[1:]
3+
# Set up command-line argument parser
4+
parser = argparse.ArgumentParser()
45

5-
flags = []
6-
paths = []
6+
# Add supported flags
7+
parser.add_argument("-l", action="store_true")
8+
parser.add_argument("-w", action="store_true")
9+
parser.add_argument("-c", action="store_true")
710

8-
# Separate flags and paths
9-
for arg in args:
10-
if arg.startswith("-"):
11-
flags.append(arg)
12-
else:
13-
paths.append(arg)
11+
# Accept one or more file paths
12+
parser.add_argument("paths", nargs="+")
1413

14+
# Parse the user's command-line arguments
15+
args = parser.parse_args()
1516

16-
total_lines = 0
17-
total_words = 0
18-
total_chars = 0
17+
flags = []
1918

19+
# Store selected flags for the existing logic
20+
if args.l:
21+
flags.append("-l")
2022

21-
for file in paths:
23+
if args.w:
24+
flags.append("-w")
2225

23-
# Read file as bytes
24-
with open(file, "rb") as f:
25-
content = f.read()
26+
if args.c:
27+
flags.append("-c")
2628

27-
# Count
28-
lines = content.count(b"\n")
29-
words = len(content.split())
30-
chars = len(content)
29+
paths = args.paths
3130

32-
# Output for this file
31+
32+
# Build the output based on the selected flags
33+
def get_output(lines, words, chars, flags):
3334
output = []
3435

36+
# Show all counts when no flag is provided
3537
if len(flags) == 0:
3638
output = [lines, words, chars]
3739

@@ -45,30 +47,41 @@
4547
if "-c" in flags:
4648
output.append(chars)
4749

48-
print(*output, file)
50+
return output
4951

50-
# Add to totals
51-
total_lines += lines
52-
total_words += words
53-
total_chars += chars
52+
53+
total_lines = 0
54+
total_words = 0
55+
total_chars = 0
5456

5557

56-
# Print total only when multiple files
57-
if len(paths) > 1:
58+
# Process each file
59+
for file in paths:
5860

59-
total_output = []
61+
# Read file as bytes
62+
with open(file, "rb") as f:
63+
content = f.read()
6064

61-
if len(flags) == 0:
62-
total_output = [total_lines, total_words, total_chars]
65+
# Count lines, words, and characters
66+
lines = content.count(b"\n")
67+
words = len(content.split())
68+
chars = len(content)
6369

64-
else:
65-
if "-l" in flags:
66-
total_output.append(total_lines)
70+
# Create and print output for this file
71+
output = get_output(lines, words, chars, flags)
6772

68-
if "-w" in flags:
69-
total_output.append(total_words)
73+
# Print counts with aligned columns
74+
print(" ".join(f"{value:3}" for value in output), file)
7075

71-
if "-c" in flags:
72-
total_output.append(total_chars)
76+
# Add this file's counts to the totals
77+
total_lines += lines
78+
total_words += words
79+
total_chars += chars
80+
81+
82+
# Print total only when multiple files are provided
83+
if len(paths) > 1:
84+
total_output = get_output(total_lines, total_words, total_chars, flags)
7385

74-
print(*total_output, "total")
86+
# Print aligned total
87+
print(" ".join(f"{value:3}" for value in total_output), "total")

0 commit comments

Comments
 (0)