Command Line Parsing with System.CommandLine
Read full transcript 23 segments
-
Last week we looked at parsing command Last week we looked at parsing command line arguments in dot net manually. This line arguments in dot net manually. This line arguments in dot net manually. This week we're going to look at week we're going to look at week we're going to look at system.commandline, system.commandline, system.commandline, which is a new get package from which is a new get package from which is a new get package from Microsoft, which will process our Microsoft, which will process our Microsoft, which will process our arguments for us. Now, this is a more arguments for us. Now, this is a more arguments for us. Now, this is a more complicated process, but the end result complicated process, but the end result complicated process, but the end result is also a more robust solution. is also a more robust solution. is also a more robust solution. Which one you choose depends on your Which one you choose depends on your Which one you choose depends on your size of app. Now, if you don't know me, size of app. Now, if you don't know me, size of app. Now, if you don't know me, my name is Tim Corey and I help people my name is Tim Corey and I help people my name is Tim Corey and I help people become better developers, especially become better developers, especially become better developers, especially around C# and .NET topics. I have over a around C# and .NET topics. I have over a around C# and .NET topics. I have over a thousand videos here on this channel to thousand videos here on this channel to thousand videos here on this channel to help you on your journey. And for those help you on your journey. And for those help you on your journey. And for those of you looking for specific topics, I of you looking for specific topics, I of you looking for specific topics, I offer dozens of training courses on offer dozens of training courses on offer dozens of training courses on devforge.com. devforge.com. devforge.com. There you'll find master courses on There you'll find master courses on There you'll find master courses on Unity game development, C# and web Unity game development, C# and web Unity game development, C# and web development, along with individual development, along with individual development, along with individual courses covering Git, Azure, SQL, and courses covering Git, Azure, SQL, and courses covering Git, Azure, SQL, and much more. The proceeds of those course much more. The proceeds of those course much more. The proceeds of those course sales are what funds this free content. sales are what funds this free content. sales are what funds this free content. You'll find even more free content on You'll find even more free content on You'll find even more free content on devforge.com, so definitely check it devforge.com, so definitely check it devforge.com, so definitely check it out. All right, let's jump over to out. All right, let's jump over to out. All right, let's jump over to Visual Studio. Here I have a very simple Visual Studio. Here I have a very simple Visual Studio. Here I have a very simple command line command line command line uh application. It just has a console uh application. It just has a console uh application. It just has a console right line with nothing in it. And what right line with nothing in it. And what right line with nothing in it. And what we're going to do is we're going to we're going to do is we're going to we're going to do is we're going to elevate this to use the elevate this to use the elevate this to use the system.commandline system.commandline system.commandline NuGet package.
-
NuGet package. NuGet package. So, let's start there. Let's right click So, let's start there. Let's right click So, let's start there. Let's right click on our dependencies, manage NuGet on our dependencies, manage NuGet on our dependencies, manage NuGet package, and we're going to go to browse package, and we're going to go to browse package, and we're going to go to browse and say system.commandline. And this is a a package from Microsoft And this is a a package from Microsoft that currently is in 2.0.12 that currently is in 2.0.12 that currently is in 2.0.12 version and will help us with parsing version and will help us with parsing version and will help us with parsing our packages. So, it does have some our packages. So, it does have some our packages. So, it does have some basics of how to use this, but that's basics of how to use this, but that's basics of how to use this, but that's what this video is for as well. So, what this video is for as well. So, what this video is for as well. So, let's install this. let's install this. let's install this. And there's a lot to this package. We're And there's a lot to this package. We're And there's a lot to this package. We're not going to cover everything. This is not going to cover everything. This is not going to cover everything. This is not a not a not a you know, deep dive in this package, but you know, deep dive in this package, but you know, deep dive in this package, but I do think that um this will give you I do think that um this will give you I do think that um this will give you the the the 80% you'll probably want to the the the 80% you'll probably want to the the the 80% you'll probably want to use, and then you can look at tweaking use, and then you can look at tweaking use, and then you can look at tweaking it for your specific situation. So, it for your specific situation. So, it for your specific situation. So, let's add our using directive, using let's add our using directive, using let's add our using directive, using system.commandline. system.commandline. system.commandline. Just so we have it. We're going to need Just so we have it. We're going to need Just so we have it. We're going to need that in a little bit. And what we're that in a little bit. And what we're that in a little bit. And what we're going to do is we're going to define a going to do is we're going to define a going to do is we're going to define a command line option. command line option. command line option. It's called the option.
-
It's called the option. It's called the option. And we're going to say a string option. And we're going to say a string option. And we're going to say a string option. Let's keep it really simple for now. Let's keep it really simple for now. Let's keep it really simple for now. We'll call this the name option. We'll call this the name option. We'll call this the name option. I'm going to say equals new, and here's I'm going to say equals new, and here's I'm going to say equals new, and here's the command looks like, dash dash name. the command looks like, dash dash name. the command looks like, dash dash name. That's what we're going to look for when That's what we're going to look for when That's what we're going to look for when we are looking for this option. we are looking for this option. we are looking for this option. So, what we're going to say in here is So, what we're going to say in here is So, what we're going to say in here is description equals the name of the user. description equals the name of the user. description equals the name of the user. And this description is important in And this description is important in And this description is important in just a minute. But, just a minute. But, just a minute. But, what we're looking for is when we call what we're looking for is when we call what we're looking for is when we call this, and by the way, we're going to this, and by the way, we're going to this, and by the way, we're going to pull up this um this PowerShell window pull up this um this PowerShell window pull up this um this PowerShell window here. We're going to actually call this here. We're going to actually call this here. We're going to actually call this application, so you can say uh application, so you can say uh application, so you can say uh parse.parseargs.exe, parse.parseargs.exe, parse.parseargs.exe, and we're going to say name, and then and we're going to say name, and then and we're going to say name, and then Tim Corey, something like that. That's Tim Corey, something like that. That's Tim Corey, something like that. That's what it's going to look for when you see what it's going to look for when you see what it's going to look for when you see this this this uh name option where it says name, okay? uh name option where it says name, okay? uh name option where it says name, okay? It's going to say look for name, say It's going to say look for name, say It's going to say look for name, say okay, what's the value of that? Now, okay, what's the value of that? Now, okay, what's the value of that? Now, we're not there yet, um but that's what we're not there yet, um but that's what we're not there yet, um but that's what we're setting up first.
-
we're setting up first. we're setting up first. Okay. So, now we defined our command Okay. So, now we defined our command Okay. So, now we defined our command line arguments. We only have one, and line arguments. We only have one, and line arguments. We only have one, and that's name. that's name. that's name. Then we're going to say root command. Then we're going to say root command. Then we're going to say root command. Let's call it root command. Equals new. And this is the name you Equals new. And this is the name you give your app. Um let's go with sample give your app. Um let's go with sample give your app. Um let's go with sample app for CLI app for CLI app for CLI arg processing. arg processing. arg processing. Something like that. Something like that. Something like that. Now, if you notice, I gave my option a Now, if you notice, I gave my option a Now, if you notice, I gave my option a description and now my command has a description and now my command has a description and now my command has a description as well. And the reason why description as well. And the reason why description as well. And the reason why is because this tool not only parses is because this tool not only parses is because this tool not only parses your arguments, but it will also give your arguments, but it will also give your arguments, but it will also give the user a help menu that they can look the user a help menu that they can look the user a help menu that they can look at what the options are. And so it's at what the options are. And so it's at what the options are. And so it's going to say, "Hey, you can you have the going to say, "Hey, you can you have the going to say, "Hey, you can you have the possibility of passing in {dash} {dash} possibility of passing in {dash} {dash} possibility of passing in {dash} {dash} name with a string value and that is name with a string value and that is name with a string value and that is described as the name of the user."
-
described as the name of the user." described as the name of the user." And the overall app is sample app for And the overall app is sample app for And the overall app is sample app for CLI arc processing. So, we'll see these CLI arc processing. So, we'll see these CLI arc processing. So, we'll see these pop up in a little bit, but uh I just pop up in a little bit, but uh I just pop up in a little bit, but uh I just want to point that out. want to point that out. want to point that out. Now, we have our root command, but we Now, we have our root command, but we Now, we have our root command, but we still have to add our option to the still have to add our option to the still have to add our option to the command. So, root command {dot} options command. So, root command {dot} options command. So, root command {dot} options {dot} add {dot} add {dot} add name option. So, this option right here is just So, this option right here is just defining that we want to have this defining that we want to have this defining that we want to have this option. Now we've actually added it to option. Now we've actually added it to option. Now we've actually added it to our root command. our root command. our root command. Now, what we're going to do is we're not Now, what we're going to do is we're not Now, what we're going to do is we're not going to do every possible permutation going to do every possible permutation going to do every possible permutation of how to use System.CommandLine. I'm of how to use System.CommandLine. I'm of how to use System.CommandLine. I'm going to give you kind of my opinionated going to give you kind of my opinionated going to give you kind of my opinionated way that you're almost certainly going way that you're almost certainly going way that you're almost certainly going to use this. to use this. to use this. So, what we're going to do is we're So, what we're going to do is we're So, what we're going to do is we're going to say root command {dot} set going to say root command {dot} set going to say root command {dot} set action. action. action. And we'll say parse result And we'll say parse result And we'll say parse result and then arrow like so. So, what this is and then arrow like so. So, what this is and then arrow like so. So, what this is going to be is this is the code in here going to be is this is the code in here going to be is this is the code in here that will run when that will run when that will run when any parameters are passed in or not.
-
any parameters are passed in or not. any parameters are passed in or not. Because you don't have to pass in a Because you don't have to pass in a Because you don't have to pass in a parameter. parameter. parameter. So, we're going to run this code and So, we're going to run this code and So, we're going to run this code and then after that, we're going to say then after that, we're going to say then after that, we're going to say parse results. parse results. parse results. And we'll say uh um result equals root And we'll say uh um result equals root And we'll say uh um result equals root command.parse command.parse command.parse args. args. args. So, this is where the actual work gets So, this is where the actual work gets So, this is where the actual work gets done of parsing those those commands done of parsing those those commands done of parsing those those commands that were passed in, whether it's none that were passed in, whether it's none that were passed in, whether it's none or one flag or 30 flags. or one flag or 30 flags. or one flag or 30 flags. And then we're going to return And then we're going to return And then we're going to return parse res- result, actually sorry, parse res- result, actually sorry, parse res- result, actually sorry, result Okay. So, Okay. So, if you don't know with command uh or if you don't know with command uh or if you don't know with command uh or console applications, if you do a console applications, if you do a console applications, if you do a return, you can return a zero meaning return, you can return a zero meaning return, you can return a zero meaning things were successful or any other things were successful or any other things were successful or any other number. And traditionally, it's not number. And traditionally, it's not number. And traditionally, it's not required, but traditionally, a one required, but traditionally, a one required, but traditionally, a one indicates there's an unspecified error indicates there's an unspecified error indicates there's an unspecified error or just a generic error. So, one means or just a generic error. So, one means or just a generic error. So, one means something went wrong, zero means something went wrong, zero means something went wrong, zero means success. And quite frankly, zero means success. And quite frankly, zero means success. And quite frankly, zero means success, anything else means an error.
-
success, anything else means an error. success, anything else means an error. So, this invoke is going to return So, this invoke is going to return So, this invoke is going to return integer, either zero, things were integer, either zero, things were integer, either zero, things were successful, or one. This value can be successful, or one. This value can be successful, or one. This value can be used as a process exit code. So, it's used as a process exit code. So, it's used as a process exit code. So, it's going to return one if there was a going to return one if there was a going to return one if there was a problem. It's not going to return a problem. It's not going to return a problem. It's not going to return a count of the problems, just a one. count of the problems, just a one. count of the problems, just a one. Okay. So, you're already saying, "Wait, Okay. So, you're already saying, "Wait, Okay. So, you're already saying, "Wait, Tim, the the application's over?" Yes, Tim, the the application's over?" Yes, Tim, the the application's over?" Yes, right here the application's done. right here the application's done. right here the application's done. Now, Now, Now, in here is where we're going to do all in here is where we're going to do all in here is where we're going to do all of our code. of our code. of our code. So, this is where we're going to do what So, this is where we're going to do what So, this is where we're going to do what would have been in just program.cs, that would have been in just program.cs, that would have been in just program.cs, that goes in here. goes in here. goes in here. So, So, So, the process here, I just want to give the process here, I just want to give the process here, I just want to give you a a good overview before we actually you a a good overview before we actually you a a good overview before we actually put code in here. The process is we put code in here. The process is we put code in here. The process is we define we actually do a command line define we actually do a command line define we actually do a command line first, and that's going to put a blank first, and that's going to put a blank first, and that's going to put a blank line after our call. It just makes it um line after our call. It just makes it um line after our call. It just makes it um our console clear. our console clear. our console clear. But then we're going to But then we're going to But then we're going to we're going to say one option it's a we're going to say one option it's a we're going to say one option it's a name option. Then we're going to create name option. Then we're going to create name option. Then we're going to create our root command which is going to do our root command which is going to do our root command which is going to do the processing of our our args. They the processing of our our args. They the processing of our our args. They pass in this one option to say, "Hey, pass in this one option to say, "Hey, pass in this one option to say, "Hey, we're going to be looking for name if we're going to be looking for name if we're going to be looking for name if it's there."
-
it's there." it's there." Then we Then we Then we execute this action execute this action execute this action when the invoke gets called. So, when when the invoke gets called. So, when when the invoke gets called. So, when the invoke gets called, it's going to the invoke gets called, it's going to the invoke gets called, it's going to come up and say, "Hey, do we have an come up and say, "Hey, do we have an come up and say, "Hey, do we have an action?" Yes, then go ahead and run action?" Yes, then go ahead and run action?" Yes, then go ahead and run that. that. that. So, it's going to run this command. This So, it's going to run this command. This So, it's going to run this command. This invoke is also what wires up the the invoke is also what wires up the the invoke is also what wires up the the dash dash help and the dash dash version dash dash help and the dash dash version dash dash help and the dash dash version and other options that come by default and other options that come by default and other options that come by default with your application now. with your application now. with your application now. So, So, So, then we have the parse. This parse then we have the parse. This parse then we have the parse. This parse happens happens happens um before we invoke it. Because what um before we invoke it. Because what um before we invoke it. Because what it's doing is saying, "Okay, actually it's doing is saying, "Okay, actually it's doing is saying, "Okay, actually process the args, get the result, and process the args, get the result, and process the args, get the result, and then take those results, invoke them." then take those results, invoke them." then take those results, invoke them." And this invoke is going to call the set And this invoke is going to call the set And this invoke is going to call the set action action action and pass in the results. So, let's and pass in the results. So, let's and pass in the results. So, let's create a method down here. Let's call it create a method down here. Let's call it create a method down here. Let's call it um this is a local method. We're going um this is a local method. We're going um this is a local method. We're going to say void to say void to say void uh print name. And we're going to pass in let's call it And we're going to pass in let's call it user's name, not username but user's user's name, not username but user's user's name, not username but user's name, just to make sure that you name, just to make sure that you name, just to make sure that you understand that this is not pulling from understand that this is not pulling from understand that this is not pulling from this or any like any connection to that.
-
this or any like any connection to that. this or any like any connection to that. And we're going to say console right And we're going to say console right And we're going to say console right line, string interpolation, the captured line, string interpolation, the captured line, string interpolation, the captured uh name of the user is uh name of the user is uh name of the user is user's name. user's name. user's name. Just so we can print out that yes, we Just so we can print out that yes, we Just so we can print out that yes, we know what the user's name is. And then know what the user's name is. And then know what the user's name is. And then we're also going to create one more we're also going to create one more we're also going to create one more method here, and let's call this method method here, and let's call this method method here, and let's call this method uh void the rest uh void the rest uh void the rest of the application. of the application. of the application. Now, Now, Now, that's not a great name except for the that's not a great name except for the that's not a great name except for the fact that in this case it makes total fact that in this case it makes total fact that in this case it makes total sense. sense. sense. This is essentially standing in for This is essentially standing in for This is essentially standing in for whatever whatever whatever code you want to run in this console code you want to run in this console code you want to run in this console app. app. app. So, this is the rest of the code. Now, So, this is the rest of the code. Now, So, this is the rest of the code. Now, first we're going to call any of these first we're going to call any of these first we're going to call any of these because for example, if you pass in a because for example, if you pass in a because for example, if you pass in a name, maybe you want to put that in a name, maybe you want to put that in a name, maybe you want to put that in a variable, maybe you want to do something variable, maybe you want to do something variable, maybe you want to do something with that name. Whatever the case may with that name. Whatever the case may with that name. Whatever the case may be, you can do whatever work that is be, you can do whatever work that is be, you can do whatever work that is right here in the print name. But then right here in the print name. But then right here in the print name. But then the rest of the application will fire the rest of the application will fire the rest of the application will fire either way at the end. So, if you put either way at the end. So, if you put either way at the end. So, if you put the name in a variable, the rest of the the name in a variable, the rest of the the name in a variable, the rest of the application could potentially access application could potentially access application could potentially access that variable and say, "Oh, you got a that variable and say, "Oh, you got a that variable and say, "Oh, you got a name here? Well, then we can do this name here? Well, then we can do this name here? Well, then we can do this with it."
-
with it." with it." So, I'm not teaching you how to build So, I'm not teaching you how to build So, I'm not teaching you how to build your whole application, just how to your whole application, just how to your whole application, just how to process the arguments. Okay, so let's do process the arguments. Okay, so let's do process the arguments. Okay, so let's do a console right line that says, "The a console right line that says, "The a console right line that says, "The rest um rest um rest um this this this is a stand-in for the is a stand-in for the is a stand-in for the rest of rest of rest of your code." your code." your code." Now, by the way, if you want this code, Now, by the way, if you want this code, Now, by the way, if you want this code, is there's a link in the description to is there's a link in the description to is there's a link in the description to get that. Okay, so the rest of the get that. Okay, so the rest of the get that. Okay, so the rest of the application, we're going to put this up application, we're going to put this up application, we're going to put this up here here here like so. like so. like so. So, set action, So, set action, So, set action, we're going to run this action when we we're going to run this action when we we're going to run this action when we do invoke. And we're going to have parse do invoke. And we're going to have parse do invoke. And we're going to have parse results, but the last thing we're going results, but the last thing we're going results, but the last thing we're going to do to do to do is the rest of the application. By the is the rest of the application. By the is the rest of the application. By the way, I'm going to put a console right way, I'm going to put a console right way, I'm going to put a console right line at the end as well, a blank one. line at the end as well, a blank one. line at the end as well, a blank one. And that way what it does is it just And that way what it does is it just And that way what it does is it just makes sure we separate um makes sure we separate um makes sure we separate um our our our our our our printouts from whatever we typed on the printouts from whatever we typed on the printouts from whatever we typed on the command line. command line. command line. So, you'll see in just a minute. But, So, you'll see in just a minute. But, So, you'll see in just a minute. But, I want to up here I want to up here I want to up here call this print name if and only if call this print name if and only if call this print name if and only if you pass in a name.
-
you pass in a name. you pass in a name. So, how do you do that? Well, what we're So, how do you do that? Well, what we're So, how do you do that? Well, what we're going to do is we're going to say, let's going to do is we're going to say, let's going to do is we're going to say, let's do a new line here. We're going to say do a new line here. We're going to say do a new line here. We're going to say if if if parse result, which is this variable parse result, which is this variable parse result, which is this variable being passed in, this is the results of being passed in, this is the results of being passed in, this is the results of our of our invocation or of our parsing our of our invocation or of our parsing our of our invocation or of our parsing that they're invoked and passes in. that they're invoked and passes in. that they're invoked and passes in. Uh we're going to say parse result {dot} Uh we're going to say parse result {dot} Uh we're going to say parse result {dot} get value get value get value of the name option. of the name option. of the name option. So this is, remember the option, name So this is, remember the option, name So this is, remember the option, name option? We're going to get the value of option? We're going to get the value of option? We're going to get the value of this. this. this. And we're going to say is string name And we're going to say is string name And we're going to say is string name result. Okay. So what is this doing? Because Okay. So what is this doing? Because this is actually doing a couple things this is actually doing a couple things this is actually doing a couple things right in line. So it's getting the value right in line. So it's getting the value right in line. So it's getting the value of the name option of the name option of the name option and saying is string name result, not and saying is string name result, not and saying is string name result, not nullable string string. So what this is nullable string string. So what this is nullable string string. So what this is doing is saying, "Hey, let's make sure doing is saying, "Hey, let's make sure doing is saying, "Hey, let's make sure that that that um we're passing in a name result only um we're passing in a name result only um we're passing in a name result only if this is not null."
-
if this is not null." if this is not null." Notice we're not doing a nullable string Notice we're not doing a nullable string Notice we're not doing a nullable string here, just a string. So now we can say here, just a string. So now we can say here, just a string. So now we can say print name name result. print name name result. print name name result. So if there was a name passed in and So if there was a name passed in and So if there was a name passed in and it's not null, then go ahead and print it's not null, then go ahead and print it's not null, then go ahead and print that out. And that's going to kind of that out. And that's going to kind of that out. And that's going to kind of call this method which will print it out call this method which will print it out call this method which will print it out to the console. to the console. to the console. So now we have all of our code in place. So now we have all of our code in place. So now we have all of our code in place. Now I'm going to pull the console up Now I'm going to pull the console up Now I'm going to pull the console up here because it's just the easiest way here because it's just the easiest way here because it's just the easiest way of doing it. And we have parse args name of doing it. And we have parse args name of doing it. And we have parse args name is Tim Corey. Let's hit enter. is Tim Corey. Let's hit enter. is Tim Corey. Let's hit enter. And nothing happens. That's because I And nothing happens. That's because I And nothing happens. That's because I did not build. did not build. did not build. Got to build your stuff first. There we Got to build your stuff first. There we Got to build your stuff first. There we go. go. go. Now let's try it again. Now let's try it again. Now let's try it again. There we go. The captured name of the There we go. The captured name of the There we go. The captured name of the user is Tim Corey. This is a stand-in user is Tim Corey. This is a stand-in user is Tim Corey. This is a stand-in for the rest of your code. That's why I for the rest of your code. That's why I for the rest of your code. That's why I put the blank line before and after just put the blank line before and after just put the blank line before and after just to kind of give it some separation. So to kind of give it some separation. So to kind of give it some separation. So let's clear the screen and let's call let's clear the screen and let's call let's clear the screen and let's call this parse again without any parameters. this parse again without any parameters. this parse again without any parameters. This is a stand-in for the rest of your This is a stand-in for the rest of your This is a stand-in for the rest of your code. So, that runs no matter what, but code. So, that runs no matter what, but code. So, that runs no matter what, but if you pass in a name, it now if you pass in a name, it now if you pass in a name, it now calls that method, which does something calls that method, which does something calls that method, which does something with that name. In this case, it says, with that name. In this case, it says, with that name. In this case, it says, "Hey, um this is the name of your user."
-
"Hey, um this is the name of your user." "Hey, um this is the name of your user." Now, I could do something else, which is Now, I could do something else, which is Now, I could do something else, which is say {dash} {dash} help. say {dash} {dash} help. say {dash} {dash} help. And it says description, the sample app And it says description, the sample app And it says description, the sample app for CLI arc processing. And then it says for CLI arc processing. And then it says for CLI arc processing. And then it says usage, parse arcs. And the options are usage, parse arcs. And the options are usage, parse arcs. And the options are name and pass in the name, {question name and pass in the name, {question name and pass in the name, {question mark} {dash} H or {dash} {dash} help to mark} {dash} H or {dash} {dash} help to mark} {dash} H or {dash} {dash} help to show the help information, and {dash} show the help information, and {dash} show the help information, and {dash} {dash} version to show the version {dash} version to show the version {dash} version to show the version information. By the way, if you want to information. By the way, if you want to information. By the way, if you want to do that, do that, do that, um let's do that. >> [snorts] >> [snorts] >> Version is 1.0.0. >> Version is 1.0.0. >> Version is 1.0.0. So, So, So, wait a minute, how does it know that? wait a minute, how does it know that? wait a minute, how does it know that? Well, it's That's one of the benefits of Well, it's That's one of the benefits of Well, it's That's one of the benefits of how this works. It is it's pulling from how this works. It is it's pulling from how this works. It is it's pulling from our actual application version. So, if our actual application version. So, if our actual application version. So, if you go here and look for your version you go here and look for your version you go here and look for your version number, you're going to find that our number, you're going to find that our number, you're going to find that our version number um version number um version number um where is it? I haven't been It's been a where is it? I haven't been It's been a where is it? I haven't been It's been a long time. Um long time. Um long time. Um your version of your application your version of your application your version of your application is in here. is in here. is in here. Actually, I think that at the top here Actually, I think that at the top here Actually, I think that at the top here we've got um we've got um we've got um assembly name and assembly name and assembly name and yeah, it's in here somewhere.
-
yeah, it's in here somewhere. yeah, it's in here somewhere. Um Let's go to package um general. I think Let's go to package um general. I think There you go. Version number 1.0.0. There you go. Version number 1.0.0. There you go. Version number 1.0.0. Okay. So, uh and you can modify that if Okay. So, uh and you can modify that if Okay. So, uh and you can modify that if you want. But, you want. But, you want. But, that's picked up the version, picked up that's picked up the version, picked up that's picked up the version, picked up the help information, and it gave us the help information, and it gave us the help information, and it gave us this information about parameters that this information about parameters that this information about parameters that are possible to pass in. So, that's one are possible to pass in. So, that's one are possible to pass in. So, that's one thing. The other thing we can do here, thing. The other thing we can do here, thing. The other thing we can do here, by the way, is if we hit comma and say by the way, is if we hit comma and say by the way, is if we hit comma and say uh default value factory, uh default value factory, uh default value factory, and we're going to do a discard and we're going to do a discard and we're going to do a discard character, and then arrow, character, and then arrow, character, and then arrow, and we're going to say test user. and we're going to say test user. and we're going to say test user. So, we've now given a default value to So, we've now given a default value to So, we've now given a default value to the name option. So, if you don't pass the name option. So, if you don't pass the name option. So, if you don't pass in name at all, it's going to give you a in name at all, it's going to give you a in name at all, it's going to give you a default name. So, let's default name. So, let's default name. So, let's uh let's build this again uh let's build this again uh let's build this again and then and then and then we're going to pass in we're going to pass in we're going to pass in um um um just command or parse args.exe, no no just command or parse args.exe, no no just command or parse args.exe, no no parameters. parameters. parameters. The caption name of the user is test The caption name of the user is test The caption name of the user is test user. So, if you want to, you could pass user. So, if you want to, you could pass user. So, if you want to, you could pass in a default value um if it makes sense.
-
in a default value um if it makes sense. in a default value um if it makes sense. But, if it doesn't make sense, then then But, if it doesn't make sense, then then But, if it doesn't make sense, then then don't include anything. But, that's how don't include anything. But, that's how don't include anything. But, that's how you can create a parameter. Now, why you can create a parameter. Now, why you can create a parameter. Now, why have one parameter we can have two have one parameter we can have two have one parameter we can have two parameters? parameters? parameters? So, option int So, option int So, option int and we'll call this the count option. and we'll call this the count option. and we'll call this the count option. And equals uh new and we're going to say And equals uh new and we're going to say And equals uh new and we're going to say dash dash count. Now, notice that I'm dash dash count. Now, notice that I'm dash dash count. Now, notice that I'm passing in this as an integer. And in passing in this as an integer. And in passing in this as an integer. And in fact, I want to make this a nullable fact, I want to make this a nullable fact, I want to make this a nullable integer. So, So, I'm going to say uh description I'm going to say uh description I'm going to say uh description equals the number we are starting at. equals the number we are starting at. equals the number we are starting at. Okay, so this is just Okay, so this is just Okay, so this is just uh again, a sample application. Now, I'm uh again, a sample application. Now, I'm uh again, a sample application. Now, I'm not done yet. I've created the option, not done yet. I've created the option, not done yet. I've created the option, but I have not added to my root command. but I have not added to my root command. but I have not added to my root command. So, let's duplicate this and say count So, let's duplicate this and say count So, let's duplicate this and say count option. So, now you've added this to our option. So, now you've added this to our option. So, now you've added this to our root command. Now, we're not parsing it.
-
root command. Now, we're not parsing it. root command. Now, we're not parsing it. We need to parse it. So, let's create a We need to parse it. So, let's create a We need to parse it. So, let's create a uh uh uh uh a method here uh void uh a method here uh void uh a method here uh void um count count numbers. I don't know. Sure. numbers. I don't know. Sure. numbers. I don't know. Sure. We're going to say We're going to say We're going to say um um um start value. And we're going to say And we're going to say um um um let's keep this, you know, real simple let's keep this, you know, real simple let's keep this, you know, real simple for and we'll say I equals um start for and we'll say I equals um start for and we'll say I equals um start value. I is less than start value plus three. I is less than start value plus three. Okay? Something simple. We're just going Okay? Something simple. We're just going Okay? Something simple. We're just going to say console.writeline to say console.writeline to say console.writeline the the the string interpolation first. The number string interpolation first. The number string interpolation first. The number is is is uh I. uh I. uh I. Okay? So, in theory, you pass in a a Okay? So, in theory, you pass in a a Okay? So, in theory, you pass in a a number, it's going to start there and number, it's going to start there and number, it's going to start there and count up. Okay? Just for a couple count up. Okay? Just for a couple count up. Okay? Just for a couple numbers. numbers. numbers. Okay. So, now let's also Okay. So, now let's also Okay. So, now let's also do capture this. So, this is the count do capture this. So, this is the count do capture this. So, this is the count numbers.
-
numbers. numbers. So, up here, we're going to say if So, up here, we're going to say if So, up here, we're going to say if parse result {dot} get value parse result {dot} get value parse result {dot} get value and we're going to say and we're going to say and we're going to say um this is the count option. um this is the count option. um this is the count option. is int is int is int and we're going to say count result. Like so. Like so. And if it is, we're going to we call And if it is, we're going to we call And if it is, we're going to we call this uh count numbers. Count numbers and this is going to be Count numbers and this is going to be the count result. So, this is So, this is Now, notice that we capture the string, Now, notice that we capture the string, Now, notice that we capture the string, which command line or the args are which command line or the args are which command line or the args are string. It's a string array. But, this string. It's a string array. But, this string. It's a string array. But, this is capturing integer, which is is capturing integer, which is is capturing integer, which is different. different. different. So, [snorts] So, [snorts] So, [snorts] let's run this. Let's build this first. let's run this. Let's build this first. let's run this. Let's build this first. Cool. It built. Let's go back over here Cool. It built. Let's go back over here Cool. It built. Let's go back over here and I'm going to pass in and I'm going to pass in and I'm going to pass in uh count of five.
-
uh count of five. uh count of five. Okay? 5 6 7. Okay? 5 6 7. Okay? 5 6 7. So, it captured that just fine. Let's So, it captured that just fine. Let's So, it captured that just fine. Let's run it without the count. run it without the count. run it without the count. And again, it's still capturing test And again, it's still capturing test And again, it's still capturing test user because we're not passing in a user because we're not passing in a user because we're not passing in a name, but now if I pass in a count, it name, but now if I pass in a count, it name, but now if I pass in a count, it calls that method and does something calls that method and does something calls that method and does something with it. In this case, it just counts with it. In this case, it just counts with it. In this case, it just counts three numbers or prints out three three numbers or prints out three three numbers or prints out three numbers on the screen. But, um this numbers on the screen. But, um this numbers on the screen. But, um this allows us to capture parameters that allows us to capture parameters that allows us to capture parameters that aren't just of type string, but of any aren't just of type string, but of any aren't just of type string, but of any strongly typed object strongly typed object strongly typed object within reason. Um you know, we're within reason. Um you know, we're within reason. Um you know, we're talking about normal types, not not talking about normal types, not not talking about normal types, not not necessarily a class. Um although there's necessarily a class. Um although there's necessarily a class. Um although there's tech Please way to do it, but beside the tech Please way to do it, but beside the tech Please way to do it, but beside the point. So, what we're doing is uh we're point. So, what we're doing is uh we're point. So, what we're doing is uh we're doing just basic strings, ints, doing just basic strings, ints, doing just basic strings, ints, booleans, etc. Speaking of which, booleans, etc. Speaking of which, booleans, etc. Speaking of which, boolean um is even easier. Let's not boolean um is even easier. Let's not boolean um is even easier. Let's not close that. Um we can just say bool, and close that. Um we can just say bool, and close that. Um we can just say bool, and then we're not actually going to pass in then we're not actually going to pass in then we're not actually going to pass in a value. a value. a value. So, we're going to say option So, we're going to say option So, we're going to say option uh bool, uh bool, uh bool, and we're going to say and we're going to say and we're going to say uh uh uh is alive option is alive option is alive option equals new, and we're going to say equals new, and we're going to say equals new, and we're going to say um is alive.
-
um is alive. um is alive. Now, this is just a flag. It's not going Now, this is just a flag. It's not going Now, this is just a flag. It's not going to be a true false. We're just passing to be a true false. We're just passing to be a true false. We're just passing in is alive, in is alive, in is alive, and we're going to say and we're going to say and we're going to say um um um mark mark mark if the user is alive. Right there, it fires if the user is Right there, it fires if the user is alive, something like that. alive, something like that. alive, something like that. So, now we can do the same thing where So, now we can do the same thing where So, now we can do the same thing where we can we can do a parse. So, we can we can do a parse. So, we can we can do a parse. So, if if if parse result.get parse result.get parse result.get value value value of of of the is alive option the is alive option the is alive option is bool is bool is bool is alive result, is alive result, is alive result, >> [snorts] >> [snorts] >> [snorts] >> then we're going to say >> then we're going to say >> then we're going to say um um um Let's just do a console.writeline here. Let's just do a console.writeline here. Let's just do a console.writeline here. And we're going to say, "The And we're going to say, "The And we're going to say, "The user is alive: user is alive: user is alive: or have the actual value."
-
>> [snorts] >> [snorts] >> So, >> So, >> So, with that, we can build the project. with that, we can build the project. with that, we can build the project. And we're going to run this again. Let's And we're going to run this again. Let's And we're going to run this again. Let's clear screen first. We're going to run clear screen first. We're going to run clear screen first. We're going to run again with just again with just again with just you know, no arguments. Okay? The user you know, no arguments. Okay? The user you know, no arguments. Okay? The user is alive option one is alive. is alive option one is alive. is alive option one is alive. And that is means that I made a mistake And that is means that I made a mistake And that is means that I made a mistake up here. up here. up here. Um Um Um First of all, is alive result, not is First of all, is alive result, not is First of all, is alive result, not is alive option. alive option. alive option. Um but I think there's one more issue Um but I think there's one more issue Um but I think there's one more issue and that is I forgot to add the option. and that is I forgot to add the option. and that is I forgot to add the option. One thing you have to remember, One thing you have to remember, One thing you have to remember, um um um you want the um is alive op- You want to you want the um is alive op- You want to you want the um is alive op- You want to make sure you add your option to your make sure you add your option to your make sure you add your option to your command, otherwise it doesn't do command, otherwise it doesn't do command, otherwise it doesn't do anything. anything. anything. So, with that, let's go ahead and build So, with that, let's go ahead and build So, with that, let's go ahead and build again. again. again. Let's run our app again. Let's run our app again. Let's run our app again. And this time, the user's alive is And this time, the user's alive is And this time, the user's alive is false. false. false. So, if I were to run this again and say, So, if I were to run this again and say, So, if I were to run this again and say, "Is alive?" "Is alive?" "Is alive?" And it says, "A is alive not a not a And it says, "A is alive not a not a And it says, "A is alive not a not a valid one." That's because you have to valid one." That's because you have to valid one." That's because you have to use case sensitivity. So, let's run this use case sensitivity. So, let's run this use case sensitivity. So, let's run this again and this time say, "Is alive?" And again and this time say, "Is alive?" And again and this time say, "Is alive?" And now, the user is alive. Their is alive now, the user is alive. Their is alive now, the user is alive. Their is alive is true. So, that's how you use a is true. So, that's how you use a is true. So, that's how you use a boolean, where it's just a flag, there's boolean, where it's just a flag, there's boolean, where it's just a flag, there's no actual value you're passing in. So, no actual value you're passing in. So, no actual value you're passing in. So, if you pass in just a flag, that'd be a if you pass in just a flag, that'd be a if you pass in just a flag, that'd be a boolean value. If the flag is being boolean value. If the flag is being boolean value. If the flag is being passed in, the value is true. Otherwise, passed in, the value is true. Otherwise, passed in, the value is true. Otherwise, the value is false.
-
the value is false. the value is false. Okay. So, that's how you parse command Okay. So, that's how you parse command Okay. So, that's how you parse command line arguments with the line arguments with the line arguments with the system.commandline system.commandline system.commandline NuGet package. NuGet package. NuGet package. >> [snorts] >> [snorts] >> [snorts] >> It is a bit more complex. This is why I >> It is a bit more complex. This is why I >> It is a bit more complex. This is why I showed last week how I just parsed the showed last week how I just parsed the showed last week how I just parsed the the string. Because for some apps, the string. Because for some apps, the string. Because for some apps, that's all you need, and this is that's all you need, and this is that's all you need, and this is overkill. But, if you have a larger overkill. But, if you have a larger overkill. But, if you have a larger application, you might want to bring in application, you might want to bring in application, you might want to bring in this level of complexity because of the this level of complexity because of the this level of complexity because of the fact we do have error checking. So, as fact we do have error checking. So, as fact we do have error checking. So, as you saw with that error, so if I were to you saw with that error, so if I were to you saw with that error, so if I were to pass in a lowercase is alive, it says, pass in a lowercase is alive, it says, pass in a lowercase is alive, it says, "Hey, that didn't match. Did you mean is "Hey, that didn't match. Did you mean is "Hey, that didn't match. Did you mean is alive?" Like this, it's actually giving alive?" Like this, it's actually giving alive?" Like this, it's actually giving us a suggestion here because it says us a suggestion here because it says us a suggestion here because it says "unrecognized argument" and then it "unrecognized argument" and then it "unrecognized argument" and then it gives us that help screen as well just gives us that help screen as well just gives us that help screen as well just to highlight for us what the potential to highlight for us what the potential to highlight for us what the potential options are. All of that are Those are options are. All of that are Those are options are. All of that are Those are all really nice things to have when you all really nice things to have when you all really nice things to have when you are building an application that's going are building an application that's going are building an application that's going to interact with users that needs to be to interact with users that needs to be to interact with users that needs to be robust. So, this is a much more robust robust. So, this is a much more robust robust. So, this is a much more robust system, especially you start adding a system, especially you start adding a system, especially you start adding a lot more parameters. So, it's something lot more parameters. So, it's something lot more parameters. So, it's something to think about if you are going to grow to think about if you are going to grow to think about if you are going to grow over time, maybe it's something you over time, maybe it's something you over time, maybe it's something you implement, but if you have a small implement, but if you have a small implement, but if you have a small application, just parsing the strings is application, just parsing the strings is application, just parsing the strings is just fine. So, I want [snorts] to give just fine. So, I want [snorts] to give just fine. So, I want [snorts] to give you both options because being a you both options because being a you both options because being a developer isn't about saying this is the developer isn't about saying this is the developer isn't about saying this is the best, it's about this is the best for best, it's about this is the best for best, it's about this is the best for this situation. Thus, it depends. So, um this situation. Thus, it depends. So, um this situation. Thus, it depends. So, um this right here this right here this right here is
-
is is basically three parts. We've got our basically three parts. We've got our basically three parts. We've got our options here. We have our our setup of options here. We have our our setup of options here. We have our our setup of our command itself including down here our command itself including down here our command itself including down here the the uh running of it and invoking the the uh running of it and invoking the the uh running of it and invoking it, and then finally the action. it, and then finally the action. it, and then finally the action. And that action is to execute once we And that action is to execute once we And that action is to execute once we have invoked our our processing of the have invoked our our processing of the have invoked our our processing of the args. This process the args, and these args. This process the args, and these args. This process the args, and these commands right here check to see if the commands right here check to see if the commands right here check to see if the value is not null, and if it's not null, value is not null, and if it's not null, value is not null, and if it's not null, it puts it the value into this variable it puts it the value into this variable it puts it the value into this variable name, which you can then use for name, which you can then use for name, which you can then use for whatever we want to do with that. Now, whatever we want to do with that. Now, whatever we want to do with that. Now, obviously, we just called some simple obviously, we just called some simple obviously, we just called some simple methods here, but you could put this methods here, but you could put this methods here, but you could put this into a a class name or class property, into a a class name or class property, into a a class name or class property, or you could, you know, execute or you could, you know, execute or you could, you know, execute something else based upon it. Uh the something else based upon it. Uh the something else based upon it. Uh the example they have Microsoft has in their example they have Microsoft has in their example they have Microsoft has in their documentation is um you could say, "Hey, documentation is um you could say, "Hey, documentation is um you could say, "Hey, if it's a file name, then go ahead and if it's a file name, then go ahead and if it's a file name, then go ahead and print that whole file to the console." print that whole file to the console." print that whole file to the console." Like there's different things you can do Like there's different things you can do Like there's different things you can do based upon what you're passing it. So, based upon what you're passing it. So, based upon what you're passing it. So, this, you know, this stuff right here, this, you know, this stuff right here, this, you know, this stuff right here, this is all you have to figure out what this is all you have to figure out what this is all you have to figure out what do you want to do with the data that I do you want to do with the data that I do you want to do with the data that I have, but the rest of it is how to parse have, but the rest of it is how to parse have, but the rest of it is how to parse that data properly, okay? So, that's it.
-
that data properly, okay? So, that's it. that data properly, okay? So, that's it. Thanks for watching. The source code's Thanks for watching. The source code's Thanks for watching. The source code's down below, and as always, I am Tim down below, and as always, I am Tim down below, and as always, I am Tim Corey. Corey. Corey. >> [music]
Summary
This tech transcript discusses leveraging the `System.CommandLine` NuGet package from Microsoft for more robust command-line argument parsing in .NET applications, contrasting it with manual methods. The practical takeaway is that this package offers an efficient solution for handling arguments, with the choice between manual and package-based parsing depending on application complexity.