Code Main and Arguments
If what you’re hoping to build is a command line program, you need a way of specifying all those arguments to the
curses view class. I’ll be using
argparse to handle the following command line arguments.
00:22 This displays the program’s version, allows you to choose a pattern or loop through all the patterns. This specifies the view class. I’m not going to implement other views, but this future-proofs it for your homework.
00:59 It currently defines a single function that gets called by the program to register all the args. The first thing this function does is create the parser by instantiating an instance of argument parser.
This takes two parameters, the name of the program and the description string used in the help message. Once the parser is in place, I call
add_argument() for each command line argument to be supported by the program.
The first argument is version. This uses
argparse’s version action, which knows to print info to the screen and exit. The version information here is loaded from the
__version__ variable, which I’ll add to
__init__ when I’m done here.
01:55 The next argument allows the user to specify the initial pattern for the grid. This argument takes the name of a pattern and the choices parameter here restricts the allowed names to those returned by the get all patterns method in our patterns file.
02:11 Note that at the moment there is no way to specify an external data file. If you want to add that feature later, you’ll have to muck with this call as you’d have a chicken and egg problem of defining the choices and saying which file to look for them in. Ignoring our chickens and moving on.
Or you can be lazy like I am, and if
--all is present, just ignore if
--pattern is there as well. Using the
store_true action here specifies this flag acts as a Boolean and doesn’t require any arguments after it.
__version__ in the module’s
__init__ file is a very common pattern. This value serves another purpose as well. When it comes time to package this program up, this variable can also be used to give the package number.
Okay, arguments have been prepared. Let’s actually wrap this sucker up in a program script. To do so, you need an entry point for the program. Then inside there you use that function I just showed you in
The main method is what the module calls when it is loaded. More on that in a second. Its first task is to process the command line args. That was all taken care of in
cli.py, and now using the view argument from the argument parser, load that given view class.
And what does
show_pattern() do? I thought you’d never ask. Similar to what was done in the REPL at the end of the last lesson, it instantiates the view, passes in the pattern and the remaining command line arguments, and calls
show(). Code in a module gets run as soon as the module gets loaded.
Naming the entry point
__main__ is pretty common. If this module is run as a script, our
main() function gets called. Yep. That’s the
main() function in the file called
__main__, under a
__name__ check for
Become a Member to join the conversation.