11import argparse
2+ import cowsay
23import sys
3- from cowpy import cow
4+
5+ available_animals = cowsay .char_names
46
57parser = argparse .ArgumentParser (
68 prog = "cowsay" ,
7- description = "uses the cowsay library to show user-supplied text, as said by a specified animal" ,
9+ description = "Uses the cowsay library to show user-supplied text, as said by a specified animal" ,
810)
911
10- parser .add_argument ("--animal" , help = "The animal to be saying things" , default = "cowacter" )
12+ parser .add_argument ("--animal" , help = "The animal to be saying things" , default = "cow" , choices = available_animals )
1113parser .add_argument ("message" , help = "The message to say" , nargs = "*" , default = ["Hello, World!" ])
1214
1315args = parser .parse_args ()
1416
15-
16- available_animals = {name .lower (): name for name in cow .Cowacter .cowacters } # List of all characters
17-
1817msg = " " .join (args .message )
1918animal = args .animal .lower ()
2019
21- if animal not in available_animals :
22- print (f"Invalid choice: '{ animal } ' (choose from { ', ' .join (available_animals )} )" )
20+ if not hasattr ( cowsay , animal ) :
21+ print (f"Invalid choice: '{ args . animal } ' (choose from: { ', ' .join (available_animals )} )" )
2322 sys .exit (1 )
2423
25- animal_class_name = available_animals [animal ]
26- AnimalClass = getattr (cow , animal_class_name )
27- print (AnimalClass ().milk (msg ))
24+ # Dynamically call the cowsay function for the chosen animal
25+ getattr (cowsay , animal )(msg )
0 commit comments