Skip to content

Commit f1b0550

Browse files
committed
A working solution to pass animal names as arguments and make them say things
1 parent 2ca98a4 commit f1b0550

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

implement-cowsay/cow.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import 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)

0 commit comments

Comments
 (0)