1 00:00:00,330 --> 00:00:03,090 ‫Earlier we discussed how to use the command line arguments. 2 00:00:03,090 --> 00:00:08,400 ‫And this part you will learn how to handle user input and make a program that does basic operations. 3 00:00:08,520 --> 00:00:15,810 ‫We will also see how to display a basic manual to the user explaining how our program works and what 4 00:00:15,810 --> 00:00:16,860 ‫are its requirements. 5 00:00:16,890 --> 00:00:22,190 ‫This is necessary since our application doesn't use console line to ask the user for input. 6 00:00:22,200 --> 00:00:29,040 ‫Instead, all the data that our app needs will be provided at the start using the command line arguments. 7 00:00:29,250 --> 00:00:39,360 ‫So what we can do is we can say something like provide arguments next time, pass help if you want to 8 00:00:39,360 --> 00:00:41,370 ‫get more details, for example. 9 00:00:42,270 --> 00:00:42,450 ‫Okay. 10 00:00:42,480 --> 00:00:47,160 ‫So this would be then the statement that the user sees once he starts the application. 11 00:00:47,160 --> 00:00:51,330 ‫And now let's actually check if the user entered help. 12 00:00:51,600 --> 00:00:54,690 ‫So if he entered help, we are going to give him some help. 13 00:00:54,690 --> 00:00:56,880 ‫We're going to say instructions. 14 00:00:56,880 --> 00:00:59,970 ‫Use one of the following commands followed by two numbers. 15 00:01:00,120 --> 00:01:02,670 ‫Use add to add two numbers. 16 00:01:02,670 --> 00:01:05,190 ‫Sub two subtract two numbers. 17 00:01:07,070 --> 00:01:09,500 ‫Our application will then pause and return. 18 00:01:09,500 --> 00:01:15,200 ‫So now let's actually handle the user input because rule number one, never trust the user because the 19 00:01:15,230 --> 00:01:16,760 ‫user will make mistakes. 20 00:01:16,760 --> 00:01:22,940 ‫And it's our job to make our application handle all of these possible input mistakes that the user might 21 00:01:22,940 --> 00:01:23,780 ‫provide. 22 00:01:24,050 --> 00:01:29,780 ‫So first, our application needs three arguments to do its job so it's safe to quit the application 23 00:01:29,780 --> 00:01:36,050 ‫if either more or less than three arguments were provided, since we need exactly to so we could do 24 00:01:36,050 --> 00:01:37,340 ‫something like this here. 25 00:01:38,450 --> 00:01:43,190 ‫If our length is not equal three then write invalid arguments. 26 00:01:43,190 --> 00:01:47,600 ‫Please use the help command for instructions and read the key. 27 00:01:49,560 --> 00:01:55,830 ‫So now, since we made sure that we won't go any further in our app unless we have three arguments, 28 00:01:55,830 --> 00:02:01,230 ‫let's check if the format of arguments is correct, since our arguments will be in the format of a command 29 00:02:01,230 --> 00:02:02,460 ‫followed by two numbers. 30 00:02:02,460 --> 00:02:09,570 ‫We want to check if the two numbers are in a correct format and we do that by using the float. 31 00:02:09,570 --> 00:02:17,280 ‫Try parse functionality so which takes a string that it will try to pass into float and a float variable 32 00:02:17,310 --> 00:02:22,920 ‫that it will use to save the parsed data if the parsing operation was successful. 33 00:02:22,920 --> 00:02:29,760 ‫Therefore, we're going to add this code here saying this number one parsed and this number two parsed. 34 00:02:29,940 --> 00:02:36,480 ‫So basically we get those values right and we can now go ahead and now we can check if the user passed 35 00:02:36,480 --> 00:02:41,100 ‫or entered something that is actually going to be a floating number or not. 36 00:02:41,250 --> 00:02:45,000 ‫So now we can check if those two are true or not. 37 00:02:45,090 --> 00:02:46,800 ‫So if they are not true. 38 00:02:46,800 --> 00:02:49,410 ‫So both of them or either of them. 39 00:02:49,410 --> 00:02:53,550 ‫So if either of them is false, then we can just say invalid arguments. 40 00:02:53,550 --> 00:02:59,820 ‫Please use the help command for instructions and then read key and quit the application. 41 00:03:01,080 --> 00:03:08,700 ‫So now at this point, we can go ahead and actually calculate the result using a float where we are 42 00:03:08,700 --> 00:03:15,030 ‫going to store the results and then we can use a switch case statement like so where we take arcs at 43 00:03:15,030 --> 00:03:17,100 ‫position zero as the switch statement. 44 00:03:17,100 --> 00:03:18,900 ‫So we're comparing is it add. 45 00:03:18,900 --> 00:03:25,080 ‫So if the user entered add, then we're going to use NUM one, which was this outflow number one. 46 00:03:26,340 --> 00:03:28,060 ‫And number two. 47 00:03:28,080 --> 00:03:33,640 ‫So we're going to add them together, store the result and result and write it onto the console. 48 00:03:33,660 --> 00:03:36,930 ‫And at this point, break out of the switch case. 49 00:03:37,470 --> 00:03:44,280 ‫And then if it was set so the user entered up as the first entry at ARX zero, then it will subtract 50 00:03:44,580 --> 00:03:46,650 ‫NUM two from num one. 51 00:03:47,400 --> 00:03:50,190 ‫And then it will display the results here. 52 00:03:51,480 --> 00:03:57,600 ‫And finally, in the default case, if none of the two options add or sub were given, then it will 53 00:03:57,600 --> 00:03:58,950 ‫say invalid arguments. 54 00:03:58,950 --> 00:04:01,170 ‫Please use the help command for instructions. 55 00:04:01,900 --> 00:04:04,180 ‫And they will break out of the case. 56 00:04:04,780 --> 00:04:09,580 ‫So at this point, we can get rid of this bright line here that Ricky will be fine. 57 00:04:09,880 --> 00:04:11,830 ‫And let's now run the application. 58 00:04:11,830 --> 00:04:17,650 ‫And of course, in order to make it run, make sure that you first close your command prompt. 59 00:04:17,650 --> 00:04:18,890 ‫Otherwise it won't work. 60 00:04:18,910 --> 00:04:20,260 ‫So now it will be built. 61 00:04:20,290 --> 00:04:23,060 ‫Now we can go ahead and pass arguments to it. 62 00:04:23,110 --> 00:04:29,490 ‫So open the command prompt using CMD, so Windows Key and then CMD. 63 00:04:29,500 --> 00:04:36,880 ‫And then you can drag your applications a file which you can of course find via solution explorer. 64 00:04:36,880 --> 00:04:42,430 ‫Right click and here open folder and here go to bin debug. 65 00:04:42,460 --> 00:04:46,900 ‫Dot net five in my case and here you will find the main args demo. 66 00:04:46,990 --> 00:04:50,500 ‫So now you can drag this into your command prompt. 67 00:04:50,500 --> 00:04:57,860 ‫So drag it in here and you can then enter for example, sub and five six. 68 00:04:58,180 --> 00:05:02,770 ‫So it will say the sub of five and six is minus one. 69 00:05:03,550 --> 00:05:04,810 ‫So let's do it again. 70 00:05:04,810 --> 00:05:06,780 ‫Let's try it with a different entry. 71 00:05:06,790 --> 00:05:14,650 ‫Let's use add 15 and 13 and you can see the sum of 15 and 13 is 28. 72 00:05:16,000 --> 00:05:21,430 ‫And now let's enter something else, something stupid like so you can see invalid arguments. 73 00:05:21,430 --> 00:05:23,950 ‫Please use help commands for instructions. 74 00:05:24,280 --> 00:05:25,210 ‫Let's try that. 75 00:05:26,200 --> 00:05:26,740 ‫Let's enter. 76 00:05:26,740 --> 00:05:27,220 ‫Help. 77 00:05:29,130 --> 00:05:32,430 ‫And we can see use one of the following commands followed by two numbers. 78 00:05:32,430 --> 00:05:36,000 ‫Add to add two numbers sub to subtract two numbers. 79 00:05:36,640 --> 00:05:38,460 ‫Okay, so let's try it again. 80 00:05:38,790 --> 00:05:46,260 ‫And here let's use any random keyword and add two numbers and you can see that this will not work because 81 00:05:46,350 --> 00:05:52,650 ‫the arguments weren't correct because it doesn't expect q w e it expects either add or sub. 82 00:05:52,680 --> 00:05:53,310 ‫So. 83 00:05:54,110 --> 00:05:55,270 ‫That's basically it. 84 00:05:55,280 --> 00:06:01,700 ‫That's how we have actually built our first command line program using arguments from outside of the 85 00:06:01,700 --> 00:06:02,390 ‫application. 86 00:06:02,390 --> 00:06:05,570 ‫So user input directly in the command prompt. 87 00:06:05,570 --> 00:06:12,170 ‫And now you could of course extend your application to do whatever you want with it, and it would do 88 00:06:12,170 --> 00:06:13,280 ‫wonders for you. 89 00:06:13,970 --> 00:06:14,450 ‫All right. 90 00:06:14,450 --> 00:06:15,740 ‫So that's it for this video. 91 00:06:15,740 --> 00:06:16,670 ‫I hope you enjoyed it. 92 00:06:16,670 --> 00:06:24,080 ‫And you're now able to create your own little applications using the command prompt and actually using 93 00:06:24,080 --> 00:06:28,130 ‫user input and know how to use the ARX. 94 00:06:28,130 --> 00:06:34,760 ‫So how to use all of those beautiful main args that you have by default when starting the application. 95 00:06:36,050 --> 00:06:38,630 ‫So see you in the next video.