Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import cowsay
import sys
import argparse


# 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.", 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")

# parses the args into a Namepace class, e.g. Namespace(animal='fox', message=['hello,', 'starfox'])
args = parser.parse_args()

# list of positional arguments as space-separated string
message = " ".join(args.message)

# call cowsay with the animal and message
print(cowsay.get_output_string(args.animal, message))
2 changes: 2 additions & 0 deletions implement-cowsay/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cowsay
argparse