Skip to content

Commit 9b645e6

Browse files
committed
implementing flags
1 parent b41d36a commit 9b645e6

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import os
33

44
parser=argparse.ArgumentParser(prog="wc",usage="implement a simple wc in python")
5+
parser.add_argument("-l",action="store_true",help="count of lines")
6+
parser.add_argument("-w",action="store_true",help="count of words")
7+
parser.add_argument("-c",action="store_true",help="count of bytes")
58
parser.add_argument("path",nargs="+")
69
args=parser.parse_args()
710

8-
# print(args.path)
11+
print(args)
912
paths=args.path
1013
words_count=0
1114
total_lines=0
@@ -21,8 +24,15 @@
2124
words_count+=len(line.split())
2225
total_lines +=lines_count
2326
total_bytes += bytes_count
24-
total_words +=words_count
25-
print(f"{lines_count:<5}{words_count:<5}{bytes_count:<5}{file:<20}")
27+
total_words +=words_count
28+
if not (args.l or args.w or args.c):
29+
args.l = True
30+
args.w = True
31+
args.c = True
32+
print((f"{lines_count:<5}" if args.l else "")+
33+
(f"{words_count:<5}" if args.w else "")+
34+
(f"{bytes_count:<5}" if args.c else "")+
35+
f"{file:<20}")
2636
words_count=0
2737

2838
print(f"{total_lines:<5}{total_words:<5}{total_bytes:<5}total")

0 commit comments

Comments
 (0)