Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

click

00:00 In this lesson, I’ll show you how you can use the click library to build a quick, easy, and powerful version of the seq utility.

00:10 Here is almost the entirety of the code that you need to make an implementation of seq using click as a command line interface.

00:19 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 click.

00:35 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 type=click.INT,

01:10 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.

01:55 So, there’s a little help text, "The separator for the numbers of the sequence: default=\\n"and of course, I’m escaping this backslash ("\") so that it shows up in the actual text.

02:06 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.

02:30 And then there’s one other thing that I can do here that might not be quite as obvious, but I’m going to add it in so that this has a really precise behavior.

02:38 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.

03:18 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.

03:31 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.

03:45 And as you can see, these --version and --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.

03:55 But Python seq_click.py --help, and then the --version is super simple as well.

04:03 It just prints out version 1.0.0, and then I can run it with any of the kinds of options that I’ve put in before. Let me just try a really exciting one right off the bat.

04:14 So, I’ll have a separator and then I’ll say countdown from 100 by -5 to 24. And as you can see, it counts down by 5 perfectly well—this seems to work great.

04:26 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 seq with 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.

04:48 This decorator syntax is just so intuitive that it really seems easy.

04:53 Next up, I’ll show you how to use Python Prompt Toolkit, which is an alternative kind of command line interface.

Become a Member to join the conversation.