Building CLI Tools with CLICK- Introduction

Building Command Line Tools with Python, Julia and Nodejs Module 02 - Building Command Line Tools in Python with CLICK
17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.66
List Price:  €93.80
You save:  €28.14
£56.20
List Price:  £80.29
You save:  £24.09
CA$96.29
List Price:  CA$137.56
You save:  CA$41.27
A$109
List Price:  A$155.72
You save:  A$46.72
S$95.29
List Price:  S$136.13
You save:  S$40.84
HK$548.18
List Price:  HK$783.15
You save:  HK$234.96
CHF 63.57
List Price:  CHF 90.82
You save:  CHF 27.25
NOK kr772.45
List Price:  NOK kr1,103.56
You save:  NOK kr331.10
DKK kr489.98
List Price:  DKK kr700
You save:  DKK kr210.02
NZ$118.75
List Price:  NZ$169.65
You save:  NZ$50.90
د.إ257.05
List Price:  د.إ367.23
You save:  د.إ110.18
৳7,680.95
List Price:  ৳10,973.26
You save:  ৳3,292.30
₹5,842.52
List Price:  ₹8,346.81
You save:  ₹2,504.29
RM334.79
List Price:  RM478.30
You save:  RM143.50
₦90,777.03
List Price:  ₦129,687.03
You save:  ₦38,910
₨19,491.43
List Price:  ₨27,846.09
You save:  ₨8,354.66
฿2,579.37
List Price:  ฿3,684.97
You save:  ฿1,105.60
₺2,281.41
List Price:  ₺3,259.29
You save:  ₺977.88
B$366.88
List Price:  B$524.14
You save:  B$157.26
R1,343.94
List Price:  R1,920
You save:  R576.05
Лв128.54
List Price:  Лв183.64
You save:  Лв55.09
₩96,612.79
List Price:  ₩138,024.19
You save:  ₩41,411.40
₪264.97
List Price:  ₪378.55
You save:  ₪113.57
₱4,029.04
List Price:  ₱5,756.02
You save:  ₱1,726.98
¥10,812.58
List Price:  ¥15,447.20
You save:  ¥4,634.62
MX$1,208.81
List Price:  MX$1,726.94
You save:  MX$518.13
QR256.24
List Price:  QR366.07
You save:  QR109.83
P970.12
List Price:  P1,385.95
You save:  P415.82
KSh9,343.66
List Price:  KSh13,348.66
You save:  KSh4,005
E£3,382.65
List Price:  E£4,832.56
You save:  E£1,449.91
ብር3,997.39
List Price:  ብር5,710.80
You save:  ብር1,713.41
Kz58,507.55
List Price:  Kz83,585.80
You save:  Kz25,078.25
CLP$67,632.73
List Price:  CLP$96,622.33
You save:  CLP$28,989.60
CN¥506.81
List Price:  CN¥724.04
You save:  CN¥217.23
RD$4,150.64
List Price:  RD$5,929.75
You save:  RD$1,779.10
DA9,419.60
List Price:  DA13,457.15
You save:  DA4,037.55
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q546.30
List Price:  Q780.47
You save:  Q234.16
GY$14,650.65
List Price:  GY$20,930.40
You save:  GY$6,279.74
ISK kr9,869.98
List Price:  ISK kr14,100.58
You save:  ISK kr4,230.60
DH711.61
List Price:  DH1,016.63
You save:  DH305.01
L1,253.52
List Price:  L1,790.82
You save:  L537.30
ден4,049.40
List Price:  ден5,785.11
You save:  ден1,735.70
MOP$564.53
List Price:  MOP$806.50
You save:  MOP$241.97
N$1,337.58
List Price:  N$1,910.91
You save:  N$573.33
C$2,584.86
List Price:  C$3,692.82
You save:  C$1,107.95
रु9,352.12
List Price:  रु13,360.74
You save:  रु4,008.62
S/263.28
List Price:  S/376.13
You save:  S/112.85
K266.90
List Price:  K381.31
You save:  K114.40
SAR262.55
List Price:  SAR375.08
You save:  SAR112.53
ZK1,789.88
List Price:  ZK2,557.08
You save:  ZK767.20
L326.78
List Price:  L466.85
You save:  L140.07
Kč1,658.90
List Price:  Kč2,369.96
You save:  Kč711.06
Ft25,921.07
List Price:  Ft37,031.68
You save:  Ft11,110.61
SEK kr766.36
List Price:  SEK kr1,094.85
You save:  SEK kr328.48
ARS$60,874.37
List Price:  ARS$86,967.11
You save:  ARS$26,092.74
Bs485.36
List Price:  Bs693.40
You save:  Bs208.04
COP$272,889.01
List Price:  COP$389,858.15
You save:  COP$116,969.14
₡35,190.72
List Price:  ₡50,274.61
You save:  ₡15,083.89
L1,734
List Price:  L2,477.26
You save:  L743.25
₲519,666.57
List Price:  ₲742,412.64
You save:  ₲222,746.06
$U2,710.62
List Price:  $U3,872.48
You save:  $U1,161.86
zł284.04
List Price:  zł405.79
You save:  zł121.74
Already have an account? Log In

Transcript

All right, so welcome back again to any class session we try to see how to work with. Click right so let's see how to first of all install click the news and move on from there. So if I go back to my Moodle two folder, there's nothing so I'm going to create a folder called MKT how for this click to place we wicked nice. Now let's go inside our clip and we're going to create a sample file. I just come back to default here, create a new file and we'll be saving this particular file as busy CLR to basic CLR with pi, right you can see it has a name. Now let's see how to work with click.

So first of all let's install Click on Install just go to your terminal and click pip install. Click buy that is how to install click on your system. So in case you are using Python 2.7 which is deprecated by now because by 2020 you can use this particular option I to to install it Dennis options I can also use PIP three To install Python to install it for Python three purpose is not to be PIP three install CLI. Very interesting, very exciting. Now let's see how to work with it. So the first thing that we want to import click input click very simple.

And I'm going to create my simple function which is going to be me. So it is very important and very essential that when you are writing your CLS you put them either in functions or in classes, right that is the basic idea behind it is going to be with key to string which is going to be a simple simple CLR is very interesting. Now let's print something very simple to print. Hello CLR CL I bought this right very interesting. Interesting though good to integrate this with lotion mean all the best ways to just go with this mean? So this mean is referring to the mean find it right now.

Time I run this particular file, also going to run it perfectly for us. But that is not the best way. Right? So let's use this to evening project. So this is very good. So if name is called to mean, then run this main function we just wrote into this.

If so how would we convert this particular function into the simplest we'll get this come back to the top of the funnel and go with click command to the click command isn't this is a decorator, right click decorator that's going to convert this particular function into Seola. If I save it, and I come back to my terminal and greet Python three, then how to run the scene father to have this option. Go to list Hello, click put this to the newt convert this particular function into a new line. So I can actually go to this option of that does help and then I hope to see how to use this particular Osceola. So see how it says to that is going to be the usage, very excited with options. Then you also have a Simple COI, which is referring to this doc string, it will have the option C very interested.

That is something very cool about it. So in case I want to add a patient with a cat just come up to this place with click, go to vision option, right, then I specify the Press Club option, right, there's going to be Press Club vision is going to be 0.01. Right? It's very interesting and very useful for you to be naming your vision in this particular option, right? Then I can just go with the probe name, the problem is going to be the program name, I'm gonna call it a basic CLR. Very simple.

So if I save it, and I come back here and run it again, we're going to see our vision option there. And it's how to add version to it. Very simple. So with this simple code, you'll be able to build a simple CLA. They've given us a very nice documentation, a very nice hope that we can use. So we get to see their vision, and he's come back to this place and go with vision.

Let's bring it to the top. with Visual Basic see Li referring to the program name here. Right? And then the beige interpreter, right? We're very interested in a very simple that is how to build a simple sealer with click now let's see some other seconds to do. So how do we add options to it?

So there are several things can also do we have seen how to add a simple patient info and add an option to go to this option. As we learned that options are non mandatory and non mandatory. They are not a must in every option. Let's go with that. That is the basic idea behind it. So let's see how to create a simple option.

So a simple option is going to be at click that option, right pivot DNS argument to take it a row to take the dash that's the name of the option. So let's do something like see first name right, then you can just specify it is going to be the long format have the option here to keep the short format it is very essential to always give the long format Kunis short format is going to be a short format so there's going to be that if right for face name, then an estimator can specify a description for it. So to say this name description that is asked by civitan I come back here and let's run it again. For now go to see our first new option here, right so we have the short form long form and this is going to be the type and description first name description.

So how do you actually let it wait to allow it to work you have to pass this option into our function, right? So to be able to identify so by default to this click it automatically going to identify that this first name is the one that is being passed. Right to me changing it from here, so hello, COI but it's going to be Lucy alight Buddhists, right if it they can just print. Hello, Deline clip isn't taught format If I save it now, and let's run it again, everything is working as expected. So I can just come back to my family there specified any of us just going to print out Hello, cry Buddhists. Hello, Jesse.

Very interesting, very simple. So with this simple code, we need to add an option to it. So if you notice this was a test, right? So that is the type of diff of energy I'll give you various areas to specify spiritually specify the type. Let's come back to this type, then I'll just go with string, right try to it's going to tell me that this particular type is only for strength. We come back here, run it again.

Everything is working as expected. So what if I don't specify any as we learned that options are non mandatory, in case I go with this option, without this fight, I need to print it perfectly for us. Hello, none right? So options are non mandatory. So the best way of fixing This non aluna is that you can just specify a default value by default. And you keep the default value as we see with now.

And I come back again, I run it again without any option would be using the default Hello friend as the default rate. And this how to default to Oh, we have seen how to create a simple option using the long format initial format, and then how to add description with the help how to add a type, how to add a default value to it. Very interesting. So there's something very basic about it. Now let's see how to also add some other aspect to it. So let's add another professor click third option, which is going to be age and I'm going to keep it short format for each, then let's give help is going to be your age.

Very interesting to add a type here is going to be now in SPSS. Have any type to be at least twice a week now Don't forget to pass in the option here to age two which is going to be Hello. Quite you ages This is metaphoric your age is that we pass it not here to very interesting because I see it now this printed game so hello friend your age is not right by default keyboard the default value here and an anon also not too if I come back to the desktop hope we can see how to use it so we see that our age how they specify that is quite sweet if I come back and I run it from scratch, basically, and let's give it the age is 20 zero day just to fit this me this 20 perfect is wicked right sweet if I give it a value like let's see this 20 is working perfectly well.

So there is an intuitive way to work with magically anytime you put a type at the top here, right, it's almost clearly converted that the condition at the top right is something very interesting. Let's add something to it. This was a sprint. So the last year you will be who you will be this year you'll be this year by now, right to be adding a value added value. So it's got us here by now just give it a name. Good to be one plus two each the dependency supply.

Right. So we just had it so much. Yeah, bye. pivots or lists or anything you So what's going to give to us. So you'll be this 203 a year by now very interested to read this portion at the top here, I'm converting it automatically, but I can ignore this one here. If I ignore this one from here, and I run into it give us an error because you cannot commit, combine a string, and the number of threads given us also prototype right port supported type.

So one of the ways of serving just come back to the split and go by the hit around our age. Then if I run it is going to break previously, right. So the simplest ways that lm recommended way is to specify the top a type at the top of the option. So this is very a tweet is based in beta this one because we have a new to see how to add options to it. Now let's see how to work with multiple values. So it gives you want to receive multiple bye For a single option, let's see how to do that.

To induce was going to be like this is going to be multiple value for an option, right? Do you see multiple value or multiple ultimate for an option? So for example, let's say you have salary you want to receive multiple values so multiple estimates for salaries at 200 different police IDs for one particular option How do you need to do the simplest we'll just go with the same thing. Click that option, then our passing day long format of Arsalan can in a short format of salary. Right then from here I can specify the help going to be your salary. And I will just go with nlg that is admit to accept less accept only two By going to is only going to accept to so don't forget to pass this salary inside our function so very interested unless added to it to be print apologies for the night behind the print then we're going to be your salary your salary is that right?

Okay good now is anything good? See, you seem to know you have done so far. Let's go with the help. So, we see that we have our salary here which is a test we have ambition you have our each of our first name, it gives out a salary to with the candidates run the same thing that we did, which is this steam thing. Right. So it was going to give us as soon as empty was since we specified that this is receiving a number of argument is to go to put everything as a tuple Right, so guess I pass in that value is going to be the salary.

Salary. So I put 22,000, to receive 40,000. Let's put them together for us very interesting. So as I said earlier, if I add another value to it, it's going to give us an error, right? So goes on this credit history. If I use only one, it's also going to give us an error.

So it's very, very strict. It has to be two, two, you have to know when to use it. Very interesting. Now let's add something to eat in case you want to add a type to it can actually add types to this particular tuple. It's about the face width, I can just go with this type, then grid is going to automatically convert it into int. Then from here, I can sum it up.

So let's sum it up here. So I can just sum it up sum. This particular value here is allow us to print it. This is time before us. Come back to this place with what ultimate support that is something very cool, right with the number of arguments. So we're going to sum it perfectly for us.

Okay, so that is something interesting I've done now let's see how to add not just multiple values but in case we want to add multiple options, right? So what do I mean by multiple options? Are there multiple options that you just go with this option is going to be multiple options. Everybody can just specify something like this. See, first name right, this is going to be Jesse, and a first name. David.

David, like that, when the same auction I targeted So one of the ways of doing that is just go with the same options. Click that option. Then I just go with the first UCT then I wrote the city that is not listed. Right, get from me, I can just hope it's cool too. Your cities then from here can just go into multiple set to true. So by default, all of these options are set to what we set to once we can only use it once.

So I have to pass my city here and then have to print it print you visited satisfy cities, right? Those format then so let's save it and let's come back to them No, run our help. is going to print it bring it perfect for us, our cities, then I can actually do the same thing. See for London. So he visited London. He was dead.

He was dead, New York. Ronnie's not going to put all of them inside it for us. Very simply the basic idea behind using Multiple setting multiple to true, very interesting and very simple, very nice. Very cool. So this same thing can be applied to the face name in any of these cases. And it's how to work with multiple objects.

Now we have been using print Print to print all over. So in case you're using Python three, it was working out if I use placement to call it wait. So let's run it again. And let's use it to it's working perfectly. But this is because we are running it by trying to write it to make it work by clicking through the optional using clicked with echo to help us with this issue. Phyllis, let me show you what I mean.

So I agree to print. Hello, Python two. And this one again, as long as you did this Python two, it went quite perfectly right. If I do Python three is giving us an error right there it is giving us an error. That is right. 3d doesn't work, right?

This does not work we do have this particular option Python three. So how do you fix that issue to fix that issue? Can this come back to this place and then use click the echo instead of print want to use click.so not going to be click to click on the parts inside that particular bracket. So we did option now Could I despite them two by three is still going to wick and give us no effort. So let's save it. Let's run it again.

Bear with me to this we can dissipate on three and this one is going to while everything is okay, so the basic idea behind botnet symposia lie with click so in the next session, we try to see how to work with click not not options, but click positioner. Thank you and see in this session.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.