|
1 | 1 | import argparse |
| 2 | +import os |
2 | 3 |
|
3 | 4 | parser = argparse.ArgumentParser( |
4 | 5 | prog="py-wc", |
5 | 6 | description="A Python implementation of the Unix wc command", |
6 | 7 | ) |
7 | 8 |
|
8 | | -parser.add_argument("-l", "--lines", help="Print the newline counts") |
| 9 | +parser.add_argument("-l", "--lines", action="store_true", help="Print the newline counts") |
9 | 10 | parser.add_argument("-w", "--words", action="store_true", help="Print the word counts") |
10 | 11 | parser.add_argument("-c", "--bytes", action="store_true", help="Print the byte counts") |
11 | | -parser.add_argument("path", nargs="?", default=".", help="Directory to list") |
| 12 | +parser.add_argument("path", nargs="+", help="The file path to process") |
12 | 13 |
|
13 | 14 | args = parser.parse_args() |
| 15 | +file_paths = args.path |
| 16 | + |
| 17 | +file_details_list = [] |
| 18 | + |
| 19 | +# Get file details |
| 20 | +for file_path in file_paths: |
| 21 | + with open(file_path, "r", encoding="utf-8") as f: |
| 22 | + content = f.read() |
| 23 | + |
| 24 | + details = { |
| 25 | + "file_path": file_path, |
| 26 | + "file_size": os.path.getsize(file_path), |
| 27 | + "line_count": content.count("\n"), # matches real wc -l |
| 28 | + "word_count": len(content.split()), # split on whitespace |
| 29 | + } |
| 30 | + |
| 31 | + file_details_list.append(details) |
| 32 | + |
| 33 | + |
| 34 | +print(file_details_list) |
| 35 | + |
| 36 | +def get_line_count(text): |
| 37 | + return text.count("\n") |
| 38 | + |
| 39 | + |
| 40 | +def get_word_count(text): |
| 41 | + return len(text.split()) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +show_all = not (args.lines or args.words or args.bytes) |
| 46 | + |
| 47 | + |
| 48 | +# print(args) |
14 | 49 |
|
0 commit comments