I am trying to make my program run from commands line arguments, so i have 3 options in my code that you can choose to run.
The question is that i want to parse the port along with the arguments, how do i do that?
Each option has a different program configuration. My attempt show below; so inside the the program i also want to pass the the port as a argument so when i write "program 1 5656" in the console. The application sees that its the first option 1 to run and then parses the 5656 into the port variable.
I tried below but when i enter the command it gives me the wrong option as in it start option 2 instead of 1.
class MainClass
{
static void Main(string[] args)
{
// Test if input arguments were supplied:
if (args.Length == 1)
{
int port = int.Parse(args[1]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
if (args.Length == 2)
{
int port = int.Parse(args[2]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
if (args.Length == 3)
{
int port = int.Parse(args[3]);
server = new TcpListener(IPAddress.Any, port);
//Rest of the program
}
}
}