|
1 | | -import sys |
| 1 | +import argparse |
2 | 2 |
|
3 | | -args = sys.argv[1:] |
| 3 | +# Set up command-line argument parser |
| 4 | +parser = argparse.ArgumentParser() |
4 | 5 |
|
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") |
7 | 10 |
|
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="+") |
14 | 13 |
|
| 14 | +# Parse the user's command-line arguments |
| 15 | +args = parser.parse_args() |
15 | 16 |
|
16 | | -total_lines = 0 |
17 | | -total_words = 0 |
18 | | -total_chars = 0 |
| 17 | +flags = [] |
19 | 18 |
|
| 19 | +# Store selected flags for the existing logic |
| 20 | +if args.l: |
| 21 | + flags.append("-l") |
20 | 22 |
|
21 | | -for file in paths: |
| 23 | +if args.w: |
| 24 | + flags.append("-w") |
22 | 25 |
|
23 | | - # Read file as bytes |
24 | | - with open(file, "rb") as f: |
25 | | - content = f.read() |
| 26 | +if args.c: |
| 27 | + flags.append("-c") |
26 | 28 |
|
27 | | - # Count |
28 | | - lines = content.count(b"\n") |
29 | | - words = len(content.split()) |
30 | | - chars = len(content) |
| 29 | +paths = args.paths |
31 | 30 |
|
32 | | - # Output for this file |
| 31 | + |
| 32 | +# Build the output based on the selected flags |
| 33 | +def get_output(lines, words, chars, flags): |
33 | 34 | output = [] |
34 | 35 |
|
| 36 | + # Show all counts when no flag is provided |
35 | 37 | if len(flags) == 0: |
36 | 38 | output = [lines, words, chars] |
37 | 39 |
|
|
45 | 47 | if "-c" in flags: |
46 | 48 | output.append(chars) |
47 | 49 |
|
48 | | - print(*output, file) |
| 50 | + return output |
49 | 51 |
|
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 |
54 | 56 |
|
55 | 57 |
|
56 | | -# Print total only when multiple files |
57 | | -if len(paths) > 1: |
| 58 | +# Process each file |
| 59 | +for file in paths: |
58 | 60 |
|
59 | | - total_output = [] |
| 61 | + # Read file as bytes |
| 62 | + with open(file, "rb") as f: |
| 63 | + content = f.read() |
60 | 64 |
|
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) |
63 | 69 |
|
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) |
67 | 72 |
|
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) |
70 | 75 |
|
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) |
73 | 85 |
|
74 | | - print(*total_output, "total") |
| 86 | + # Print aligned total |
| 87 | + print(" ".join(f"{value:3}" for value in total_output), "total") |
0 commit comments