Now, you might be thinking, “What? That’s crazy.” This is just the
seq() function, and then you’re just calling it in
__main__. And to be fair, I have left something really important out but, really, this is all of the actual logic in your code you need to build a command line interface with
The secret to making this work is just that
click uses decorators to accomplish the actual work of taking in command line arguments and converting them to the required Python code. For example, in order to get access to the operands I can just say
@click.argument() and then I pass in the name
"operands", which will map what I’m doing here to this actual
operands parameter of the
seq() function. Then, I can say
and I can say number of arguments
nargs=-1, and that means any number of arguments—so essentially, this just means a list of integers. Now, if I want to handle the separator option, I can do a very similar thing where I say
@click.option()‚ and then this requires a tiny bit more work. I just say here
"separator", and then, I can also pass in flag
"-s"—and I need to include the double hyphens here in the
"--separator" as well. And then from there, it’s as simple as giving it a
default—and I’ll use a little more space here—so,
default = "\n" (newline), and then I’ll also put in a help text.
So, I have all of the argument and option logic handled just with this simple thing here. It’s amazing. And then I can do a little bit more work here and I can say
@click.version_option(), and I’ll just say
version="1.0.0" and that needs to be a string, of course, because
1.0.0 isn’t a real number in Python.
I’m also going to add a
@click.command(), which is simply a directive for how
click should operate as the command line interface. And this command will say
context_settings= a dictionary of
ignore_unknown_options=true—and of course,
True, as a real Python Boolean.
03:01 And what this is actually just going to do is it’s going to say, “If you see something that looks like an option but you weren’t expecting it, just ignore it.” And the reason for that, of course, is so that I can enter in negative numbers to the command line interface because otherwise, a negative number might be interpreted as a flag.
So, this is just a little convenience thing that
click offers you to make that distinction. But look how simple that was. It really required almost no work at all. And as you’ll see in a second, it works just great.
So, I’ll try this first with the
--help option, so
seq_click.py and then
--help. And as you’ll notice, it has a great little help text here with a usage message, and then it lists the different options that are possible.
And as you can see, these
--help, by default, only have this double hyphen syntax, but if you want to peruse the documentation, of course, there are ways to get the short option for those as well.
And it works with all the different numbers of options that I’ve specified so far, and it seems to do just a good job. So that’s
click, and as you can see,
click is super simple but also super powerful, so don’t let yourself be tempted to think that because of its simplicity,
click lacks some kind of power or versatility, because it really doesn’t.
Become a Member to join the conversation.