Python Command Line Arguments tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome I'm Dave today we will learn how to pass arguments from the command line in Python and I'll provide links to all resources in the description below I've got vs code open I have an open folder over here named lesson 15 and we're going to create a new file and let's call this file hello underscore person dot pi to accept arguments from the command line that is values that we will pass in to the programs that we call we need to use a module so we will import ARG parse and that is part of the Python standard Library when we previously discussed python modules I gave a link to the python module index and we can see the ARG parse module right here it says command line option and argument parsing Library I also want to call your attention to the python standard library page and this shows kind of the different modules instead of in the index that we had on the other page it shows them kind of in feature areas here so different things you can look at a different way to find the same thing so if I search the page with control F for ARG parse we can once again find it under generic operating systems services so just another way to find the modules that you would be looking for back in vs code let's go ahead and Define a parser and we'll set this equal to ARG parse and then dot then we'll say argument parser and from there we'll use parentheses and we're only going to pass in some of the basic settings that we can for this argument parser but if you look back at that module page for ARG parcel you will see many other settings as well here it will just say description equals and then we'll say provides a personal greeting whoa I'm not spelling good today there we go provides a personal gradient so we have set the basic description for our argument parser now let's say parser dot add argument and there we'll go ahead and pass a few different values first we're going to put in a value that you could use at the command line as a flag so just Dash in this will be short for name so we'll also say you could also provide Dash Dash name which is just a long version of the same thing so you could use either one of these at the command line and I'll show you how then there's another setting here we'll call metavar and we set that equal to name as well now this is just the display name if you get a message that refers back to this argument now we're going to say this is required so here we just set that equal to true and then we can also provide a help message and here let's just say the name of the person to greet and that should be all we need to add this argument to our parser now let's say args equals parser dot parse underscore args and that is a method on parser so we just call that and now we should get those different arguments so when we want to print a message at this point let me Define the message and I'll use an F string so we'll have F and a quotation and I'll say hello and then inside of this we'll say args dot name now where is name coming from let me put an exclamation mark behind this but where is name coming from what you see this value right here that says name now that's pretty much what it's going to refer to however if you want to assign something different we could say dest and set that equal to let's say first name then you would no longer use name here you would use first name for example so either way I usually use dust off and we just refer back to name so let me go ahead and remove that and we'll see how that works as well I'll quickly click yes here but underneath our last one we actually need to go ahead and print our message also so there is a basic example of using the ARG parser now let's go ahead and use this in a terminal window I'm going to press Ctrl in the back tick to open our terminal window that I've already got over here on the right control B to hide the file tree so we can see a little bit more of our code and then alt Z to wrap any code down that was extending out of the window so now it wraps down to a second line now we can't use the play button like we used to to call this file because we need to pass an argument so we're just going to type right here in the terminal and I'll type Pi because I'm on Windows but if you're on Mac or Linux you're probably going to need to type python 3. I'll type Pi then I want the file name which is hello underscore person dot Pi but if I press enter right now I'm going to get an error let's go ahead and do it to see what happens and it says we have an error it shows the usage here and of course the dash H is for help then we have the dash in and it's referring to a name and then it says the error the following arguments are required that's because we required this argument over here so let's try this once again or let's even see what the help does so let's go ahead and pull this up again and then just do Dash H for help and let's see what we get now we get our provides a personal greeting so that's the description we passed in up here and it shows the options and of course Dash H for help and dash in for name the name of the person to greet so let's once again pull that up and now instead of Dash H I'll say dash n and I'll pass in my name as a string just Dave and we get back hello Dave now that's what's expected let's once again close our terminal window for the time being and now let's create a function just above everything we have even the import which I know is something you haven't seen me do before for but let's do it this time and let's say deaf hello and we're going to pass in not only a name but also a language that I'll abbreviate as Lang now inside this function I'm going to define a dictionary called greetings inside the dictionary I'm going to use English and I'll match that to the value of hello that needs to be a string also after that let me copy these lines down with shift alt and the down arrow twice and I'm going to change these so the second one is going to be Spanish and that will be ola and then the third will be German and that will be close instead of hello as we say in English it's Hollow with an A okay after those three let's go ahead and Define our message once again message equals an F string and inside of this string it's going to start with the value that we get from the dictionary so greetings and then we're going to refer to whatever we get in that Lang param that is up here and then after that we want a space and then we're going to use the name param value the parameter so as we pass these values in remember they're called arguments but when we Define a function these are parameters or as I often say params okay now we're going to have a print once again for that message so that's what our hello function is going to do it's going to determine the language we're going to have a name and we're going to say hello in the requested language now someone could import this function from this file if they wanted to and if you remember what we learned about modules we can go ahead and put our if name equals main here to then only run the code underneath if this file is actually the file being ran and it's not having the hello function imported from somewhere so we'll say if two underscores name two more underscores and we'll say equals and we need two equal signs as well then we'll have two underscores Main and two more underscores and then our colon now everything is going to need to be tabbed over so I'll highlight everything and tab over once and now remember we have a second argument so let's add another argument to our parser underneath so we'll say parser.add argument and now here we're going to call this one L instead of dash n so F Dash L we'll also have dash dash Lang and then we'll have a metavar and just to point out that they're not the same here I'll put the full word language instead of Lang but we're still going to refer to Lang when we get the value after that on the next line I'm once again going to say required equals true and then we can pass choices so we can have specific choices here and if these choices are not correct then an error will occur once again so here we'll make sure that the values we want are passed either English or Spanish or German are the choices and then after those choices let's go ahead and put in a help value as well and here we'll say the language of the greeting and that's that's good enough just the language of the greeting okay now that we've defined that we need to change what we're doing below we still want to get the args from the parser with parse args but then here we're not just going to print this we're going to call our hello function so we'll say hello and we'll pass in the args.name and then we'll also pass in the args dot Lang so quickly to review before we use this code we just created our hello function above and then if this is the file that's called into action then we'll use the ARG par so that's why the import is down here it's not needed up here at all and then we'll go ahead and use this and we pass the two arguments to the parser and then we call the hello function with those values let's once again press control on the back tick to open a terminal I can clear this out by typing clear as well just so we start at the top and now let's go ahead and say Pi or once again if you're on Mac or Linux Python 3 then we'll have hello underscore person dot Pi now all of the same errors will apply so if we don't pass a name we would get an error and likewise if we don't pass a language we would get an error but even if we pass an incorrect language like French for example we should get an error let's check that out and yes we do have an error it says invalid Choice French we should choose from English Spanish or German so let's go ahead and choose one of those I'll enter in German and press enter and we have hello Dave and I can see I forgot to put an exclamation mark inside of that function greeting so let's go ahead and put that back seems like we have to have an exclamation mark with a happy greeting right so after German let's put in Spanish now to test this out and we have Ola Dave with the exclamation mark let's once again close the terminal here in vs code and let's show the file tree Again by clicking the explore icon over here and now as we have in previous lessons let's create a new file let's call this file rps8 dot Pi because we're going to pull in the code from our previous example of rock paper scissors and that file was called rps7 in the previous lesson so let's just paste in the code from rps7 that we had and we did learn how to make a module with our rock paper scissors game but now we're going to go ahead and pass in a command line argument to the game so we can personalize the game but before we do that and I do want to thank Ahmad and those that comment on the videos but especially thanks to a mod for catching this one because in a previous lesson the one on fstrings we created three F strings inside of our rock paper scissors game and as he noted we no longer need to use the string Constructor here in lines 63 through 65 because you can just pass game Count into this F string and it will go ahead and show it without using that string Constructor so it will just make it a string as you create that F string so we can go ahead and select all three instances of the Str and that parentheses and I did that with Ctrl D after I selected the first one then I'll press backspace then of course we need to get rid of the closing parentheses on those and this will still work without that string Constructor now that said and I'll save those changes I do want to highlight that online 33 and 35 you still need the string Constructor here and that's because we're applying it to that enum value and then we're calling a string method so we really need a string there specifically and it's not being converted it before that happens so you need to go ahead and leave that string Constructor on these two lines but on 63 through 65 here when we have the game Count player wins and python wins thank you Ahmad for pointing that out because I did miss that it didn't cause an error there was no problem other than it's just not needed so we might as well remove it so now let's scroll to the bottom of the file where we're calling rock paper scissors here and we're going to change this just a little bit I'm going to start by pulling this rock paper scissors definition here where we set it equal to RPS to create our closure I'm just going to control X to cut that and go ahead and put it right above the call to the function here and then let's go back to our hello person because this code that we use with ARG parse is kind of boilerplate and that means we're not changing much of this right here we want to get a name to go ahead and pass to our game and personalize that so let's just copy all of this the import ARG parse all the way down to where we set the args equal to parser.parse RS and Ctrl C to copy and then let's bring it over to our rock paper scissors game and this is going to go before we Define rock paper scissors there so right after the if name equals main I will paste this in now we're not using the language argument so then I can go ahead and remove that piece of it now let's change our description just a little bit so here we'll say provides a personalized so there will just change that word to personalized game experience and now here instead of the name of the person to greet we'll say the name of the person playing the game playing the game and ALT Z once again to wrap any code down that wants to extend off the screen now I'm going to remove an extra line here and then all we need to do is pass our args DOT name to RPS and now we're finished with this part but now we're passing the name value to our rock paper so scissors game but our game is not handling that yet let's start by going to the top of the file where the function begins and now we know it's going to receive a param so I'll say name and let's put a default value just in case this would be used somewhere that it doesn't receive that name so we don't have an error if that happens and we'll just make the default value player one after that we need to go ahead and put in another non-local here and pass name because it's coming from above from RPS as it goes into the play RPS function now let's scroll down and see where else we can use it and one place would be here where we ask the player to enter any of the numbers for rock paper or scissors let's make this an F string and then we'll go ahead and put name in front here so I'll say name and a comma and then we could just make that a lowercase e and then underneath here we could also make this an F string and use that value so we could say whoops not that let's go ahead quote and then we need a curly brace and name and then let's put a comma and then there we'll just put a lowercase y For You must enter as well and you know what let's be a little nicer here let's put please in both of these so we'll say please enter and instead of you must enter we'll say once again please and I need to get rid of that t there go so it would be like Dave please enter one two or three scrolling down just a little bit further we have another line here where it says you so let's change that we already have an F string so now I'll just go ahead and remove that capital Y and we'll put in name once again I'll put in a comma and a lowercase U so Dave you chose and then it would give that value now we have our nested decide winner function so once again we need a non-local for that name value if we're going to use it inside of this function and we definitely are so instead of just you win now we could have an F string so this once again needs to be an F here and then we can put in our name value so name comma you win and now let's just copy this because it's also used on a couple of other lines so Ctrl C there and then we'll highlight this Ctrl D to select the next one that's identical and then Ctrl V to paste and we have that same F string for all of the winning messages let's scroll just a little bit more and here on 56 where we have python wins well that works but we could personalize that a little bit more once again we need to make that an F string if we do and I hope I haven't missed any of those slash in and then let's say sorry comma and put in name once again we could put in a couple of dots so sorry Dave and then we could even put in an emoji so let's find a sad emoji here and I just typed the Windows key plus period on Windows to bring up this Emoji menu so I have a little sad face with the message that python won instead of me okay after that one let's look a little bit further and yes we can provide another one here on line 67. I'm sorry on line 66 where we say player it doesn't need to say player now it could have a personalized name so let's put in name and after that we need an apostrophe s now we can use that apostrophe because on the outside we have double quotes So that single quote doesn't cause an issue and how about we do the same for the play again message so we'll put an F here to make this an F string and instead of play again question mark we'll have play again comma and then we'll put in that personalized name so it's going going to ask play again Dave for example and let's see if there's any other instances as we scroll down and yes we've got thank you for playing which is fine we could personalize that but let's personalize the buy message instead so we can say buy and then just put name in one more time so now we've personalized our entire game now I know I went over that a little bit quickly but we covered Concepts we had before from the F strings to the non-local definitions where we pulled that value into the other functions that were nested okay let's hide the file tree with control B and then I'll press Ctrl and the backtick to open our terminal up again I'll type clear just to clear everything out and now let's go ahead and start our rps8 file once again we can't use the play button like we used to at the top because we need to pass in these arguments so I'm going to say pi and you might need to say python3 just depending on your operating system then I'll go ahead and say rps8 dot pi and I'm going to pass my name with the dash in as Dave and let's see what we get it says Dave please enter and so I'm going to choose Rock and we got a tie game but we can see Dave's wins are zero and that says play again Dave so I did personalize that fairly well let's see if we get something else let me choose a number that is not one two or three like five Dave please enter one two or three so it's also being polite and working as expected another Thai game let's go ahead and play till someone wins and we've got three tie games let's go ahead and play again and now we won and it says Dave you win so that's exactly what we wanted for this game so now you know how to pass command line arguments and we've personalized our rock paper scissors game remember to keep striving for Progress over Perfection and a little progress every day will go a very long way please give this video a like if it's helped you and thank you for watching and subscribing you're helping my channel grow have a great day and let's write more code together very soon
Info
Channel: Dave Gray
Views: 6,385
Rating: undefined out of 5
Keywords: python command line arguments tutorial, python, command line arguments, command line, command arguments, arguments, python arguments, python terminal arguments, how to pass command line arguments in python, how to pass terminal arguments in python, arguments for python files, how to pass values to python programs, python programs, python programming, python tutorial, python beginners, python beginners tutorial, command line arguments for beginners, beginners python, beginners
Id: mZbRRQMJ7Ew
Channel Id: undefined
Length: 21min 24sec (1284 seconds)
Published: Tue May 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.