|
1 | 1 | import argparse |
2 | | -# ------------------------------------------- |
3 | | -# Set up the argument parser |
4 | | -# ------------------------------------------- |
5 | 2 |
|
6 | | -parser = argparse.ArgumentParser( |
7 | | - prog ="display-file-content", |
8 | | - description = "Implement cat command with -n and -b flag support", |
9 | | - ) |
| 3 | +def parse_args(): |
| 4 | + parser = argparse.ArgumentParser( |
| 5 | + prog ="display-file-content", |
| 6 | + description = "Implement cat command with -n and -b flag support", |
| 7 | + ) |
10 | 8 |
|
11 | | -parser.add_argument("-n", "--number-all-lines", |
12 | | - action="store_true", |
13 | | - help="Number every line in the file" |
14 | | - ) |
| 9 | + parser.add_argument("-n", "--number-all-lines", |
| 10 | + action="store_true", |
| 11 | + help="Number every line in the file" |
| 12 | + ) |
15 | 13 |
|
16 | | -parser.add_argument("-b", "--number-non-empty-lines", |
17 | | - action="store_true", |
18 | | - help="Number non empty lines in the file" |
19 | | - ) |
| 14 | + parser.add_argument("-b", "--number-non-empty-lines", |
| 15 | + action="store_true", |
| 16 | + help="Number non empty lines in the file" |
| 17 | + ) |
20 | 18 |
|
21 | | -parser.add_argument("paths", nargs="+", help="File paths to process") |
| 19 | + parser.add_argument("paths", nargs="+", help="File paths to process") |
22 | 20 |
|
23 | | -args = parser.parse_args() |
| 21 | + args = parser.parse_args() |
| 22 | + return args |
24 | 23 |
|
25 | | -# ------------------------------------------- |
26 | | -# Implement functionality |
27 | | -# ------------------------------------------- |
28 | 24 |
|
29 | | -line_number = 1 |
| 25 | +def print_line(line, line_number = None): |
| 26 | + if line_number is None: |
| 27 | + print(line, end="") |
| 28 | + else: |
| 29 | + print(f"{line_number} {line}", end="") |
30 | 30 |
|
31 | | -for filepath in args.paths: |
| 31 | +def process_line(line, args, line_number): |
| 32 | + if args.number_non_empty_lines: |
| 33 | + if line.strip() == "": |
| 34 | + print_line(line) |
| 35 | + else: |
| 36 | + print_line(line, line_number) |
| 37 | + line_number += 1 |
| 38 | + |
| 39 | + elif args.number_all_lines: |
| 40 | + print_line(line, line_number) |
| 41 | + line_number +=1 |
| 42 | + |
| 43 | + else: |
| 44 | + print_line(line) |
| 45 | + |
| 46 | + return line_number |
| 47 | + |
| 48 | +def process_file(filepath, args, line_number): |
| 49 | + """Read a file and print its lines. Returns updated line_number.""" |
32 | 50 | with open(filepath, "r", encoding="utf-8") as f: |
33 | 51 | for line in f: |
34 | | - if args.number_non_empty_lines: |
35 | | - if line.strip() == "": |
36 | | - print(line, end="") |
37 | | - else: |
38 | | - print(f"{line_number} {line}", end="") |
39 | | - line_number += 1 |
40 | | - |
41 | | - elif args.number_all_lines: |
42 | | - print(f"{line_number} {line}", end="") |
43 | | - line_number +=1 |
44 | | - |
45 | | - else: |
46 | | - print(line, end="") |
| 52 | + line_number = process_line(line, args, line_number) |
| 53 | + return line_number |
| 54 | + |
| 55 | +def main(): |
| 56 | + args = parse_args() |
| 57 | + line_number = 1 |
| 58 | + |
| 59 | + for filepath in args.paths: |
| 60 | + line_number = process_file(filepath, args, line_number) |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + main() |
0 commit comments