|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import glob |
| 4 | +import os |
| 5 | + |
| 6 | +def count_lines(text): |
| 7 | + if not text: |
| 8 | + return 0 |
| 9 | + lines = text.splitlines() |
| 10 | + return len(lines) |
| 11 | + |
| 12 | +def count_words(text): |
| 13 | + return len([w for w in text.split() if w]) |
| 14 | + |
| 15 | +def count_chars(text): |
| 16 | + return len(text.encode('utf-8')) |
| 17 | + |
| 18 | +def wc_file(filename, options): |
| 19 | + try: |
| 20 | + with open(filename, "r", encoding="utf-8") as f: |
| 21 | + text = f.read() |
| 22 | + except FileNotFoundError: |
| 23 | + print(f"wc: {filename}: No such file") |
| 24 | + return None |
| 25 | + |
| 26 | + lines = count_lines(text) |
| 27 | + words = count_words(text) |
| 28 | + chars = count_chars(text) |
| 29 | + |
| 30 | + padding = 7 |
| 31 | + if options.lines and not options.words and not options.chars: |
| 32 | + output = f"{lines} {filename}" |
| 33 | + elif options.words and not options.lines and not options.chars: |
| 34 | + output = f"{words} {filename}" |
| 35 | + elif options.chars and not options.lines and not options.words: |
| 36 | + output = f"{chars} {filename}" |
| 37 | + else: |
| 38 | + output = f"{str(lines).rjust(padding)} {str(words).rjust(padding)} {str(chars).rjust(padding)} {filename}" |
| 39 | + |
| 40 | + print(output) |
| 41 | + return {"lines": lines, "words": words, "chars": chars} |
| 42 | + |
| 43 | +def main(): |
| 44 | + parser = argparse.ArgumentParser(description="Custom implementation of wc") |
| 45 | + parser.add_argument("-l", "--lines", action="store_true", help="count lines") |
| 46 | + parser.add_argument("-w", "--words", action="store_true", help="count words") |
| 47 | + parser.add_argument("-c", "--chars", action="store_true", help="count characters") |
| 48 | + parser.add_argument("files", nargs="+", help="files or wildcard patterns") |
| 49 | + args = parser.parse_args() |
| 50 | + |
| 51 | + all_files = [] |
| 52 | + for pattern in args.files: |
| 53 | + expanded = glob.glob(pattern) |
| 54 | + if not expanded: |
| 55 | + print(f"wc: {pattern}: No such file or directory") |
| 56 | + all_files.extend(expanded) |
| 57 | + |
| 58 | + total_lines = total_words = total_chars = 0 |
| 59 | + |
| 60 | + for file in all_files: |
| 61 | + result = wc_file(file, args) |
| 62 | + if result: |
| 63 | + total_lines += result["lines"] |
| 64 | + total_words += result["words"] |
| 65 | + total_chars += result["chars"] |
| 66 | + |
| 67 | + padding = 7 |
| 68 | + if len(all_files) > 1: |
| 69 | + if args.lines and not args.words and not args.chars: |
| 70 | + print(f"{total_lines} total") |
| 71 | + elif args.words and not args.lines and not args.chars: |
| 72 | + print(f"{total_words} total") |
| 73 | + elif args.chars and not args.lines and not args.words: |
| 74 | + print(f"{total_chars} total") |
| 75 | + else: |
| 76 | + print(f"{str(total_lines).rjust(padding)} {str(total_words).rjust(padding)} {str(total_chars).rjust(padding)} total") |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments