1 00:00:00,280 --> 00:00:03,129 In this video, we will talk about how to 2 00:00:03,129 --> 00:00:05,230 handle command-line arguments with the 3 00:00:05,230 --> 00:00:08,679 clap library. Clap stands for a command 4 00:00:08,679 --> 00:00:10,840 line argument parser, and that is exactly 5 00:00:10,840 --> 00:00:14,139 what we are going to use it for. We are 6 00:00:14,139 --> 00:00:16,449 going to accept three different kinds of 7 00:00:16,449 --> 00:00:19,990 arguments, the first will be in file, an 8 00:00:19,990 --> 00:00:23,740 optional positional argument for a file 9 00:00:23,740 --> 00:00:26,439 to read instead of standard in. The 10 00:00:26,439 --> 00:00:29,740 second will be out file, an option that 11 00:00:29,740 --> 00:00:32,649 requires a value for a filename to 12 00:00:32,649 --> 00:00:35,920 write to instead of standard out. The 13 00:00:35,920 --> 00:00:39,579 third will be silent, which is just a 14 00:00:39,579 --> 00:00:42,520 flag that you can turn on or off, which 15 00:00:42,520 --> 00:00:45,430 has the exact same meaning as our PV 16 00:00:45,430 --> 00:00:47,620 silent environment variable, it will 17 00:00:47,620 --> 00:00:50,620 silence our standard error progress 18 00:00:50,620 --> 00:00:53,230 output. The first thing we need to do is 19 00:00:53,230 --> 00:01:00,949 add clap to our dependencies. 20 00:01:00,949 --> 00:01:03,800 Next, we need to import the items that we 21 00:01:03,800 --> 00:01:08,030 need. 22 00:01:08,030 --> 00:01:11,659 App is where you start with clap, and arg 23 00:01:11,659 --> 00:01:15,300 is used for each of the arguments. 24 00:01:15,300 --> 00:01:17,340 All right, let's make a local variable to 25 00:01:17,340 --> 00:01:19,170 help match us to store stuff in. I'm 26 00:01:19,170 --> 00:01:22,290 going to call app new pipe viewer that's 27 00:01:22,290 --> 00:01:24,300 the name of our program for the 28 00:01:24,300 --> 00:01:26,550 auto-generated help, and then we're using 29 00:01:26,550 --> 00:01:28,470 the Builder pattern here, so I'm going to 30 00:01:28,470 --> 00:01:30,680 give it a bunch of different arguments. 31 00:01:30,680 --> 00:01:32,760 So the first one we're going to give it 32 00:01:32,760 --> 00:01:36,120 is in file. Now this is a positional 33 00:01:36,120 --> 00:01:37,920 argument so all I have to give it is 34 00:01:37,920 --> 00:01:40,590 help, because the default is to get 35 00:01:40,590 --> 00:01:43,140 positional arguments. I want to say read 36 00:01:43,140 --> 00:01:44,970 from a file instead of standard in, and 37 00:01:44,970 --> 00:01:47,910 then continuing our Builder pattern, the 38 00:01:47,910 --> 00:01:51,450 next argument it's going to be out file. 39 00:01:51,450 --> 00:01:54,330 Now out file is going to be an option 40 00:01:54,330 --> 00:01:56,130 that takes a value, so we're gonna 41 00:01:56,130 --> 00:01:58,620 specify the short name for option, which 42 00:01:58,620 --> 00:02:01,200 is going to be oh, it's a long name for 43 00:02:01,200 --> 00:02:03,210 our option which is gonna be out file, 44 00:02:03,210 --> 00:02:06,060 we're gonna say it takes a value so you 45 00:02:06,060 --> 00:02:07,860 can't specify this option without giving 46 00:02:07,860 --> 00:02:09,360 it a value, because we need the file name. 47 00:02:09,360 --> 00:02:11,610 And then of course the help output for 48 00:02:11,610 --> 00:02:14,220 the auto-generated help that clap will 49 00:02:14,220 --> 00:02:17,850 make for us. Then next our third argument 50 00:02:17,850 --> 00:02:21,030 is going to be silent, so once again 51 00:02:21,030 --> 00:02:22,590 we're gonna do Arg 52 00:02:22,590 --> 00:02:25,170 and give us another argument with the 53 00:02:25,170 --> 00:02:27,480 name called silent, the name is what we 54 00:02:27,480 --> 00:02:30,750 retrieve out of the clap library, so when 55 00:02:30,750 --> 00:02:33,480 we want to have that long name that you 56 00:02:33,480 --> 00:02:35,520 can specify we actually have to put the 57 00:02:35,520 --> 00:02:37,680 long name as well. So we did a short name 58 00:02:37,680 --> 00:02:42,060 of s, a long name of silent, and then 59 00:02:42,060 --> 00:02:44,010 that's all the arguments we want, so we 60 00:02:44,010 --> 00:02:46,920 can call get matches, and that's why I 61 00:02:46,920 --> 00:02:49,950 named our local variable matches. Let's 62 00:02:49,950 --> 00:02:52,290 get our three values out of matches, so 63 00:02:52,290 --> 00:02:55,050 starting with in file we call matches, 64 00:02:55,050 --> 00:02:57,480 there's a method called value of that 65 00:02:57,480 --> 00:03:00,270 returns an option that either is none or 66 00:03:00,270 --> 00:03:02,610 it's a string containing the value that 67 00:03:02,610 --> 00:03:05,040 we want. So since it's an option we can 68 00:03:05,040 --> 00:03:07,680 do unwrap or default, like we did with PV 69 00:03:07,680 --> 00:03:10,230 silent, so either it gives us the string 70 00:03:10,230 --> 00:03:12,000 that is there or if it's a none then it 71 00:03:12,000 --> 00:03:13,769 gives us an empty string, the default 72 00:03:13,769 --> 00:03:15,780 value for a string, did the exact same 73 00:03:15,780 --> 00:03:18,340 thing for out file, 74 00:03:18,340 --> 00:03:20,440 then for silent we need to handle both. 75 00:03:20,440 --> 00:03:23,650 So we're gonna use an if expression so 76 00:03:23,650 --> 00:03:26,980 I'm gonna say if matches is present and 77 00:03:26,980 --> 00:03:30,700 then give it silent. So if that is true, 78 00:03:30,700 --> 00:03:33,400 then the value of silent will be true, 79 00:03:33,400 --> 00:03:36,129 otherwise let's go get it from the 80 00:03:36,129 --> 00:03:38,290 environment variable. So the command line 81 00:03:38,290 --> 00:03:39,610 version takes priority. 82 00:03:39,610 --> 00:03:42,310 I left the semicolons off of those if 83 00:03:42,310 --> 00:03:44,500 expression branch arms, so that they get 84 00:03:44,500 --> 00:03:46,599 returned as devalued the if expression. 85 00:03:46,599 --> 00:03:50,980 Okay, let's debug out the values of our 86 00:03:50,980 --> 00:03:52,840 three variables so we can take a look at 87 00:03:52,840 --> 00:03:55,329 them. Here's one thing to remember when 88 00:03:55,329 --> 00:03:56,799 you're dealing with command-line 89 00:03:56,799 --> 00:04:00,310 arguments and Cargo, Cargo accepts its 90 00:04:00,310 --> 00:04:03,730 own options and arguments. So what you 91 00:04:03,730 --> 00:04:07,060 need to do is put - - when you want to 92 00:04:07,060 --> 00:04:09,519 start the arguments and options for your 93 00:04:09,519 --> 00:04:11,709 program, and then Cargo won't interpret 94 00:04:11,709 --> 00:04:14,410 them. So when you pass our program at - 95 00:04:14,410 --> 00:04:18,548 H you see claps built-in help generation, 96 00:04:18,548 --> 00:04:20,560 that's just something you get for free, 97 00:04:20,560 --> 00:04:23,709 there are lots of ways to influence it 98 00:04:23,709 --> 00:04:25,630 and change how it looks I'm not going to 99 00:04:25,630 --> 00:04:26,889 go over those in this video. 100 00:04:26,889 --> 00:04:29,169 Let's go ahead and clear the screen and 101 00:04:29,169 --> 00:04:32,020 try out some of these arguments. So if we 102 00:04:32,020 --> 00:04:34,450 do no arguments we see in file as blank, 103 00:04:34,450 --> 00:04:36,940 out file as blank and silent is false, 104 00:04:36,940 --> 00:04:39,580 okay. So let's run it again and give it 105 00:04:39,580 --> 00:04:44,680 some file, now in file is some file, ok. 106 00:04:44,680 --> 00:04:47,289 Control C out of it again run it again, 107 00:04:47,289 --> 00:04:50,110 this time let's add - - out file and 108 00:04:50,110 --> 00:04:51,010 give it a name 109 00:04:51,010 --> 00:04:55,120 maybe file dot out, and run it again, ok 110 00:04:55,120 --> 00:04:57,490 there we go out file is file that out. 111 00:04:57,490 --> 00:05:01,900 Sontrol C run it again let's do - s, try 112 00:05:01,900 --> 00:05:04,870 out a short argument now silent is true, 113 00:05:04,870 --> 00:05:06,910 ok let's take off the - s and make sure 114 00:05:06,910 --> 00:05:09,430 our environment variable still works set 115 00:05:09,430 --> 00:05:11,200 that to some non blank silent is still 116 00:05:11,200 --> 00:05:13,870 true. There we go we can see that this 117 00:05:13,870 --> 00:05:15,820 all works nicely, so I'm going to go back 118 00:05:15,820 --> 00:05:18,580 and remove our debug statement, and we're 119 00:05:18,580 --> 00:05:22,000 all done. In the next video, we will talk 120 00:05:22,000 --> 00:05:24,639 about reading and writing files, using 121 00:05:24,639 --> 00:05:29,639 buffered I/O and traits.