File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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 ))
You can’t perform that action at this time.
0 commit comments