So now let's look at how we can accept and use command line arguments.
The first thing we need to do is bring standard.
In var into our scope.
So inside of Main, let's now create a vector called args.
So let args and then we know it's going to be a vector of string and we can say environment that's what
a short for args and then skip one and I'll explain that in a second and then we have a collect iterator
for us.
So we're going to use Skip one because we run our program.
That is actually the first argument.
But we don't want that one.
We want everything that follows our.
Program name.
And that's going to give us the rest of everything else.
So we also know that we want it.
We want for arguments.
We don't want any less.
And we also don't want anymore.
So we want our length.
Basically two equal four.
So if it does not equal four, then we want to print our help out for us.
So now we can remove this.
And now we can also say.
Frontline and we want to provide them a useful error message.
Wrong number of argument is given expected for.
Got another.
Value pressed in.
So we'll say air.
And then we want it to be read and we also want it to be bold.
And then we want to look at how many argument we actually were printed out.
And then if we get to this point, we don't want our process to continue, so we want it to actually
exit.
And then we wanted to exit with air code one, which signifies.
That are.
Program did not.
Run successfully.
So now let's do a couple of runs.
So we'll say cargo run and let's see what it does.
So we see we have find and replace.
Good.
So that was our print help function being called and then we see error wrong number of arguments given
expected four got zero and now we see also our process didn't exit successfully which is right here.
So great.
So we see that it was running correctly.
So now let's try running it and give it four arguments.
So we'll say ARG one or two, ARG three and ARG four and everything.
Ran correctly.
So obviously we don't have the logic in yet to handle our arguments, but this is a great start to our
program and now we know how we can utilize accepting arguments in from the command line and making sure
that they are being stored correctly inside of our argument, our args vector.
So now let's look at how we can actually structure our program correctly and separate all of this logic
into different files.
That way, we only use Main to run our program and not handle all of our core logic.