Skip to content

Commit 234f0a9

Browse files
committed
Cowsay
1 parent f296b70 commit 234f0a9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

implement-cowsay/cow.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cowsay
2+
import argparse
3+
import sys
4+
import os
5+
6+
# Dynamically determine the cows directory
7+
COWS_DIR = os.path.join(os.path.dirname(cowsay.__file__), "cows")
8+
9+
# Set up argument parsing
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument("--animal", required=True)
12+
parser.add_argument("message", nargs="+")
13+
args = parser.parse_args()
14+
15+
# Combine message parts into a single string
16+
message = " ".join(args.message)
17+
18+
# Dynamically fetch available animals
19+
animals = [f[:-4] for f in os.listdir(COWS_DIR) if f.endswith(".cow")]
20+
21+
# Validate the provided animal
22+
if args.animal not in animals:
23+
print("Error: '--animal' is not a valid cowsay animal.")
24+
print("Available animals:", ", ".join(sorted(animals)))
25+
sys.exit(1)
26+
27+
# Just pass the animal name directly
28+
print(cowsay.cowsay(message, cow=args.animal))

0 commit comments

Comments
 (0)