File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
implement-shell-tools/cat Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+
3+ parser = argparse .ArgumentParser (
4+ prog = "cat shell tool" ,
5+ description = "making cat tool with oython"
6+ )
7+
8+ parser .add_argument (
9+ "files" ,
10+ nargs = "+" ,
11+ help = "file to read"
12+ )
13+
14+ parser .add_argument (
15+ "-n" ,
16+ "--number" ,
17+ action = "store_true" ,
18+ help = "add number to the lines"
19+ )
20+
21+ parser .add_argument (
22+ "-b" ,
23+ "--number_nonblank" ,
24+ action = "store_true" ,
25+ help = "does not number the blank lines"
26+ )
27+
28+ args = parser .parse_args ()
29+
30+ number_line = 0
31+ for file in args .files :
32+ with open (file , "r" ) as f :
33+ lines = f .readlines ()
34+
35+ for line in lines :
36+ if args .number_nonblank :
37+ if line .strip ():
38+ number_line += 1
39+ print (f"{ number_line } { line } " , end = "" )
40+ elif args .number :
41+ number_line += 1
42+ print (f"{ number_line } { line } " , end = "" )
43+ else :
44+ print (line )
45+
46+
47+
48+
Original file line number Diff line number Diff line change 1+
You can’t perform that action at this time.
0 commit comments