Often getting the right data into your program is a real chore. The |
|
In Chapter 1, we ended up writing a very flexible program that could extend warm salutations to an optionally named entity such as the "World" or "Universe":
$ ./hello.py Hello, World! $ ./hello.py --name Universe Hello, Universe!
The program would respond to the -h
and --help
flags with helpful documentation:
$ ./hello.py -h usage: hello.py [-h] [-n str] Say hello optional arguments: -h, --help show this help message and exit -n str, --name str The name to greet (default: World)
The argparse
module helped us define a parser for the parameters and generate the usage, saving us loads of time and making our program look professional. Every program in this book is tested on different inputs, so you’ll really understand how to use this module by the end. I would recommend you look over the documentation (docs.python.org/3/library/argparse.html). Now let’s dig further into what this module can do for us. In this chapter, we will: