Skip to content

Commit 801138c

Browse files
committed
feat:implement cat
1 parent e609ff3 commit 801138c

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog="cat",
5+
description="implement cat",
6+
)
7+
8+
parser.add_argument("paths", nargs="+", help="The file to process")
9+
parser.add_argument("-n", action="store_true", help="Number all lines")
10+
parser.add_argument("-b", action="store_true", help="Number non-empty lines only")
11+
12+
args = parser.parse_args()
13+
counter = 1
14+
15+
for path in args.paths:
16+
with open(path, "r") as f:
17+
for line in f:
18+
if args.n:
19+
print(f"\t{counter} {line}", end="")
20+
counter += 1
21+
elif args.b:
22+
if line.strip() != "":
23+
print(f"\t{counter} {line}", end="")
24+
counter += 1
25+
else:
26+
print(f"\t {line}", end="")
27+
else:
28+
print(f" {line}", end="")

0 commit comments

Comments
 (0)