1+ import argparse
2+ parser = argparse .ArgumentParser (
3+ prog = "my-wc" ,
4+ description = "Simple wc clone with -l and -w options" ,
5+ )
6+ lineCounter = 0
7+ wordCounter = 0
8+ charCounter = 0
9+ parser .add_argument ("-l" , action = "store_true" , help = "count lines" )
10+ parser .add_argument ("-w" , action = "store_true" , help = "count words" )
11+ parser .add_argument ("-c" , action = "store_true" , help = "count characters" )
12+ parser .add_argument ("path" , nargs = "+" , default = "." , help = "The file to count" )
13+ args = parser .parse_args ()
14+
15+ if not args .l and not args .w and not args .c :
16+ for file_path in args .path :
17+ with open (file_path , "r" ) as f :
18+ content = f .read ()
19+ arrText = content .split ("\n " )
20+ lineCounter += len (arrText )
21+ arrWords = content .split ()
22+ wordCounter += len (arrWords )
23+ charCounter += len (content )
24+ print ("Line count:" , lineCounter ,"lines" )
25+ print ("Word count:" , wordCounter ,"words" )
26+ print ("Character count:" , charCounter ,"characters" )
27+ elif args .l and args .w :
28+ for file_path in args .path :
29+ with open (file_path , "r" ) as f :
30+ content = f .read ()
31+ arrText = content .split ("\n " )
32+ lineCounter += len (arrText )
33+ arrWords = content .split ()
34+ wordCounter += len (arrWords )
35+ print ("Line count:" , lineCounter ,"lines" )
36+ print ("Word count:" , wordCounter ,"words" )
37+ elif args .l and args .c :
38+ for file_path in args .path :
39+ with open (file_path , "r" ) as f :
40+ content = f .read ()
41+ arrText = content .split ("\n " )
42+ lineCounter += len (arrText )
43+ charCounter += len (content )
44+ print ("Line count:" , lineCounter ,"lines" )
45+ print ("Character count:" , charCounter ,"characters" )
46+ elif args .l :
47+ for file_path in args .path :
48+ with open (file_path , "r" ) as f :
49+ content = f .read ()
50+ arrText = content .split ("\n " )
51+ lineCounter += len (arrText )
52+ print ("Line count:" , lineCounter ,"lines" )
53+
54+ elif args .w :
55+ for file_path in args .path :
56+ with open (file_path , "r" ) as f :
57+ content = f .read ()
58+ arrText = content .split ()
59+ wordCounter += len (arrText )
60+ print ("Word count:" , wordCounter ,"words" )
61+ elif args .c :
62+ for file_path in args .path :
63+ with open (file_path , "r" ) as f :
64+ content = f .read ()
65+ charCounter += len (content )
66+ print ("Character count:" , charCounter ,"characters" )
0 commit comments