Skip to content

Commit 510d424

Browse files
Add Python cowsay CLI with animal selection and validation
1 parent 2567a98 commit 510d424

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

implement-cowsay/cowsay_script.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import cowsay
4+
import sys
5+
6+
def main():
7+
parser = argparse.ArgumentParser(
8+
description="Make animals say things"
9+
)
10+
parser.add_argument(
11+
"message", nargs="+", help="The message to say."
12+
)
13+
parser.add_argument(
14+
"--animal",
15+
default="cow",
16+
help="The animal to be saying things."
17+
)
18+
19+
args = parser.parse_args()
20+
21+
text = " ".join(args.message)
22+
animal = args.animal
23+
24+
supported_animals = [
25+
"beavis", "cheese", "cow", "daemon", "dragon", "fox",
26+
"ghostbusters", "kitty", "meow", "miki", "milk", "octopus",
27+
"pig", "stegosaurus", "stimpy", "trex", "turkey", "turtle", "tux"
28+
]
29+
30+
if animal not in supported_animals:
31+
print(f"usage: cowsay [-h] [--animal {{{','.join(supported_animals)}}}] message [message ...]")
32+
print(f"cowsay: error: argument --animal: invalid choice: '{animal}' (choose from {','.join(supported_animals)})")
33+
sys.exit(1)
34+
35+
output = cowsay.get_output_string(animal, text)
36+
print(output)
37+
38+
if __name__ == "__main__":
39+
main()

0 commit comments

Comments
 (0)