hey guys welcome to the next blog on Python rule for beginners in this blog I'm going to show you how you can pass the command-line arguments to your Python script so to start with I have a script called my parser dot py and in order to pass the command-line arguments to this dot py file first of all we need to import a built-in module which is called arg parse module now using this our parse module we can parse the parameters which is passed using the command line to this script so let's see how we can parse those parameters so first of all I'm going to declare a variable called parser and then I'm going to use this arc parse keyword which we have imported and there is a special class here which is called argument parser which we are going to use now this argument parser class takes an optional parameter which is the description which we will see little bit later so the first step is to initialize your parser the second step is to add the parameters which you want to parse so this we will see in a bit but let's see the next step which is actually parsing the arguments which is passed using the command-line so let me declare one more variable which will be our arguments which we want to parse and then we will use this parser variable which we have created and call the method called parse odd so now once you have used this method pass args with this parser variable then you will have all your arguments into this odds variable so let's run this script and let's see what happens when we run this script so in order to open the terminal in PyCharm you can just click on this option which says terminal and this is going to open the terminal in the pycharm ide now it depends in which project you are working in so if you see some other location which is opened using this terminal you just need to provide the part where your project is so I'm going to right click on my file and then I'm going to copy the path of this file so right click and copy part and I will just do CD and I will paste this part which I have copied and I will remove the name of the file because I just want to CD to the directory in which I'm working in so now I'm in the directory in which this my parser dot py file is present now in order to run this Python script I can use this Python keyword and then the name of my file which is my parser dot py file now whenever you import the arc pass module and you initialize this module there is a special help parameter which is available so you can write minus H here and then press enter and you will see that this help parameter is automatically present whenever you import this arc pass module and initialize it so this is one way of running your script you can also run your script on your Windows or Linux terminal the process is same so you need to CD to the directory in which you are working in so I'm going to once again copy the path and paste it here and once I'm in this directory I will just write Python and the name of the script which is my parser and then - H which is for help and it's going to give me the same kind of result so you can run your python script either on the windows command prompt or in the pycharm the choice is totally yours so I'm going to use this pycharm terminal so let me minimize this terminal for now now as I said you can pass some argument here in the argument parser class so the argument which you can provide here is that description which is the description you want to give whenever somebody runs this script so this is going to be the description whenever you run a your file on the terminal so I'm going to once again run this Python file and you can see the description is printed here now now how to pass the command-line arguments so you can pass two kinds of arguments as the command-line arguments one are called positional arguments and other are called optional arguments so first of all we will see how to pass the positional arguments so in order to pass the positional argument you use your parser variable and then call a method called add argument here and first of all inside the add argument you gave the name of your argument for example I want the user to provide two numbers and I want to perform some mathematical operations on these two numbers so I can store these two numbers into some variables and these variables I can define here so the first variable name I want to provide here is num1 and then as the second argument you provide the help parameter so in the help what you do is you provide some help about the argument which you want to receive from the user in a same way I can provide the number two argument and I can just change the name of the argument to and the help description of argument two now the third parameter or argument which I want to provide using command line is the operation so let's say we want that user should provide some kind of mathematical operations to work with so in the help I can just write provide operator so these are the three argument which I want the user to provide now once these three arguments are provided by the users these three arguments will be parsed using this function and then all these three argument lists will be assigned to this args variable so we can just print the value of this arg variable and let's see what is the content inside the arcs so i'm going to open the terminal once again so after the Python keyword and the name of your script you provide the most number which is let's say 80 for the second number let's say it's 41 and the operator so let's say I want to provide the operator plus to add these two numbers and then I'm going to press ENTER and you will see this print statement is going to print the value of number one which is 84 the value of number two which is 41 and the value of operation which is plus here you will also observe that all these three numbers are present in the form of string so in order to work on these numbers we need to convert these numbers first into a float value or the integer value and then only we will be able to work on these numbers so how we can provide the type of these two numbers so that our script understands that these are the numbers and not the strings so the way in which you provide the type of the argument is using the type keyword here and then you provide whatever type you want for your argument or the parameter so here I have written type is equal to float and in the second argument also I have written type is equal to float and by default the type is string so if you don't provide any type it's a string so now we have provided the type to our first two numbers and let's run the script once again so now you will see that these values are no longer string values but now they are some float values also whenever you run the script which - H keyword which is the help keyword it's going to show that these are the positional arguments which is required by the script which is number one number two and operation and also we can use - H as the help and also it's going to give us the description of this script which we have provided here so now we can perform some mathematical operations on the R humans so let's perform some mathematical operations on the arguments so we can extract these argument using the odds keyword and then calling the name of the variable which we have provided so arcs dot number one is going to give you the value of number one so what I want to do here is I want to know the operation first of all so arcs dot operation and if this operation is equal to plus then I will just add these two numbers so I'm going to just say args dot num1 plus r dot number two now after adding these two numbers I need to save these numbers into a variable so let's create a variable called result here and I will initialize this result by none so initial value of result is none and then we will assign the addition of the two numbers to the result parameter and then at the end we will just print the value of the result and the same I will do for the other operations also so let's define some more operations second operation I want to provide here is the subtraction third operation I want to provide here is the multiplication and let's say fourth argument I want to provide here is the power of operation so here I can use inbuilt function which is Pio W and then I will just pass the first number as the first parameter so arcs dot num1 and the second number as the second parameter so now let's run the script once again and let's see what happens when we run this script once again so I'm going to provide the same parameters 84 forty-one and operation classier and then press Enter and you will see the result here is the addition of these two numbers because our plus condition is matte for the operation and then it's going to just add these two numbers and print the result we can make this result a little bit more sensible by just saying this is our result so I'm going to just say result coolin and then provide the comma here and once again let's run the same script with the minus operation this time so I'm going to provide the minus operation now and then press ENTER and it's going to still give me the addition this is strange because I think I have done some mistake here which is I haven't provided or changed these operators from here so in the case of - I need to provide the - here and in the case of multiplication I need to provide the Asterix here so let's run the code or script once again with - and now you will see as a result you will see the subtraction of these two numbers let's run the script with P or W and then press Enter it's going to give me the power of operation of these two numbers so this is how you can provide the positional parameters now as the name suggests these are the positional parameters that means every parameter have its own position and this position is defined by the Declaration here so the first position is for the number one second position is for number two and the third position is for this operation parameter so if you change the position for example I will provide the plus operator first and then these two numbers this will give me an error and it will say that I was expecting a number as the first argument but you have provided some string parameter here also if you don't provide any parameter so I'm going to just say first two numbers I will provide but operations I haven't defined then also it's going to give me an error and this will say that operation is not defined because we haven't defined any operation here so it's going to give us an error so there is one way of solving this error which is by providing the default value and in order to provide the default value you use the keyboard default and then provide the value of it so let's say by default the operation is plus always and when I run this code once again it's going to still give me this error because these are the positional arguments we cannot change the position of this argument and we cannot provide the optional values here and that's where the optional parameters comes in so using these optional parameters you can provide the optional values so let me show you how you can provide the optional parameters so the way you provide the optional parameters is by simply adding double hyphen symbol in front of your parameters so I have provided double hyphen symbol in front of each and every argument which is provided to the script and let's go to the terminal once again and this time we need to provide the name of the parameter which is - - num1 space the value of it for example 80 and then we need to provide the second number which is - - num2 and then after the space the value of number 2 and let's say I don't want to provide the operation now and when I press ENTER here it's still going to give me the result which is plus because now we have the default value of operation which is + and if I don't provide the value using these command line arguments then the default value of this operation will be taken which is + if we provide any argument here so - - let's say operation and we will provide the value of operation let's say - here that is going to give us the subtraction of these two values so whatever argument you will provide here the default value is overwritten by that value so this is how you can use the optional parameters and in order to use the optional parameters you also need to give the name of your optional parameters now whenever you use the optional parameters it depends upon you at which position you want to use these optional parameters so I can use foreign example number one at first position operation at the second position let's say operation is multiply now and then I will provide the number two at the third position it doesn't matter now because the position is irrelevant here because we are providing the name of the variable and then the value so value will be passed using the name variable and not by the position now there is one more notation which you can use with these optional parameters and this is the short form of whatever parameter name you want to give so let's say I want to give n as the number one and let's say I want to give I as number two and for operation I want to provide this letter O so this is the short form of these three values so n is for number one I is for number two and O is for operation and also in front of each and every letter here we need to provide a single - so either you can provide this kind of notation with explanatory keyword and double - or a single letter and a single - in front of it so now in order to use this kind of notation what I can do here is first of all let me call Python and the name of my script and let's say first of all we call the help and you will see this helps shows this letter also which is - H or help and for our parameters we have - N or the number one - I or number two or - o or operation so the way we provide these shorthand notation is we just need to write the name of the notation which is - n for number one let's provide the value of number one and - I for number two so you use this equal to symbol in between the value and your shorthand notation not the space so this time I am using the equals to symbol so I equals 70 and the value of minus o is equal to let's say plus this time and then press ENTER and it's going to give you the addition of these two numbers so for this shorthand notation you use the equal to symbol not the space in between the name and the value so this is how you can provide the command-line arguments to your Python script I hope you have enjoyed this blog and I will see you in the next blog you
Post a Comment