From bfd942c4c9fe1421c6868183f672e0f63dafae4e Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 15:34:18 +0100 Subject: [PATCH 1/8] install cowsay --- cow.py | 4 ++++ requirements.txt | 1 + 2 files changed, 5 insertions(+) create mode 100644 cow.py create mode 100644 requirements.txt diff --git a/cow.py b/cow.py new file mode 100644 index 000000000..46dbaf1dd --- /dev/null +++ b/cow.py @@ -0,0 +1,4 @@ +import cowsay +import sys + +cowsay.cow(" ".join(sys.argv[1:])) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..c6b9ffd0e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +cowsay From 5ff78fcf905651dc4e82c3f33dcc5c89e2ba1b8b Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 16:22:31 +0100 Subject: [PATCH 2/8] --animal args limited to cowsay provided options Any invalid argument for the flag will show usage message --- cow.py | 12 +++++++++++- requirements.txt | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cow.py b/cow.py index 46dbaf1dd..4d9becca2 100644 --- a/cow.py +++ b/cow.py @@ -1,4 +1,14 @@ import cowsay import sys +import argparse -cowsay.cow(" ".join(sys.argv[1:])) \ No newline at end of file + +# parse the args +parser = argparse.ArgumentParser( + prog="cowsay command line tool", + description="ASCII art of animals saying text supplied as argument") + +parser.add_argument("--animal", choices=cowsay.char_names) + +args = parser.parse_args() +# cowsay.cow(" ".join(sys.argv[1:])) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c6b9ffd0e..00e2cfc5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ cowsay +argparse \ No newline at end of file From ee78fc7ab4eb76274a7dc4edba26eb682a5827ad Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 16:32:59 +0100 Subject: [PATCH 3/8] add --help flag message cow.py --help shows help message explaining the application, as show in proj requirements. --- cow.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cow.py b/cow.py index 4d9becca2..b62f76681 100644 --- a/cow.py +++ b/cow.py @@ -6,9 +6,10 @@ # parse the args parser = argparse.ArgumentParser( prog="cowsay command line tool", - description="ASCII art of animals saying text supplied as argument") + description="Make animals say things") -parser.add_argument("--animal", choices=cowsay.char_names) +parser.add_argument("--animal", choices=cowsay.char_names, help="The animal to be saying things.") +parser.add_argument("message", nargs='*', help="The message to say") args = parser.parse_args() # cowsay.cow(" ".join(sys.argv[1:])) \ No newline at end of file From eaf7f8d3dd9faabc8194f20eb2f0d592803c35fc Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 17:00:13 +0100 Subject: [PATCH 4/8] completed --- cow.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cow.py b/cow.py index b62f76681..f6bc2a9a8 100644 --- a/cow.py +++ b/cow.py @@ -3,13 +3,27 @@ import argparse -# parse the args +# set up options and argument parser parser = argparse.ArgumentParser( prog="cowsay command line tool", description="Make animals say things") +# takes an --animal option, which takes a limited set of argument - names of characters in cowsay parser.add_argument("--animal", choices=cowsay.char_names, help="The animal to be saying things.") + +# nargs takes a list of multiple (n) args to a list then perform one action. '*' means multiple +# optional arguments, all collected into the message option since this is defined after the --animal option, +# it will gather everything after the argument for the animal option parser.add_argument("message", nargs='*', help="The message to say") +# parses the args into a Namepace class, e.g. Namespace(animal='fox', message=['hello,', 'starfox']) args = parser.parse_args() -# cowsay.cow(" ".join(sys.argv[1:])) \ No newline at end of file + +# extract the aimal argument +animal = vars(args)['animal'] + +# list of positional arguments as space-separated string +message = " ".join(vars(args)['message']) + +# call cowsay with the animal and message +print(cowsay.get_output_string(animal, message)) \ No newline at end of file From 2ceba008d0eba796c2ce85305422f6464e0f81df Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 17:04:15 +0100 Subject: [PATCH 5/8] move the files to the right folder --- cow.py => implement-cowsay/cow.py | 0 requirements.txt => implement-cowsay/requirements.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename cow.py => implement-cowsay/cow.py (100%) rename requirements.txt => implement-cowsay/requirements.txt (100%) diff --git a/cow.py b/implement-cowsay/cow.py similarity index 100% rename from cow.py rename to implement-cowsay/cow.py diff --git a/requirements.txt b/implement-cowsay/requirements.txt similarity index 100% rename from requirements.txt rename to implement-cowsay/requirements.txt From 089631d8dd2098b8af9c88fc203c76376f7afabf Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 17:17:23 +0100 Subject: [PATCH 6/8] fix: no --animal flag prints with cow as animal --- implement-cowsay/cow.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py index f6bc2a9a8..55fb75e35 100644 --- a/implement-cowsay/cow.py +++ b/implement-cowsay/cow.py @@ -22,8 +22,13 @@ # extract the aimal argument animal = vars(args)['animal'] +print(animal) + # list of positional arguments as space-separated string message = " ".join(vars(args)['message']) # call cowsay with the animal and message -print(cowsay.get_output_string(animal, message)) \ No newline at end of file +if (animal): + print(cowsay.get_output_string(animal, message)) +else: + cowsay.cow(message) \ No newline at end of file From 81376304598829b94314958e1e400ccf7854d8d9 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 17:18:55 +0100 Subject: [PATCH 7/8] remove dead code --- implement-cowsay/cow.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py index 55fb75e35..99cd9a7b2 100644 --- a/implement-cowsay/cow.py +++ b/implement-cowsay/cow.py @@ -22,8 +22,6 @@ # extract the aimal argument animal = vars(args)['animal'] -print(animal) - # list of positional arguments as space-separated string message = " ".join(vars(args)['message']) From 6cac76e85f8619900998305043aa67ee13e3c764 Mon Sep 17 00:00:00 2001 From: Raihan Sharif Date: Sun, 2 Aug 2026 17:30:18 +0100 Subject: [PATCH 8/8] tidier implementation --- implement-cowsay/cow.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py index 99cd9a7b2..4cbe9bf6a 100644 --- a/implement-cowsay/cow.py +++ b/implement-cowsay/cow.py @@ -9,24 +9,18 @@ description="Make animals say things") # takes an --animal option, which takes a limited set of argument - names of characters in cowsay -parser.add_argument("--animal", choices=cowsay.char_names, help="The animal to be saying things.") +parser.add_argument("--animal", choices=cowsay.char_names, help="The animal to be saying things.", default="cow") # nargs takes a list of multiple (n) args to a list then perform one action. '*' means multiple # optional arguments, all collected into the message option since this is defined after the --animal option, # it will gather everything after the argument for the animal option -parser.add_argument("message", nargs='*', help="The message to say") +parser.add_argument("message", nargs="*", help="The message to say") # parses the args into a Namepace class, e.g. Namespace(animal='fox', message=['hello,', 'starfox']) args = parser.parse_args() -# extract the aimal argument -animal = vars(args)['animal'] - # list of positional arguments as space-separated string -message = " ".join(vars(args)['message']) +message = " ".join(args.message) # call cowsay with the animal and message -if (animal): - print(cowsay.get_output_string(animal, message)) -else: - cowsay.cow(message) \ No newline at end of file +print(cowsay.get_output_string(args.animal, message))