File tree Expand file tree Collapse file tree 1 file changed +22
-4
lines changed
Expand file tree Collapse file tree 1 file changed +22
-4
lines changed Original file line number Diff line number Diff line change 11import cowsay
2- import sys
3- # Join all arguments after the script name into one string
4- message = " " .join (sys .argv [1 :]) or "MOO MOOO"
5- cowsay .cow (message )
2+ import argparse #helps you collect the words typed in command line
3+
4+ parser = argparse .ArgumentParser (prog = "cowsay" , description = "Make Animals say things" )
5+ parser .add_argument (
6+ "--animal" ,
7+ choices = (cowsay .char_names ),
8+ help = "The animal chosen to be saying things" ,
9+ default = "cow"
10+ )
11+ parser .add_argument (
12+ "message" ,
13+ nargs = "+" , #tells argparse that this argument can accept one or more values and collect them into a list.
14+ help = "The message to say."
15+ )
16+ args = parser .parse_args ()
17+
18+ getattr (cowsay , args .animal )(" " .join (args .message )) #Python built‑in func that get an attribute from an object by name = cowsay module in our case.
19+
20+
21+
22+ #we now need to fetch animals from cowsay.char_names
23+ print (cowsay .char_names )
You can’t perform that action at this time.
0 commit comments