The first thing we need to do is obviously just create a new project.
So I went ahead and did that.
I named mine Clive for command line interface, but feel free to name yours whatever you want.
So the next thing we're going to do is add in a couple of dependencies into our cargo Tamil file.
And the first one is going to be called text color riser.
We'll set the version to one and then rig X and we'll also set the version to one.
So just to kind of show you what these dependencies are.
Text Polarizer, it's just going to give us a couple of functions to enable and disable coloring.
And this is just going to allow us to add some style to our output in the terminal.
And then we have our Reg X crate, which is short for regular expression, and this creates going to
allow us to look for a certain expression in our file and then we'll be able to.
Change it based on some arguments that we're going to accept in.
So this is a very important crate for what our program is going to do.
So.
Just to come back and look in here.
We see we have reg x equals to one.
Argo does is really cool thing where crate set of reached a 1.0 version in rust.
They begin to follow semantic versioning.
This means that all version one changes should maintain compatibility with older versions.
So version 1.2 will be compatible with 1.3 and so on.
But once version 2.0 is released, then 2.0 is.
No longer required to be compatible with 1.0 versions.
So another thing is, if you saw this in the Reg X.
We're at version 1.5.4 is the latest, but in here we have it only as version one.
So cargo also does this thing where if we specify, hey, we just want.
One for the version is going to automatically use the newest available version prior to version two.
So this one is essentially equivalent to version 1.5.4.
So that's all we have to do in our cargo file.
So now we can start looking inside of Main.
And we can start thinking about our program structure.
So our program is going to take four arguments a string to search for, a string to replace it with
an input file and an output file so we can keep all of these arguments together by grouping them into
and arguments struck.
And all of our fields are going to be stringed.
So we're going to have a pattern, which is what we're looking for.
We're going to have replace, which is what we're going to substitute in for our pattern.
And then we're going to have our input file and our output file.
So to help us debug, we're actually going to add in an attribute called debug.
So we're going to derive debug and this is just going to allow us to use.
Standard output, a little better to kind of format how we want to be able to print out our argument
struck to be able to see the values associated with our field.
So it's just going to help us kind of debug our struc.
And now we see that we have a bunch of warnings already for having dead code.
So I actually want to suppress these that way when we output to the terminal, we don't get a ton of
warnings outputted with it.
I'd like to keep our output to the terminal pretty clean.
And that's what allow dead code does.
So now that we have our struct set up, let's just go ahead and bring in our text polarizer and we'll
just bring in everything that's within the crate, which is what that wild card is going to do for us.
It tells it to just make everything inside that crate available to us.
So now we can create a new function and we'll call it print help.
And in here, we're going to use a different form of print line.
And it's going to be the print line macro.
And the E print line is going to print to the standard air with the new line.
So it is equivalent to print line, except the output goes to standard air instead of standard out.
We should only use e print line for air and progress messages.
Otherwise you should continue to use print line instead.
So in here, let's just start adding in some messages to help the user understand how to use our program.
So what is our program going to do?
It's going to replace a string with a new string.
And then our program, because we put in our curly brackets, it's going to be called find and replace.
And then we want it to be green.
So that's how we change the color in this case.
And then we also want to print out how to use our program.
So we'll say usage
target string.
Replacement string input file and then we'll have out put.
File.
And that's all we need to do.
So let's come up here.
Let's call our function.
And now let's come down here and run it.
And it's going to build our dependencies for us.
Now we see that we have find and replace and our green text that we specified replace a string with
the new string.
And then we see that it has usage target string replacement string input file and output file.
So everything looks good and everything was printed out the way it should be.
So in the next lecture, let's look at how we can take in user arguments from the command line.