Skip to content

Commit eaf7f8d

Browse files
committed
completed
1 parent ee78fc7 commit eaf7f8d

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

cow.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
import argparse
44

55

6-
# parse the args
6+
# set up options and argument parser
77
parser = argparse.ArgumentParser(
88
prog="cowsay command line tool",
99
description="Make animals say things")
1010

11+
# takes an --animal option, which takes a limited set of argument - names of characters in cowsay
1112
parser.add_argument("--animal", choices=cowsay.char_names, help="The animal to be saying things.")
13+
14+
# nargs takes a list of multiple (n) args to a list then perform one action. '*' means multiple
15+
# optional arguments, all collected into the message option since this is defined after the --animal option,
16+
# it will gather everything after the argument for the animal option
1217
parser.add_argument("message", nargs='*', help="The message to say")
1318

19+
# parses the args into a Namepace class, e.g. Namespace(animal='fox', message=['hello,', 'starfox'])
1420
args = parser.parse_args()
15-
# cowsay.cow(" ".join(sys.argv[1:]))
21+
22+
# extract the aimal argument
23+
animal = vars(args)['animal']
24+
25+
# list of positional arguments as space-separated string
26+
message = " ".join(vars(args)['message'])
27+
28+
# call cowsay with the animal and message
29+
print(cowsay.get_output_string(animal, message))

0 commit comments

Comments
 (0)