Skip to content

Commit 8e9fd4f

Browse files
committed
Implement cowsay CLI using cowsay library
1 parent 407b010 commit 8e9fd4f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

implement-cowsay/cow.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import cowsay
4+
5+
6+
def main():
7+
# 1. Get supported animals dynamically from the library
8+
animals = list(cowsay.char_names)
9+
10+
# 2. Set up the argument parser
11+
parser = argparse.ArgumentParser(
12+
description="Make animals say things"
13+
)
14+
15+
parser.add_argument(
16+
"--animal",
17+
choices=animals,
18+
default="cow",
19+
help="The animal to be saying things."
20+
)
21+
22+
parser.add_argument(
23+
"message",
24+
nargs="+",
25+
help="The message to say."
26+
)
27+
28+
# 3. Parse arguments
29+
args = parser.parse_args()
30+
31+
# 4. Combine message words into a single string
32+
text = " ".join(args.message)
33+
34+
# 5. Dynamically call the correct animal function
35+
# e.g. cowsay.cow(text), cowsay.turtle(text), etc.
36+
speaker = getattr(cowsay, args.animal)
37+
print(speaker(text))
38+
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)