ArgParse - Optional Arguments

Building Command Line Tools with Python, Julia and Nodejs Module 02 - Building Command Line Tools in Python with ArgParse
9 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
€60.04
List Price:  €85.78
You save:  €25.73
£52.20
List Price:  £74.57
You save:  £22.37
CA$96.57
List Price:  CA$137.96
You save:  CA$41.39
A$107.16
List Price:  A$153.09
You save:  A$45.93
S$90.17
List Price:  S$128.82
You save:  S$38.65
HK$546.07
List Price:  HK$780.13
You save:  HK$234.06
CHF 56.24
List Price:  CHF 80.34
You save:  CHF 24.10
NOK kr701.61
List Price:  NOK kr1,002.34
You save:  NOK kr300.73
DKK kr448.11
List Price:  DKK kr640.19
You save:  DKK kr192.07
NZ$119.34
List Price:  NZ$170.50
You save:  NZ$51.15
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,512.87
List Price:  ৳12,161.77
You save:  ৳3,648.89
₹6,162.88
List Price:  ₹8,804.49
You save:  ₹2,641.61
RM295.88
List Price:  RM422.70
You save:  RM126.82
₦107,536.13
List Price:  ₦153,629.63
You save:  ₦46,093.50
₨19,804.90
List Price:  ₨28,293.93
You save:  ₨8,489.02
฿2,263.93
List Price:  ฿3,234.32
You save:  ฿970.39
₺2,880.27
List Price:  ₺4,114.84
You save:  ₺1,234.57
B$382.90
List Price:  B$547.02
You save:  B$164.12
R1,233.84
List Price:  R1,762.70
You save:  R528.86
Лв117.53
List Price:  Лв167.91
You save:  Лв50.37
₩97,257.52
List Price:  ₩138,945.27
You save:  ₩41,687.75
₪235.73
List Price:  ₪336.77
You save:  ₪101.04
₱4,010.39
List Price:  ₱5,729.37
You save:  ₱1,718.98
¥10,397.50
List Price:  ¥14,854.21
You save:  ¥4,456.71
MX$1,309.19
List Price:  MX$1,870.35
You save:  MX$561.16
QR255.01
List Price:  QR364.32
You save:  QR109.30
P943.39
List Price:  P1,347.77
You save:  P404.37
KSh9,039.20
List Price:  KSh12,913.70
You save:  KSh3,874.50
E£3,401.11
List Price:  E£4,858.94
You save:  E£1,457.82
ብር9,999.68
List Price:  ብር14,285.86
You save:  ብር4,286.18
Kz63,827.73
List Price:  Kz91,186.39
You save:  Kz27,358.65
CLP$68,145.06
List Price:  CLP$97,354.26
You save:  CLP$29,209.20
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,408.74
List Price:  RD$6,298.47
You save:  RD$1,889.73
DA9,093.35
List Price:  DA12,991.06
You save:  DA3,897.70
FJ$158.28
List Price:  FJ$226.12
You save:  FJ$67.84
Q536.40
List Price:  Q766.32
You save:  Q229.91
GY$14,634.99
List Price:  GY$20,908.02
You save:  GY$6,273.03
ISK kr8,623.46
List Price:  ISK kr12,319.76
You save:  ISK kr3,696.30
DH633.06
List Price:  DH904.41
You save:  DH271.35
L1,154.10
List Price:  L1,648.78
You save:  L494.68
ден3,701.61
List Price:  ден5,288.25
You save:  ден1,586.63
MOP$562.68
List Price:  MOP$803.86
You save:  MOP$241.18
N$1,239.98
List Price:  N$1,771.48
You save:  N$531.49
C$2,575.56
List Price:  C$3,679.53
You save:  C$1,103.97
रु9,878.18
List Price:  रु14,112.30
You save:  रु4,234.11
S/247.37
List Price:  S/353.41
You save:  S/106.03
K293.47
List Price:  K419.26
You save:  K125.79
SAR262.62
List Price:  SAR375.19
You save:  SAR112.57
ZK1,658.21
List Price:  ZK2,368.97
You save:  ZK710.76
L304.89
List Price:  L435.57
You save:  L130.68
Kč1,468.04
List Price:  Kč2,097.30
You save:  Kč629.25
Ft23,641.10
List Price:  Ft33,774.46
You save:  Ft10,133.35
SEK kr660.62
List Price:  SEK kr943.78
You save:  SEK kr283.16
ARS$95,148.96
List Price:  ARS$135,932.91
You save:  ARS$40,783.95
Bs483.58
List Price:  Bs690.86
You save:  Bs207.28
COP$280,654.43
List Price:  COP$400,952.08
You save:  COP$120,297.65
₡35,381.84
List Price:  ₡50,547.65
You save:  ₡15,165.81
L1,844.66
List Price:  L2,635.35
You save:  L790.68
₲505,480.47
List Price:  ₲722,145.91
You save:  ₲216,665.44
$U2,801.15
List Price:  $U4,001.81
You save:  $U1,200.66
zł255.30
List Price:  zł364.73
You save:  zł109.43
Already have an account? Log In

Transcript

All right, so welcome back again. So in this session, we'll be trying to see how to work with option argument and then position argument. So in the previous session, we saw how to work with a class to build a simple CLA. So in this session, we'll be trying to go on the next step. So let's see how to work with option argument. So to recap, to build a CLR with a class specific just going to import a path.

If it didn't just going to create your function, then going to create your constructor to be able to initialize and consume or the argument that you keep keeping is going to be the path right. Then later on in case you want to add a version to it, you just go with this particular option of adding the patient to me, then you start every argument within this particular option. So this is going to be Santa doesn't be used to store, audit, administrate. It stores for the ultimate. That's your past. Very interesting.

Now let's see how to work with this. Well, first off, we'll be working with caution. I admit the option argument and that's related option arguments usually goes with the Justice right, so anything that's starting with the Duster is going to be seen as an optional argument. So by default, APA is able to identify that anything that is, that is a no is an optional argument than anything that is which doubt and justice is a position argument. So let's see an example. So I'm going to add, and I want to go in cold, let's go to the passer with our documents, right and I'm passing that particular template with first name.

So I've been doing that is gonna take the long format, then it's always recommended to always bring the shoot from us, right, there's going to be short format, pivot, then I can just give the particular stuff I want to be so we can actually add a description day with the help, then this is going to be description. So let's call it us face name. Very interested. That is what I need. So this is how to add an optional argument. Very simple, so we don't need to pass anything.

So that is all three facts come back to this place and let's run it again. Come back to my help. And I write it no good. You see, the first name is Amanda admitted the fifth. So now we have our first name here, which is the optional argument. Very interesting.

And then this is going to be the description that is given to us. Very interesting, right? That is the basic idea. Okay? If it's Felicity and enough to do so, in case I want to actually receive and consumed what I'm passing and displayed out, I just come back to this place. let's print something out.

So let's go to print. And let's say you're your first name. We bring it up here, your first name or let's say hello something right. Hello, then they need to both formats. Then for me to be able to receive that only only that particular admit, I just have to bring it with this towards me, so this first need to fairing to this particular facing right at the end. So what does this Didn't happen.

So let's print it out to see what this one is. That is going to be like the namespace is going to be great. The title of this of the let's see with Iran and see why we can be able to do this. Come back. Kevin hope everything is working. So let's add our first name.

It's going to be first name. First Name is going to Peter. Greta, I should add this act that is being received as being causing to store everything in a namespace, right? It's going to stay within a namespace is a namespace, right? The linear space actually stores all the argument that you're passing. Then we have our Hello, Peter.

Right, very interesting. So since it's a namespace, now everything is coming as an option. And then we can pick that face name only that name from that particular storage namespace and the basic idea behind so let's add another one to it. We can just come back to this place duplicate it Let's give it as another name. So let's call this one as each, right. So this is going to be the age, then this is going to be age.

So this is going to be your age. Very interesting. So your age, your age, so don't just Oh, so he's going to bring the ATMs will come to the same place. And then give it Hello your ages. Hello. Yo, each is in our supply they need to this is going to be it.

So it's very, very simple and very easy to do. So let's come back again. Print everything I printed now we have first name, Peter. Hello. He does not write everything give us money that says it's optional. You can go without it.

Right. There's still going to be but I just come back to this please supply the name. Each to the age of Peter is 23 is going to print it out for us. Hello, Peter. Hello. You're ages 30 theory, that is the basic idea behind working with OSHA.

Now, I can make very simple and very nice. So I call it optional. Optional. Okay, that is something very basic about it. So we saw that it gives us not not. So how do you fit that, we can actually give a default value for that.

So it's going to be in this option to specify a default value default, then I give a particular value solicited default one is going to be 20. Right? So if I save it, and I come back, and I run the same thing that we did, without any argument 20 that any argument is given as an error because we did not run it or come back. Let's go without each. So hello, your age is 20. Right?

So this is picking that default value. That is something very interesting. So that's how to add a default value to it. So it gives you don't want to use each in this particular format. You want to use something like let's see, birthday or something right. Let's add another command to a so I'm just going to go with this option.

So quit another add a commit, then I'm going to add the agenda to it right? There's going to be gender, pivot, then disco dash, the short format is going to be SVG. Then let's give the hell is going to be your gender. So your gender Okay, so let's see this one. Let's pass it so hello you hsts and then and then your gender is whatever you want to pass. They can use a string if you want, but there isn't, we are using this option is because we don't want to you want to be, it'd be comfortable for anybody that is using any version of Python, right?

It's going to be this option. Have my gender. So let's see if there aren't again, if I come back here, let's close this run 20 Let's give the gender of male. Let's run it again. So hello, Peter, right? Hello ages 28 a nice meal, right?

That is very, very interesting. But there is another option of openness gives you don't use this particular gender name. If I come back to the help option to this option, I don't use this help option, right, I don't want to use this gender, or to give you a different planning, then an added option of this. So that this is going to give it a different name right so let's keep this one as six right instead of gender do the same. So now it's not going to appear as a gender again, it's going to appear as says now to change this one agenda to six. So this destination or This, this is going to replace this particular stuff.

And then it's going to show it here to come back in here. Let's run it again to help the professor now gender type Is this right? Very interesting. It's normal gender, so I can actually run what I did previously. And it's going to work so your, your gender is male, right? You didn't agenda still is now describing it as six here, right?

Because this team is going to give it a different description, as you saw here in a hope auction. So there's a new description is given to us six, right? The old one was gender without it if I go down this one. And then I come back to here, this must also be changed to gender. And I see it in let's see what we have done so far. Come back, can I run it again?

So now this is agenda right? So does the basic idea behind a disk to the disk allows us to be able to actually give it a name that we want it to be here, and it's a basic idea. So that is how to work with an argument. In a message, I'm going to see some figures so that we can do with a parser SR net session stateless

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.