2 hours 30 minutes
Hello and welcome back to Intermediate Python here on Cyber Eri on demand, I as always I'm your instructor, Joe Perry. And this video is actually part two of less than one. In this video, we're gonna be talking about how to actually use command line arguments. So
in our last video will video and 1/2 we learned to use an interpreter input from our user.
We talked about string token ization. We talked about string interpretation. This video is gonna be a lot simpler. All we're here to do is figure out how we can get our command line arguments.
So we're gonna go back into our er VM here. We've got a new bunch of 64 bit VM, and we're just going to create a new project that we're gonna call that, uh, c m D line Doc Pie.
And inside of that, we're gonna give our standard shebang line.
You've been Python three and we're going to create our main function
And then we're going to invoke May.
Now what? We Our goal here is just to take input from the command line and we're going to do very simple work here. All we're gonna do is we're going to print out the individual arguments one at a time, So that's gonna be our objective. We're gonna put that inside of our main here.
There we go. So in order to do that, we're going to make use of a module called Cysts. Now, I know we haven't really talked about modules deeply yet, and trust me after less after module three of this course you're going to be is familiar with them as you could possibly hope to be. But right now we're just going to understand that to use a module
to use a python library,
all we're going to do is important
not with a condom or not with a comment to rather import cysts, which is gonna be the model we're using. And that's going to give us access to all of the functions on all of functionality contained in that module.
So to make use of python command line arguments, it's actually substantially easier than you might suspect. And we can see just by doing cysts dot org,
we're going to get a list of everything that was passed to this program when it was run
and I wrote lend there so that
I won't lend there so that we can see and test and just make sure that this is actually doing what we think it is. So we're going to just print out the number of the length of the list of the arguments this program received. We're gonna do that, Jemaah,
and we're going to command lying dot pie and first gonna invoke it with zero with no arguments and see what number prints which may surprise some of you.
You see the number one here now the reason for that is because the name of the program the name of the executed ill, is actually considered an argument. So when you're addressing arguments in Python, one of the main Gotsch is is an off by one error where you address item zero thinking that you're addressing the first argument.
Instead, you generally want to start with one which is going to the first argument past your program,
and we could see that by demonstrating a B, c d e
so notionally we only have five arguments. But if we count our program's name as an argument, we should be getting back
the number six. So that's how we can get the the length the number of arguments have given our program. Now, we're just going to look it very easily how we can use those. So instead of doing a print, Len here,
we're just going to say four I in
and again, we're going to run command line dot pie by itself, and you could see what it prints out. There is the name of the program and we'll do 12345
And it prints out each of these arguments on their own line,
so that's actually all there is to it. For those of you who have worked with other programming languages, I know that taking arguments and other languages could be far more complex and difficult in python. All you have to do is import cysts, which is the module that Python uses to interact with the interpreter directly and then just make use of cyst out our govt,
and that will give you all of the access to the grand line arguments you could possibly need.
So that's actually gonna be it for less than one. Hopefully, this has been useful information to you. Now you understand how to take token ized input, everything that we did in our previous video of less than one. You know, all of the uses of tokens and the string splitting. You could do all of that with command line arguments in exactly the same way. As long as you're addressing them out of the system R V list.
So those were the two objectives. We learned to use an interpreter input from our users,
and we learned to use command line arguments.
That's gonna be it for this lesson. Thank you all for watching. As always, I have been your instructor, Joe Perry, and you're watching intermediate python on Cyber eri on demand.