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
25 changes: 25 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import argparse
import cowsay
import sys

available_animals = cowsay.char_names

parser = argparse.ArgumentParser(
prog="cowsay",
description="Uses the cowsay library to show user-supplied text, as said by a specified animal",
)

parser.add_argument("--animal", help="The animal to be saying things", default="cow", choices=available_animals)
parser.add_argument("message", help="The message to say", nargs="*", default=["Hello, World!"])

args = parser.parse_args()

msg = " ".join(args.message)
animal = args.animal.lower()

if not hasattr(cowsay, animal):
print(f"Invalid choice: '{args.animal}' (choose from: {', '.join(available_animals)})")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you have specified the choices argument in, do you need to include this here as well? If you un with an invalid animal, you'll see some duplication of errors in the output

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @LonMcGregor, i did see that but the example test case in README file % python3 cow.py --animal fish Turtles are cool too! showed the message as such. should i still edit this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot the readme explicitly requested that. In that case, this implementation is good and complete. Well done.

sys.exit(1)

# Dynamically call the cowsay function for the chosen animal
getattr(cowsay, animal)(msg)