File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ import argparse
3+ import cowsay
4+
5+
6+ def main ():
7+ # 1. Get supported animals dynamically from the library
8+ animals = list (cowsay .char_names )
9+
10+ # 2. Set up the argument parser
11+ parser = argparse .ArgumentParser (
12+ description = "Make animals say things"
13+ )
14+
15+ parser .add_argument (
16+ "--animal" ,
17+ choices = animals ,
18+ default = "cow" ,
19+ help = "The animal to be saying things."
20+ )
21+
22+ parser .add_argument (
23+ "message" ,
24+ nargs = "+" ,
25+ help = "The message to say."
26+ )
27+
28+ # 3. Parse arguments
29+ args = parser .parse_args ()
30+
31+ # 4. Combine message words into a single string
32+ text = " " .join (args .message )
33+
34+ # 5. Dynamically call the correct animal function
35+ # e.g. cowsay.cow(text), cowsay.turtle(text), etc.
36+ speaker = getattr (cowsay , args .animal )
37+ print (speaker (text ))
38+
39+
40+ if __name__ == "__main__" :
41+ main ()
You can’t perform that action at this time.
0 commit comments