Skip to content

Commit 4471a05

Browse files
committed
Refactor cat implementationinto functions for readability
1 parent 0a84822 commit 4471a05

File tree

1 file changed

+52
-35
lines changed
  • implement-shell-tools/cat

1 file changed

+52
-35
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,63 @@
11
import argparse
2-
# -------------------------------------------
3-
# Set up the argument parser
4-
# -------------------------------------------
52

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+
)
108

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+
)
1513

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+
)
2018

21-
parser.add_argument("paths", nargs="+", help="File paths to process")
19+
parser.add_argument("paths", nargs="+", help="File paths to process")
2220

23-
args = parser.parse_args()
21+
args = parser.parse_args()
22+
return args
2423

25-
# -------------------------------------------
26-
# Implement functionality
27-
# -------------------------------------------
2824

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="")
3030

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."""
3250
with open(filepath, "r", encoding="utf-8") as f:
3351
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

Comments
 (0)