how to build a nice command line interface (quick and easy) with python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this bite size tutorial we are going to create a nice maintainable command line interface for a python program in just a few lines let's code [Music] hi i'm rafael and this is codercave if you're interested in software development consider subscribing to this channel in this tutorial we are going to use click to create a command line interface around a python program this is useful for those quick and dirty tools that you want to quickly get to work you don't need them to be rock solid but they still need to get arguments so instead of using sys.rgv we are going to use click in this tutorial we will first create a simple command then we will explore a command group and then we will see how to invoke a command without passing the command name let's get into it so the very first thing i'm going to do i'm going to create a virtual environment once the virtual environment is ready i will install click all right now that click has been installed let's move to our code editor so the very first thing here is to import click now this is going to be in a hello world kind of application so let's add some scaffolding code okay now let's create this salute me salute me is going to be our hello world command we want to invoke that through command line so the method will obviously receive the name and it's going to print salute hello world or hello name so it's going to be an f string all right so we want name to be parametric and be coming from the command line so very simply we just decorate this method with click command and then we need to define a click option the option will be dash dash name we can define a default which is going to be you guessed it world and then we can also define a help text so the help text will show in the command line when we when we invoke the program with dash dash help all right so here we have everything we need let's switch back to the command line and let's invoke this program and here by default we have hello world but what if we say hello name caveman hello caveman alright so this is a very simple a very simple command you can't go simpler than that and you probably noticed that this is even this is already easier than using sys.rgv to get your your parameters into your code let's step up the game a little bit let's explore the command groups so let's imagine that we are creating this program to check in someone in a hotel so first we salute them and then we check them in so let's quote the check-in method check-in takes the room in which we want to check in the the guest so we say print um so it's going to be enough string you're staying at the room and then we say room the room that was passed as an argument all right so nothing special there and this is this check-in is going to be a command so we decorate that again click command and an option okay so basic scaffolding for this method is there but we need to make some structural changes so now uh we want the check-in method to be a sub-command of salute me so command becomes a group and then we need to add check in to salute me so salute me dot add command that's how you add the comment and then just check in so now the hierarchy is defined there are a couple of things i need to add so i want to have a context uh going around so that i can i can use the parameter from salute me in the method of in the check-in so i will add it here as well so pass context so to manipulate the context i have to get in a parameter in the first place i will call it ctx so here when uh when i call um this program uh all the way to check-in um i will capture the name but the name will not be passed in the check-in and the way to get it over here would be to copy this parameter but i don't want to copy this whole thing it's a bad practice so i'm going to use the context so in the context i know that the parameters are in a dictionary called params so the name will be ctx params and it will be called name because name is the name of the parameter all the way here so instead of saying you are staying at room we will address the guest by name so name you are staying at room something all right so with that done let's give it a try and the check-in will be not check underscore in but check dash in and the room i want to room one two three let's try boom all right so there is a mistake obviously in the code and the problem here is that i'm referencing the i'm referencing the wrong context so let's go back into the code so instead of context.params i have to do context.parent dot params so this is going to get the context of the the parent command so let's try again let's go back to the command line and and now it works so the first step is to salute caveman and then caveman you are staying at room 123 so the parameter passing is working as expected as a last step in this tutorial let's explore a default execution let's say that we want to invoke our code without parameters and we want it to do something if we do a uh my code without parameters we are greeted with the help text which is a very useful feature but that's not exactly what uh we want let's say we we want the salutation to happen by default so with the code structured like that this is not going to be possible or almost we only need to do one simple change to the code to support that which is going into the group and say invoke without command we set that to true so once this is done we can go back to the command line and when we invoke it like that it will use the default name so if i say name caveman in this case it's going to salute me correctly if this tutorial was helpful hit like and subscribe and there you have it just a few lines of code to step up the game of your command line tools this is much better than sis rgb it's clearer self-documented and a lot more maintainable [Music]
Info
Channel: Coder Cave
Views: 231
Rating: undefined out of 5
Keywords: codercave, bite-size tutorial, python, cli, command line, command line interface, how to, how-to, software development, coding
Id: 20P76Hteyns
Channel Id: undefined
Length: 9min 0sec (540 seconds)
Published: Tue Aug 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.