|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import sys |
3 | | -import os |
| 3 | +import argparse |
4 | 4 |
|
5 | | -global_line_counter = 1 |
6 | 5 |
|
7 | | -def print_file(file_path, options): |
8 | | - global global_line_counter |
| 6 | +def print_file(file_path, options, counter): |
9 | 7 | try: |
10 | 8 | with open(file_path, "r", encoding="utf-8") as f: |
11 | | - content = f.read() |
| 9 | + for line in f: |
| 10 | + stripped = line.rstrip("\n") |
12 | 11 |
|
13 | | - lines = content.split("\n") |
14 | | - if lines and lines[-1] == "": |
15 | | - lines.pop() |
| 12 | + should_number = options.number_mode == "all" or ( |
| 13 | + options.number_mode == "non-empty" and stripped != "" |
| 14 | + ) |
16 | 15 |
|
17 | | - for line in lines: |
18 | | - prefix = "" |
| 16 | + if should_number: |
| 17 | + print(f"{counter}\t{stripped}") |
| 18 | + counter += 1 |
| 19 | + else: |
| 20 | + print(stripped) |
19 | 21 |
|
20 | | - should_number = ( |
21 | | - options["number_mode"] == "all" or |
22 | | - (options["number_mode"] == "non-empty" and line.strip() != "") |
23 | | - ) |
| 22 | + except FileNotFoundError: |
| 23 | + print(f"cat: {file_path}: No such file or directory", file=sys.stderr) |
24 | 24 |
|
25 | | - if should_number: |
26 | | - prefix = f"{global_line_counter:6}\t" |
27 | | - global_line_counter += 1 |
| 25 | + return counter |
28 | 26 |
|
29 | | - sys.stdout.write(prefix + line + "\n") |
30 | 27 |
|
31 | | - except Exception as e: |
32 | | - print(f"cat: {file_path}: {e}", file=sys.stderr) |
33 | | - sys.exit(1) |
| 28 | +def main(): |
| 29 | + parser = argparse.ArgumentParser(description="Concatenate files and print output") |
| 30 | + parser.add_argument("-n", "--number", action="store_true", help="number all lines") |
34 | 31 |
|
| 32 | + parser.add_argument( |
| 33 | + "-b", "--number-nonblank", action="store_true", help="number non-empty lines" |
| 34 | + ) |
35 | 35 |
|
36 | | -def main(): |
37 | | - global global_line_counter |
38 | | - args = sys.argv[1:] |
39 | | - |
40 | | - options = {"number_mode": "off"} |
41 | | - files = [] |
42 | | - |
43 | | - for arg in args: |
44 | | - if arg == "-n": |
45 | | - options["number_mode"] = "all" |
46 | | - elif arg == "-b": |
47 | | - options["number_mode"] = "non-empty" |
48 | | - else: |
49 | | - files.append(arg) |
50 | | - |
51 | | - if not files: |
52 | | - print("cat: missing file operand", file=sys.stderr) |
53 | | - sys.exit(1) |
54 | | - |
55 | | - for file_path in files: |
56 | | - global_line_counter = 1 |
57 | | - print_file(file_path, options) |
| 36 | + parser.add_argument("files", nargs="+", help="files to read") |
| 37 | + |
| 38 | + args = parser.parse_args() |
| 39 | + |
| 40 | + if args.number_nonblank: |
| 41 | + args.number_mode = "non-empty" |
| 42 | + elif args.number: |
| 43 | + args.number_mode = "all" |
| 44 | + else: |
| 45 | + args.number_mode = "none" |
| 46 | + |
| 47 | + counter = 1 |
| 48 | + for file in args.files: |
| 49 | + counter = print_file(file, args, counter) |
58 | 50 |
|
59 | 51 |
|
60 | 52 | if __name__ == "__main__": |
|
0 commit comments