Creating a simple CLI with Python Click

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey all! Starting off my channel, would like your help, if you could, with some pointers!

👍︎︎ 1 👤︎︎ u/guicarraro 📅︎︎ May 10 2021 🗫︎ replies
Captions
[Music] hello welcome to fencing gee my name is gee of course not gooey so not gooey uh gee so today we're going to be talking about command line interfaces right so specifically how to create one using a python library called click so a command line interface it's something that you can use in your terminal to interact with your computer be it windows mac os linux antiflavor you might like that uh you can run from the terminal so in here i i'm gonna showcase a couple of uh my command line interfaces that i've done before and after that i'm gonna show you how to create one so today um right so one of the that i've created before is a g-recommended line interface so this one actually fetches my stories to actually work with abac which is for a billing uh a tool that it's usually just available through through a webview and who wants to go to a website i want to actually see text in my black screen and we also do have here something like call turnip uh this one i created i think it's quite fun oh it's not installed in here but i'm going to install it later to show you but this is to track you know turner prices and turnip exchange for animal crossing so i don't want to be just you know going to website taking a look into the prices that people are might be selling their terms at i just animate it all out here from my command line interface um you can do all sorts of things in here right so um let's show you how to create one and how to do something cool that so right here i am in a folder called super cli i know it's not you know super creative but today we're going to be creating a super cli so that can do pretty much anything that we want so to do that let's open visual studio code it see our code editor here today that we're going to be using and let's create a couple of files shall we so first of all we need to be able to install our command line interface so i'm going to just open a new file here i'm going to select here to be python and what i'm going to do is just call the setup tool so we're gonna be able to use pip pip as the package and manager for uh python and we're just gonna do that right so from uh setup tools import setup so we're just going to do a setup of our tool here so to do that i am i need to actually call out a couple of things so the name of our cli here which is going to be called of course super cli why wouldn't it be super super cli and also we're gonna do is just give it a version right so let's call it let's give it a version we're starting off so semantically just let's just call it zero zero zero and uh whoops not capitalized i thought you you're trying to be way too helpful here um then the packages of course uh we're gonna packages pack ages no there's something wrong here no no [Music] let's find the packages here this is going to actually tell me what uh no find packages there we go that's better all right so this is just going to find packages for me so i don't have to do much uh also i need to actually tell which requirements i actually have right to install so install requires and then here i'm going to pass a list of packages that i actually are required so for now let's just use click and also what we need is an entry point uh entry point there we go and the entry point here is going to tell uh how to run this so it's this is going to be a console scripts so what it's going to do is actually get installed on the copy the files put in the path of my um um where it's runnable right executable uh put the shebang of python so it's going to be able to call that python whenever running this and then i just need to pass it along where it needs to run right so this is super cli uh i want to call it using super super yes it's going to be from the super cli package and it's the function super cli all right that's going to make more sense soon i i swear to god all right so let's solve that save data setup dot pi okay saved and let's try to install it so i'm gonna just open up a terminal here let's make it a little bit bigger in here so you can see and what i'm gonna do is just a pip install editable edit all is important so whenever we save we don't have to be reinstalling it all the time so it's just going to be make a reference to the files that we have in here to the actual path of execution so let's try to install it there we go so super cli is installed it's going to actually create all the reference information and dependency lists you know all this fancy stuff that actually makes it work and now we should have a super cli in here that of course does nothing there no super cli uh module there's no super cli function so let's create one shall we so to do that we're just going to create super cli python and what we're going to do is import click now what click gives us is something really cool that is a decorator we can talk about those later if you want to know more just put it down in comments and i can make a video about decorators and how to create one and how the decorator framework actually well it's not really a framework but anyhow if you want to know more about that i can definitely talk about that if you want so in here click actually gives gives us a command uh decorator that creates a command based on that ain't that cool so um what we're going to call is this is just that that super cli function right so super cli function and for now let's just print hello world how creative gay oh wow world an exclamation mark because who are we exclamation our marks are cool all right so now we do have super cli so what happens if i try to run super again here well would you look at that hello world so what it's doing here it's actually calling that out parsing out whatever it's coming after so click is going to do that for us and telling a and injecting whatever is necessary to our command line to work and you're probably asking yourself injecting what do you mean by that so um there are other parsers and uh to actually create command line interfaces i just like click because it looks fancy and i'm fancy you did the math um so um what do you mean by fancy then so let's say i want to actually say hello to myself hello gee um let's how would i do that well i would do something like this so hello then and my name of course well let's go to here hello space name plus exclamation mark there we go but i need to pass the super cli a name of course right so what click gives us actually here is the option decorator which we can actually pass the long version of hey the parameter name the option name and you can also give it a short name for a name a help string so name of the person to say hello and we could also say it's required that's pretty cool isn't it so now let's see how it goes so now if i save this and i run super again it's going to tell me hey you're missing the option name or dash n oh that's cool but i don't know what that means what if i run help why would you look at that so click already creates for us a the documentation and manuals how to use our our cli if we do make it so um so if i as you can see there's nothing here about what super is what we can do is actually create a docs string docs string is just you know a documentation string that you pass for your function so people would know what it does and this is and i can just tell it uh this prince hello name and that's it so now if i save that it what it's going to do is just going to pass along that documentation string there so people will know what it does and now okay it's asking for a name so if i do super dash dash name and guillerme my full name actually i'm giving away where i'm from don ain't i yeah anyhow it says hello guillermo so it's just gonna inject for the option name the actual string guillermo so he's just gonna print it out hello get out of me fancy isn't it now let's say we wanted to not throw an error whenever someone just calls out super but doesn't actually pass along the name so click has a bunch of ways that you can work around that so you can actually do a default of value which if it is required we don't want a default value of course um but you can actually actually do something really cool like prompt so and tell it how to prompt your name please and then it's a new line so what is it going to look like let's see so if we just call out super it's just going to ask hey your name please and then i'm just going to say yeah it's guillermo you're so polite oh hello isn't it so cool so canadian um so you know you can do play around with this and do all all sorts of things uh i want to show before before i go uh this is already 10 minutes and i know i said it was going to be fast but let's say i wanted to pass now an argument something that doesn't actually have a a option notation to be parsed upon right so it can also do use click argument and pass the name of the argument here so let's say number and this here is going to be just a number of times to say hello so uh what we can do here is actually do four and range number and our number is going to be passed along as a text so i'm just going to put it here as an integer because right python's not going to try to it's going to not like to create a range in an of a string right words they don't actually make sense numbers unless it's base 64 but you know what i mean um so i'm going to just do a range here for the number that it's passed along as an argument and what it's going to do is just going to print out a number a number of times so if i try to run supper again back here please now it's going to say that we're missing the argument number so let's pass along 10 until i tell them tell super my name and it's going to tell me hello get out of me 10 times isn't it cool so with that you can already do all sorts of things right so you can automate a model so let's say you want to print out a model of a text so let's say we have a super important text for then let's say here name and then colon uh let's put it in a line so you've been a great person to talk with so let's say our cli says like yeah it's been great uh talking with you i know i'm not very creative so and now what you can do if you have in your job you have to repeat that for you know 10 times for a person you just say person and a super important text for person you've been a great person to talk with um i'm not really creative hey guys but you know you you can see the the you can see that there's a lot of things that he could do with it so next time what i'm going to show you here is um how to create a cli that fetches let's say the stock price of a stock right so the price of a stock in the market and to do that what i'm planning on doing is actually going to yahoo and uh there we go let's just open here and going to yahoo and searching for the stock price or not maybe we're gonna just go to google google also gives that to us so if you just you know type something like gme to the moon we can just you know parse it out and try to grab the stock price that'll be fun wouldn't it so please subscribe and if you guys um please subscribe if you guys want to see uh how that would work but i'm just gonna be using click all right i've been talking for quite a while thank you everyone see you in the next one bye
Info
Channel: FancyGUI
Views: 3,890
Rating: undefined out of 5
Keywords: python, CLI, Visual Studio, TSLA, Tesla, Stock Market, GitHub, Programming, click, Command Line Interface, Code
Id: GnSKhetBa48
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Sat May 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.