0

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
        }
    }
}
9
  • 1
    ok so whats the question? Commented Aug 16, 2016 at 10:24
  • The question is that i want to parse the port along with the arguments, how do i do that? (Updated the question Commented Aug 16, 2016 at 10:25
  • 2
    arrays are 0 indexed Commented Aug 16, 2016 at 10:27
  • "it gives me the wrong option" doesn't describe exactly what you're doing or what you're seeing. Commented Aug 16, 2016 at 10:27
  • 1
    @Sayse so you mean that i should start at 0? in the port parts? Commented Aug 16, 2016 at 10:31

2 Answers 2

1

It seems, that you want port being the last parameter:

  static void Main(string[] args) {  
    // if we have parameters...
    if (args.Length > 0) { 
      //TODO: int.TryParse is a better choice
      int port = int.Parse(args[args.Length - 1]); // ... port is the last one
      server = new TcpListener(IPAddress.Any, port);
      // Rest of the program
    }
  }

Edit: if you want to pass just two parameters (option and port)

  static void Main(string[] args) { 
    if (args.Length == 2) {
      //TODO: int.TryParse is a better choice 
      int option = int.Parse(args[0]);
      int port = int.Parse(args[1]);

      // Rest of the program, e.g.
      if (option == 1) {
        ...  
      } 
      else if (option == 2) {
        ...
      }
      else if (option == 3) {
        ...
      }   
    }
  }
Sign up to request clarification or add additional context in comments.

5 Comments

So that allows me to select the each configuration, so if i choose option one i would write "program 1 1234" 1 being the first config/option and 1234 being the last parameter for port.
@Freon: Yes, as far as I can see from your code, you're looking for something like that, with port being the last parameter
Because like before i had it via user input on console, so like if (option == "1"){//Rest of program} . Now i want to do the same but just passing the parameters. Anyway when i run your version it always gives me the first option, i cant choose option 2
@Freon: in this case, first read exactly two parameters (option and port) and then put the if on option value, not on args.Length; see my edit
Thanks, i realised i could still use if statements/switch statements
0

Is this, what you want?

class MainClass
{
    static void Main(string[] args)
    {
        // Test if input arguments were supplied:
        var switchvalue = int.Parse(args[0]);
        if (switchvalue == 1)
                {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (switchvalue == 2)
        {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
        if (switchvalue == 3)
        {
            int port = int.Parse(args[1]);
            server = new TcpListener(IPAddress.Any, port);
            //Rest of the program
        }
    }
}

2 Comments

Kind of like that but when i type "program 1 1234" it just hangs, no errors. When i run on Vstudio it gives me Index was outside the bounds of the array.
Ahh yeah forgot to change the args to 1. It works now. It was hard to explain what i was wanting but thank you. Didnt know that you can easily use a switch statement to pass as a arg[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.