Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
import argparse
import cowsay


def main():
# 1. Get supported animals dynamically from the library
animals = list(cowsay.char_names)

# 2. Set up the argument parser
parser = argparse.ArgumentParser(
description="Make animals say things"
)

parser.add_argument(
"--animal",
choices=animals,
default="cow",
help="The animal to be saying things."
)

parser.add_argument(
"message",
nargs="+",
help="The message to say."
)

# 3. Parse arguments
args = parser.parse_args()

# 4. Combine message words into a single string
text = " ".join(args.message)

# 5. Dynamically call the correct animal function
# e.g. cowsay.cow(text), cowsay.turtle(text), etc.
speaker = getattr(cowsay, args.animal)
print(speaker(text))


if __name__ == "__main__":
main()