Understanding Python: Click-based CLI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back to understanding python my name is jake and today we'll be continuing to evolve our pizza builder script by rewriting the cli to use the third party library click click is the library that i use both personally and professionally to write python clies i believe that after this video you'll want to give it a try as well as we go if you learned something new let me know by liking the video now let's get started in our vs code editor i have our art parts version of the pizza builder cli on the right hand side we can take some inspiration from that for implementation using click on the left hand side now since click is a third party library we're going to want to create a virtual environment activate the virtual environment and then use pip to install click into that environment for more on virtual environments you can watch the video that i did on the subject which will be linked in the top right now so first thing we'll move into our click cli folder and then create a new virtual environment and activate it okay now that we are in our new virtual environment we'll go ahead and pip install click and we see that it is successfully installed we'll go ahead and ignore the warnings about older pip versions like everyone else does all right and now that click is installed we can import click let's go ahead and set our interpreter within vs code which we click cli and that suppresses the warning that we saw above all right so since this is building on top of what we did before we can actually copy our sizes and crust from earlier we don't need to recreate those manually so now we have our small medium large extra large and extra extra large sizes as well as our different kinds of crusts normal thin and deep now next thing that we'll want to do is work on our build pizza method so let's copy what we did on the right as a starting point and see how this needs to evolve as we introduce click into the process so the neat thing about click as compared to arc parse is to build your cli you use decorators meaning that instead of defining a parser and adding arguments to that parser and then calling you know explicit parse args and then passing in those parse arguments into something else we can just use decorators to find our cli directly on top of this method and the first decorator we're going to use to do this is at click dot command so we now have click dot command command being the decorator wrapping our build pizza method if you're unsure how decorators work you should see a link pop up on the top right now please give that link a watch as decorators can be very confusing but i think that video will certainly clarify them for you now for those of you that have watched the video and remember how i broke decorators into four different types this click dot command decorator is what i call a type 4 decorator meaning that it can accept different arguments and it will return a different function from what is decorating so build pizza won't be the same build pizza as we saw before we can actually see this in action so i'll comment out this line save it and then we'll load this in iphone okay now we have pizza builder and we have our build pizza function we see that is a function defined under main and if we check the id of build pizza we see it has this id we'll just remember the last four two five two eight now if we were to wrap that in the click dot command decorator so we'll call this build two is equal to click dot command remember that you can also manually call decorators like this passing in build pizza we now have build two which is a different function we see click.decorators.command and so on and so forth we can also check the id of build 2. we see that yes indeed these are actually two separate functions so when we decorate build pizza with click.command we are using a type 4 decorator that will be replacing build pizza with a click command all right back to the file we'll touch more on that in just a few minutes now let's look into actually defining the cli itself on the right we had an argument of size how would we replicate this with click click lets us layer these decorators so while we had click.command we also have click dot argument and this is as we'll see a little bit later is a type 3 decorator so while it will decorate the function from before it will just return a modified version of that function okay and a lot like what we did on the right with arc bars we can specify the name of this argument to be size for the type we're going to use some more click magic we'll say click dot choice and the choices that we're going to allow will be the keys of sizes just like we did before with arc parse and helpfully enough we'll also specify a default and that default will be large awesome so now we have a new argument defined the argument being size it'll give users an option between a single choice of any of the keys within sizes so s m l excel and xxl now here's another area where click differs from arc parse so you may be thinking at this point where psi is going to be stored or how we're going to access the value of size well since click is decorating the function build pizza we'll say that size is a positional argument for build pizza and nicely enough we can then reference size just like this in the function below and then we can do the same exact thing for crusts at click dot argument we want that to be a crust type will be the same thing as before click dot choice and that will be crush dot keys and we can also specify a default of normal just like we did for size we'll put crest as a positional argument get rid of order dot and now our line to build the initial string for pizza is a little bit simpler than what we saw in the auger purse version we just use size as the index for sizes and crust as the index for crusts getting out those values let's pause where we're at right now and take a look at how these arguments are affecting build pizza for demonstration purposes we'll comment those out save go back into the file okay so we still have the original build pizza available and the id of that last four seven nine eight four now if we reply one of these arguments to build pizza again we'll call this build two pass in build pizza we now have build two you can already tell that this is different from what we saw before with click.command if we check the id of build 2 we can see that yes indeed these are the exact same functions so click arguments different from click commands meaning that they are a type 3 decorator so they accept arguments and they return a modified version of the same function we can see that modification if we look under the new dunder attribute click params and we see the first position being argument size and just taking a quick look at all the methods available for argument size we can take a peek at just how much functionality click makes available through the library so through a pretty simple interface of decorators we can build a really complex cli okay back to what we're doing before i'll go ahead and restore those and we'll move on to defining the rest of our cli the next thing that we wanted to do was toppings now toppings are different from size and crust we want size and crust to be passed in as arguments to our script but there's actually a different term for what we want to do with things like toppings extra cheese or extra sauce and click differentiates these as options so much like we did with click.argument we also have click dot option and options actually give us a little bit more functionality than what we had with arguments so kind of like what we did before with toppings in augparse we can use much of the same notation for click so dash t as well as dash dash toppings one being the short form and one being the long form now the type of these will be free from so we'll just specify those as a string now another area where click differs from arg parse is the way that you would specify the number of arguments that you accept click is significantly more opinionated and while you can specify the number of args this is more explicit than what's available in arc parse meaning that you can't just say plus the authors of click determine that having a variable number of arguments being passed into a single option is a bit less deterministic than what would be ideal so we can either specify two for the number of args that means that if someone passes in one topping or three or more toppings it's gonna complain the way to get around this is by using multiple equals true now this does change the call behavior meaning that if we wanted to specify multiple toppings we have to pass those individually for example dash t bacon and dash t onions so this is a bit of a compromise in behavior and finally we can be a little bit lazy by just copying our help text from the right here perfect we now have our first option done just like what we did with size and crust we can specify toppings right here as another positional argument get rid of order.toppings or just toppings and we're continuing on our way let's add our options for extra cheese and extra sauce these will be at click.options as well being extra cheese and then we encounter another deviation from argparse so with arc parts we wanted these to be treated as flags meaning that if you pass that option it'd be treated as the value true and more specifically you'd be stored in the destination variable cheese well we can do this even more simply with click but instead of having to explicitly state a destination variable we just say cheese and then cheese becomes the destination did give us our flag behavior we say is flag is equal to true simple right and then again because i'm lazy we copy our help text add extra cheese to your pizza incredibly easy now just like we did with size crust and toppings we can now add cheese remember that we're storing it to the variable cheese in our positional arguments get rid of order dot cheese for just cheese and then we'll modify that to give us sauce as well just replacing cheese or sauce now there's just a couple more things that we need to do before this script is ready if you look on the right we normally have to parse our args since this is all built on top of our build pizza function we don't explicitly have to call build pizza as a matter of fact we don't even have to print either we can use another function within click so instead of returning pizza we'll simply say click dot echo so what this will do is echo out to the command line and the final touch to make it all work is if name is equal to under main simply call build pizza all right and without further ado let's save and test it out first let's check our help text oh and silly me i made a simple mistake not keeping my parentheses in order save it clear and try again perfect so now we see usage pizza builder tells us we can specify options it gives us the sizes of pizzas as well as the different types of crusts followed by the options down below all beautifully formatted but there's one thing missing in our art parse version we had a nice little description saying welcome to the pizza builder let's build a pizza we don't have that right now in this click version but we can add that really easily we can simply add that as a doc string so if we save that do help again we get our nice dock string message all right let's try this out so we should have defaults for most things so let's give it a run without specifying anything okay and we get a message saying that our pizza is large simple enough we'll specify a different size now we have an extra large pizza we'll add a type of crust we have an extra large thin crust pizza now specified toppings both my usual two toppings here we see that we have an extra large thin crust with bacon and onions we'll go ahead and throw on some extra cheese and now we see plus extra cheese stuff that up for extra sauce and we also have extra sauce in there as well beautiful and taking away any of these options should work until you start violating argument order so here's that built-in handling that click comes with we specified thin but technically the first argument for pizza builder is the size of a pizza regardless of if a default exists if it thinks that you're trying to pass in something as a first positional argument it's going to try to put that in as your first positional argument so if we correct that by putting in small it works again now we can get rid of that second argument just fine and go with just options without any issues if we try to specify something different for the type of pizza we'll get the same thing invalid value for normal thin or deep yes is not one of normal thin or deep that's how those choices work and if you throw on a little something extra at the end like something it will do the error handling for that as well well that wraps up this video now that you understand how to use click to generate a cli you can easily incorporate them in your own projects what is your favorite trick with click or was it something i showed today leave a comment down below to let me know as always today's code will be added to the understanding github repo so check the description for a link and of course if you have any questions or suggestions for topics you'd like me to cover leave a comment for me to keep up with the series please consider subscribing thanks for watching [Music] you
Info
Channel: Jake Callahan
Views: 45
Rating: undefined out of 5
Keywords: python, python cli, python programming, cli, python tutorial, python for beginners, python (programming language), python click, python 3, building command line applications with click, python command line interface, registration cli in python, python argparse, python command line arguments, argparse for cli - intermediate python programming p.3, click python tutorial, writing awesome command-line programs in python, python fire cli tutorial, click, learn python
Id: TVFO2ABZqK8
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.