A sub-parser gives you the ability to write commands within your script. This allows you to write scripts that have different actions. depending on the arguments, like
git does with
status, and its other commands.
A common pattern is to associate a function with each subcommand. Rather than writing a giant
else block of all the possible commands, you can use the
.set_defaults() allows you to set values onto the namespace explicitly.
The first one is
raiders(), and it takes
args as a parameter. This isn’t required by the sub-parser, but it is good practice when using the methodology that I’m going to be showing you. By passing in the namespace object, your subcommand can get at any parameters that were passed to it.
raiders() subcommand prints out a quote and uses the
animal argument from the
args namespace. The
doom() command is similar. As I’m passing in
args, I can use any of the argument definitions you’ve seen up until now. The
doom() subcommand uses a
title parameter is used in the help message, and the
dest attribute indicates where to store the subcommand name. As I only want the user using the sub-parser and nothing else, I’ve set it to be required. Now to add the subcommands.
It’s always snakes. Here’s that
func trick I so badly attempted to explain before. The
.set_defaults() method on any parser, sub-parser or otherwise, sets one or more name-value pairs on the namespace. Here, I’m setting something called
func with a value of the
That’s a reference to the function itself. In a few lines, I’ll be using this default argument to invoke my subcommand function. For the
doom command, I do something similar: another sub-parser, a new argument—this time, for repeating—and I use the
.set_defaults() to point the
func argument at the
doom function. I also snuck something extra in here.
metavar parameter to the
.add_argument() method changes the name of the variable in the help message. Without this, you’d see
-r and then
REPEAT in all caps. With it, you see
NUM in all caps.
And the third subcommand, in the same vein: new parser, new argument, new association for the
func argument, this time pointing to the
crusade function. Scrolling down … Line 37 is no different than any other parser you’ve built.
The magic is on line 38. Line 37 creates the namespace like usual. depending on what subcommand was invoked, a different
.set_defaults() method will have been called, changing the value of
func inside of the namespace.
This overwriting mechanism means you can take the value of
func and invoke it as a function. Each subcommand was associated with a different function at the top of the script. So this call will invoke the corresponding function. Tricky, huh?
By putting the
-h after the
doom subcommand, you get the
doom subcommand’s help info. This is where that
metavar parameter comes into play. Without it, the
NUM here would say
REPEAT in all caps. That probably have been a better choice, but then I wouldn’t have been able to demonstrate
metavar. Don’t forget, you can also set the
help parameter in the
.add_argument() method to put more detailed information on this line, as well.
Become a Member to join the conversation.