1 00:00:06,660 --> 00:00:09,690 - Applications can receive command line arguments, 2 00:00:09,690 --> 00:00:11,430 not breaking news. 3 00:00:11,430 --> 00:00:14,520 For example, when we run our application, 4 00:00:14,520 --> 00:00:17,640 our application can optionally receive an argument 5 00:00:17,640 --> 00:00:20,760 specifying the name of the data file to read. 6 00:00:20,760 --> 00:00:22,620 If you run the application like this, 7 00:00:22,620 --> 00:00:26,520 it'll actually pass in two arguments into the application. 8 00:00:26,520 --> 00:00:29,430 The first argument, argument zero, 9 00:00:29,430 --> 00:00:32,010 will be the name of the application 10 00:00:32,010 --> 00:00:33,120 and that might be useful. 11 00:00:33,120 --> 00:00:35,130 Imagine you've got some kind of error. 12 00:00:35,130 --> 00:00:37,710 You might wanna display the name of the application 13 00:00:37,710 --> 00:00:38,613 to the user. 14 00:00:39,510 --> 00:00:44,310 And arg1 is the actual real parameter of interest 15 00:00:44,310 --> 00:00:47,883 which in this case would be data.text. 16 00:00:50,100 --> 00:00:54,050 Okay, so we've got some code in the util module 17 00:00:56,220 --> 00:00:59,520 which grabs the command line from the user. 18 00:00:59,520 --> 00:01:02,910 Our application is actually a bit more resilient 19 00:01:02,910 --> 00:01:04,710 and most applications are 20 00:01:04,710 --> 00:01:09,210 if the user doesn't specify this extra argument, 21 00:01:09,210 --> 00:01:12,960 then it'll default to data.text anyway. 22 00:01:12,960 --> 00:01:14,760 So let's see how that works. 23 00:01:14,760 --> 00:01:17,730 Let's see how our application actually processes 24 00:01:17,730 --> 00:01:19,560 the command line arguments. 25 00:01:19,560 --> 00:01:22,680 We start off looking at util.rs, 26 00:01:22,680 --> 00:01:25,200 the function parse_command_line, 27 00:01:25,200 --> 00:01:28,500 and then we'll see where it's called from in main 28 00:01:28,500 --> 00:01:31,923 and see what it does with the input. 29 00:01:33,000 --> 00:01:37,143 Right, so in util.rs, 30 00:01:38,280 --> 00:01:41,250 here's the function to parse the command lane. 31 00:01:41,250 --> 00:01:45,570 So first thing I'd note is that it returns an option. 32 00:01:45,570 --> 00:01:48,000 Okay, I'm looking for the name of the file 33 00:01:48,000 --> 00:01:50,430 to read in, like data.text. 34 00:01:50,430 --> 00:01:53,640 If the user doesn't specify that parameter, 35 00:01:53,640 --> 00:01:58,440 then this function returns none to say not specified. 36 00:01:58,440 --> 00:02:00,720 Otherwise, it'll return some 37 00:02:00,720 --> 00:02:02,280 and it'll be the name of the file 38 00:02:02,280 --> 00:02:03,873 that the user passed in. 39 00:02:05,400 --> 00:02:08,520 Right, so to read command line arguments, 40 00:02:08,520 --> 00:02:10,320 in the standard crate, 41 00:02:10,320 --> 00:02:12,173 there's a module called env 42 00:02:12,173 --> 00:02:16,320 and the env crate contains lots of useful functions 43 00:02:16,320 --> 00:02:20,370 for getting information about the ambient environment 44 00:02:20,370 --> 00:02:23,100 in which your application is executing. 45 00:02:23,100 --> 00:02:26,250 So for example, you can in instead of env, 46 00:02:26,250 --> 00:02:28,350 there's an args function, 47 00:02:28,350 --> 00:02:29,730 and the args function, it gives you back 48 00:02:29,730 --> 00:02:32,970 the command line arguments including argument zero 49 00:02:32,970 --> 00:02:34,620 which is the name of the application, 50 00:02:34,620 --> 00:02:37,530 and then argument one what we're looking for 51 00:02:37,530 --> 00:02:40,560 which would be the name of the file to read in. 52 00:02:40,560 --> 00:02:42,030 So it's actually worthwhile 53 00:02:42,030 --> 00:02:44,130 just taking a quick look at std:env 54 00:02:44,130 --> 00:02:46,290 to see what environmental information 55 00:02:46,290 --> 00:02:48,240 is actually available in there. 56 00:02:48,240 --> 00:02:52,320 So I've opened up the Rust documentation 57 00:02:52,320 --> 00:02:56,040 for the env module in the standard crate. 58 00:02:56,040 --> 00:03:00,570 And if you look at the functions defined in this module, 59 00:03:00,570 --> 00:03:04,200 you'll find interesting, there's an args function, 60 00:03:04,200 --> 00:03:06,150 which will give you the arguments 61 00:03:06,150 --> 00:03:09,330 the programmers started with from the command line. 62 00:03:09,330 --> 00:03:11,040 Okay, there are other functions as well. 63 00:03:11,040 --> 00:03:13,860 You can figure out what was the directory 64 00:03:13,860 --> 00:03:16,200 from which my application was executed. 65 00:03:16,200 --> 00:03:18,633 The current_dir function, it's quite useful. 66 00:03:19,530 --> 00:03:21,060 You can also get vars, 67 00:03:22,050 --> 00:03:26,070 vars here gives you the environment variables. 68 00:03:26,070 --> 00:03:29,583 So an application runs on an operating system, 69 00:03:30,540 --> 00:03:32,220 in that operating system, 70 00:03:32,220 --> 00:03:34,260 environment variables will be defined. 71 00:03:34,260 --> 00:03:37,113 So you can pluck out environment variables from here. 72 00:03:38,160 --> 00:03:41,910 Okay, so lots of useful functions. 73 00:03:41,910 --> 00:03:43,080 It's the args function 74 00:03:43,080 --> 00:03:44,980 that we're particularly interested in. 75 00:03:45,900 --> 00:03:49,230 And the args function, when you call the args function, 76 00:03:49,230 --> 00:03:51,480 it returns a structure type called args. 77 00:03:52,470 --> 00:03:56,910 Okay, now it turns out that args is more like an iterator 78 00:03:56,910 --> 00:03:58,620 over the command line arguments 79 00:03:58,620 --> 00:04:02,430 rather than the collection of arguments themselves. 80 00:04:02,430 --> 00:04:06,150 So typically what you would do is you would then iterate 81 00:04:06,150 --> 00:04:09,270 over this object and grab element zero, 82 00:04:09,270 --> 00:04:11,790 element one, element two, element three. 83 00:04:11,790 --> 00:04:16,080 An alternative approach is to say give me all the items 84 00:04:16,080 --> 00:04:18,390 that this iterator could iterate over. 85 00:04:18,390 --> 00:04:21,450 Just give me the values as a list. 86 00:04:21,450 --> 00:04:23,910 So the args object has a collect function 87 00:04:23,910 --> 00:04:25,893 which turns out to be quite useful. 88 00:04:26,820 --> 00:04:27,660 And that's what I've used here. 89 00:04:27,660 --> 00:04:29,340 When I call args, 90 00:04:29,340 --> 00:04:31,890 it'll give me back an iterator like a pointer. 91 00:04:31,890 --> 00:04:34,050 Here's argument zero, here's argument one. 92 00:04:34,050 --> 00:04:36,360 I can say I can't be bothered to iterate. 93 00:04:36,360 --> 00:04:38,160 Just collect all the items together 94 00:04:38,160 --> 00:04:40,950 into a list or rather a vector. 95 00:04:40,950 --> 00:04:45,150 So this'll give you back a vector of command line arguments. 96 00:04:45,150 --> 00:04:47,340 Argument zero is the program name 97 00:04:47,340 --> 00:04:51,000 and argument one hopefully would be the filename to process. 98 00:04:51,000 --> 00:04:54,070 So we get that data back into a vector of string 99 00:04:55,050 --> 00:04:57,300 and then we see whether the user 100 00:04:57,300 --> 00:05:00,210 actually did specify a filename at all. 101 00:05:00,210 --> 00:05:02,610 If the args length is two, 102 00:05:02,610 --> 00:05:06,150 then the user specified argument zero, name of program, 103 00:05:06,150 --> 00:05:08,760 argument one, filename. 104 00:05:08,760 --> 00:05:13,410 So in that case, grab argument one as a string, clone it. 105 00:05:13,410 --> 00:05:15,450 Okay, think about ownership of strings here 106 00:05:15,450 --> 00:05:16,680 just to be on the safe side. 107 00:05:16,680 --> 00:05:19,950 And then we turn some, remember the option was indicating 108 00:05:19,950 --> 00:05:22,920 whether these use a specified filename as a string. 109 00:05:22,920 --> 00:05:24,780 Well, that's the value that I'm gonna put in there 110 00:05:24,780 --> 00:05:26,910 like data.text or whatever filename 111 00:05:26,910 --> 00:05:29,070 is specified from the command line. 112 00:05:29,070 --> 00:05:31,770 If the argument length wasn't two, 113 00:05:31,770 --> 00:05:33,450 then I'm just gonna return none. 114 00:05:33,450 --> 00:05:36,360 Maybe the user didn't pass in the name of the file. 115 00:05:36,360 --> 00:05:38,820 Maybe they passed in three arguments or four arguments, 116 00:05:38,820 --> 00:05:41,220 but in any way, it's not what I was expecting. 117 00:05:41,220 --> 00:05:43,110 So in that case, I'd just say none, 118 00:05:43,110 --> 00:05:45,540 which is kind of like the equivalent of a null pointer 119 00:05:45,540 --> 00:05:47,250 to say the user didn't pass in, 120 00:05:47,250 --> 00:05:50,673 did not pass in a command line argument. 121 00:05:51,750 --> 00:05:54,630 Okay, so that's my parse command line function. 122 00:05:54,630 --> 00:05:56,160 It returns an option. 123 00:05:56,160 --> 00:05:58,350 And I call that at the start of main. 124 00:05:58,350 --> 00:06:02,310 So let's take a look at main, when the application starts, 125 00:06:02,310 --> 00:06:05,130 from my util module we just looked at, 126 00:06:05,130 --> 00:06:09,540 the parse command line function that returns an option 127 00:06:09,540 --> 00:06:14,130 and which could be either some filename or none. 128 00:06:14,130 --> 00:06:17,760 So a common approach when you get back an option is to say 129 00:06:17,760 --> 00:06:20,400 give me the value and if it isn't the value, 130 00:06:20,400 --> 00:06:21,870 then use this as a default. 131 00:06:21,870 --> 00:06:23,340 That's the unwrap_or function. 132 00:06:23,340 --> 00:06:26,100 We looked at that, oh, way back in lesson five 133 00:06:26,100 --> 00:06:28,950 or something that seems like a lifetime ago. 134 00:06:28,950 --> 00:06:32,340 So if the user did pass in a filename, 135 00:06:32,340 --> 00:06:34,110 then it would give me it back. 136 00:06:34,110 --> 00:06:36,300 And if the user didn't specify a filename, 137 00:06:36,300 --> 00:06:38,760 then the option would be none. 138 00:06:38,760 --> 00:06:41,040 When I try to unwrap it, it'll kind of fail 139 00:06:41,040 --> 00:06:43,290 and it'll give me back this string as a default. 140 00:06:43,290 --> 00:06:45,450 So that's basically the default filename 141 00:06:45,450 --> 00:06:48,450 that the user's going to specify. 142 00:06:48,450 --> 00:06:51,510 Okay, and then from there, obviously at that point, 143 00:06:51,510 --> 00:06:53,010 we're then into file handling 144 00:06:53,010 --> 00:06:55,110 to read the data from that file. 145 00:06:55,110 --> 00:06:58,530 So if ever you want to do command line arguments, 146 00:06:58,530 --> 00:07:00,570 basically you can take this code 147 00:07:00,570 --> 00:07:03,360 and it's gonna look pretty much like this every time. 148 00:07:03,360 --> 00:07:05,773 It's quite idiomatic. Okay, so there you go.