In this video, we will talk about how to
handle command-line arguments with the
clap library. Clap stands for a command
line argument parser, and that is exactly
what we are going to use it for. We are
going to accept three different kinds of
arguments, the first will be in file, an
optional positional argument for a file
to read instead of standard in. The
second will be out file, an option that
requires a value for a filename to
write to instead of standard out. The
third will be silent, which is just a
flag that you can turn on or off, which
has the exact same meaning as our PV
silent environment variable, it will
silence our standard error progress
output. The first thing we need to do is
add clap to our dependencies.
Next, we need to import the items that we
need.
App is where you start with clap, and arg
is used for each of the arguments.
All right, let's make a local variable to
help match us to store stuff in. I'm
going to call app new pipe viewer that's
the name of our program for the
auto-generated help, and then we're using
the Builder pattern here, so I'm going to
give it a bunch of different arguments.
So the first one we're going to give it
is in file. Now this is a positional
argument so all I have to give it is
help, because the default is to get
positional arguments. I want to say read
from a file instead of standard in, and
then continuing our Builder pattern, the
next argument it's going to be out file.
Now out file is going to be an option
that takes a value, so we're gonna
specify the short name for option, which
is going to be oh, it's a long name for
our option which is gonna be out file,
we're gonna say it takes a value so you
can't specify this option without giving
it a value, because we need the file name.
And then of course the help output for
the auto-generated help that clap will
make for us. Then next our third argument
is going to be silent, so once again
we're gonna do Arg
and give us another argument with the
name called silent, the name is what we
retrieve out of the clap library, so when
we want to have that long name that you
can specify we actually have to put the
long name as well. So we did a short name
of s, a long name of silent, and then
that's all the arguments we want, so we
can call get matches, and that's why I
named our local variable matches. Let's
get our three values out of matches, so
starting with in file we call matches,
there's a method called value of that
returns an option that either is none or
it's a string containing the value that
we want. So since it's an option we can
do unwrap or default, like we did with PV
silent, so either it gives us the string
that is there or if it's a none then it
gives us an empty string, the default
value for a string, did the exact same
thing for out file,
then for silent we need to handle both.
So we're gonna use an if expression so
I'm gonna say if matches is present and
then give it silent. So if that is true,
then the value of silent will be true,
otherwise let's go get it from the
environment variable. So the command line
version takes priority.
I left the semicolons off of those if
expression branch arms, so that they get
returned as devalued the if expression.
Okay, let's debug out the values of our
three variables so we can take a look at
them. Here's one thing to remember when
you're dealing with command-line
arguments and Cargo, Cargo accepts its
own options and arguments. So what you
need to do is put - - when you want to
start the arguments and options for your
program, and then Cargo won't interpret
them. So when you pass our program at -
H you see claps built-in help generation,
that's just something you get for free,
there are lots of ways to influence it
and change how it looks I'm not going to
go over those in this video.
Let's go ahead and clear the screen and
try out some of these arguments. So if we
do no arguments we see in file as blank,
out file as blank and silent is false,
okay. So let's run it again and give it
some file, now in file is some file, ok.
Control C out of it again run it again,
this time let's add - - out file and
give it a name
maybe file dot out, and run it again, ok
there we go out file is file that out.
Sontrol C run it again let's do - s, try
out a short argument now silent is true,
ok let's take off the - s and make sure
our environment variable still works set
that to some non blank silent is still
true. There we go we can see that this
all works nicely, so I'm going to go back
and remove our debug statement, and we're
all done. In the next video, we will talk
about reading and writing files, using
buffered I/O and traits.