From e7bf176e1d4593b10f87443861c164c3fcc5b0e1 Mon Sep 17 00:00:00 2001 From: mikiyas-stp Date: Mon, 11 Aug 2025 21:25:55 +0100 Subject: [PATCH 1/2] cowsay implementation using testapi file as a source file where i search for animals : inside the cowsay>test>testapi.py --- implement-cowsay/.gitignore | 1 + implement-cowsay/anisay.py | 27 +++++++++++++++++++++++++++ implement-cowsay/requirements.txt | 1 + 3 files changed, 29 insertions(+) create mode 100644 implement-cowsay/.gitignore create mode 100644 implement-cowsay/anisay.py create mode 100644 implement-cowsay/requirements.txt diff --git a/implement-cowsay/.gitignore b/implement-cowsay/.gitignore new file mode 100644 index 00000000..b694934f --- /dev/null +++ b/implement-cowsay/.gitignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/implement-cowsay/anisay.py b/implement-cowsay/anisay.py new file mode 100644 index 00000000..6b2f97ae --- /dev/null +++ b/implement-cowsay/anisay.py @@ -0,0 +1,27 @@ +import argparse +import cowsay + +def main(): + animals = cowsay.char_names + parser = argparse.ArgumentParser( + description="Make animals say things!" + ) + parser.add_argument( + '--animal', '-a', + choices=animals, + default='cow', + help='The animal to be saying things (default: cow)' + ) + parser.add_argument('message',nargs='+',help='The message to say') + args = parser.parse_args() + text = ' '.join(args.message) + + try: + output = cowsay.get_output_string(char=args.animal, text=text) + print(output) + except cowsay.CowsayError as e: + print(f"Error: {e}") + exit(1) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/implement-cowsay/requirements.txt b/implement-cowsay/requirements.txt new file mode 100644 index 00000000..cc557103 --- /dev/null +++ b/implement-cowsay/requirements.txt @@ -0,0 +1 @@ +cowsay \ No newline at end of file From b16105b9bf0672977c4a79380889edce02c2d79b Mon Sep 17 00:00:00 2001 From: mikiyas-stp Date: Mon, 11 Aug 2025 21:32:54 +0100 Subject: [PATCH 2/2] edited code for better readability --- implement-cowsay/anisay.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/implement-cowsay/anisay.py b/implement-cowsay/anisay.py index 6b2f97ae..c1f93ab6 100644 --- a/implement-cowsay/anisay.py +++ b/implement-cowsay/anisay.py @@ -1,8 +1,10 @@ import argparse -import cowsay +import cowsay #to do the ascii art with animals def main(): + #getlistof animals within cowsay animals = cowsay.char_names + #create a parser for command line argument parser = argparse.ArgumentParser( description="Make animals say things!" ) @@ -12,8 +14,11 @@ def main(): default='cow', help='The animal to be saying things (default: cow)' ) + #argument for the message parser.add_argument('message',nargs='+',help='The message to say') + #to parse the arguments from the command line args = parser.parse_args() + #the messageword into a single string text = ' '.join(args.message) try: @@ -22,6 +27,6 @@ def main(): except cowsay.CowsayError as e: print(f"Error: {e}") exit(1) - +#run main function only if this script is run directly if __name__ == '__main__': main() \ No newline at end of file