2.4 Command-Line Arguments
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
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
00:15
in our last video will video and 1/2 we learned to use an interpreter input from our user.
00:20
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.
00:30
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.
00:42
And inside of that, we're gonna give our standard shebang line.
00:46
You've been Python three and we're going to create our main function
00:52
and return.
00:54
No.
00:55
And then we're going to invoke May.
00:58
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.
01:19
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
01:38
to use a python library,
01:41
all we're going to do is important
01:44
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.
01:56
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,
02:10
we're going to get a list of everything that was passed to this program when it was run
02:15
and I wrote lend there so that
02:17
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,
02:36
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.
02:45
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.
03:02
Instead, you generally want to start with one which is going to the first argument past your program,
03:07
and we could see that by demonstrating a B, c d e
03:10
so notionally we only have five arguments. But if we count our program's name as an argument, we should be getting back
03:17
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,
03:30
we're just going to say four I in
03:34
sis
03:36
RV
03:38
print.
03:39
Uh,
03:44
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
03:53
And it prints out each of these arguments on their own line,
03:57
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,
04:15
and that will give you all of the access to the grand line arguments you could possibly need.
04:19
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.
04:40
So those were the two objectives. We learned to use an interpreter input from our users,
04:44
and we learned to use command line arguments.
04:47
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.
Up Next
Similar Content