‫Earlier we discussed how to use the command line arguments.
‫And this part you will learn how to handle user input and make a program that does basic operations.
‫We will also see how to display a basic manual to the user explaining how our program works and what
‫are its requirements.
‫This is necessary since our application doesn't use console line to ask the user for input.
‫Instead, all the data that our app needs will be provided at the start using the command line arguments.
‫So what we can do is we can say something like provide arguments next time, pass help if you want to
‫get more details, for example.
‫Okay.
‫So this would be then the statement that the user sees once he starts the application.
‫And now let's actually check if the user entered help.
‫So if he entered help, we are going to give him some help.
‫We're going to say instructions.
‫Use one of the following commands followed by two numbers.
‫Use add to add two numbers.
‫Sub two subtract two numbers.
‫Our application will then pause and return.
‫So now let's actually handle the user input because rule number one, never trust the user because the
‫user will make mistakes.
‫And it's our job to make our application handle all of these possible input mistakes that the user might
‫provide.
‫So first, our application needs three arguments to do its job so it's safe to quit the application
‫if either more or less than three arguments were provided, since we need exactly to so we could do
‫something like this here.
‫If our length is not equal three then write invalid arguments.
‫Please use the help command for instructions and read the key.
‫So now, since we made sure that we won't go any further in our app unless we have three arguments,
‫let's check if the format of arguments is correct, since our arguments will be in the format of a command
‫followed by two numbers.
‫We want to check if the two numbers are in a correct format and we do that by using the float.
‫Try parse functionality so which takes a string that it will try to pass into float and a float variable
‫that it will use to save the parsed data if the parsing operation was successful.
‫Therefore, we're going to add this code here saying this number one parsed and this number two parsed.
‫So basically we get those values right and we can now go ahead and now we can check if the user passed
‫or entered something that is actually going to be a floating number or not.
‫So now we can check if those two are true or not.
‫So if they are not true.
‫So both of them or either of them.
‫So if either of them is false, then we can just say invalid arguments.
‫Please use the help command for instructions and then read key and quit the application.
‫So now at this point, we can go ahead and actually calculate the result using a float where we are
‫going to store the results and then we can use a switch case statement like so where we take arcs at
‫position zero as the switch statement.
‫So we're comparing is it add.
‫So if the user entered add, then we're going to use NUM one, which was this outflow number one.
‫And number two.
‫So we're going to add them together, store the result and result and write it onto the console.
‫And at this point, break out of the switch case.
‫And then if it was set so the user entered up as the first entry at ARX zero, then it will subtract
‫NUM two from num one.
‫And then it will display the results here.
‫And finally, in the default case, if none of the two options add or sub were given, then it will
‫say invalid arguments.
‫Please use the help command for instructions.
‫And they will break out of the case.
‫So at this point, we can get rid of this bright line here that Ricky will be fine.
‫And let's now run the application.
‫And of course, in order to make it run, make sure that you first close your command prompt.
‫Otherwise it won't work.
‫So now it will be built.
‫Now we can go ahead and pass arguments to it.
‫So open the command prompt using CMD, so Windows Key and then CMD.
‫And then you can drag your applications a file which you can of course find via solution explorer.
‫Right click and here open folder and here go to bin debug.
‫Dot net five in my case and here you will find the main args demo.
‫So now you can drag this into your command prompt.
‫So drag it in here and you can then enter for example, sub and five six.
‫So it will say the sub of five and six is minus one.
‫So let's do it again.
‫Let's try it with a different entry.
‫Let's use add 15 and 13 and you can see the sum of 15 and 13 is 28.
‫And now let's enter something else, something stupid like so you can see invalid arguments.
‫Please use help commands for instructions.
‫Let's try that.
‫Let's enter.
‫Help.
‫And we can see use one of the following commands followed by two numbers.
‫Add to add two numbers sub to subtract two numbers.
‫Okay, so let's try it again.
‫And here let's use any random keyword and add two numbers and you can see that this will not work because
‫the arguments weren't correct because it doesn't expect q w e it expects either add or sub.
‫So.
‫That's basically it.
‫That's how we have actually built our first command line program using arguments from outside of the
‫application.
‫So user input directly in the command prompt.
‫And now you could of course extend your application to do whatever you want with it, and it would do
‫wonders for you.
‫All right.
‫So that's it for this video.
‫I hope you enjoyed it.
‫And you're now able to create your own little applications using the command prompt and actually using
‫user input and know how to use the ARX.
‫So how to use all of those beautiful main args that you have by default when starting the application.
‫So see you in the next video.