I'm working on a program public class Talk
that connects client and server in two way communication. I will be running the program using a Terminal window. How do I add command line options so that these work Talk –h [hostname | IPaddress] [–p portnumber]
, or Talk –s [–p portnumber]
, or Talk –a [hostname|IPaddress] [–p portnumber]
, or Talk –help
This is what I have so far:
if (args[0] == "-h" && args[2] == "-p" && (Integer.parseInt(args[3]) % 1 == 0)) {
// code for Talk –h [hostname | IPaddress] [–p portnumber]
} else if (args[0] == "-s" && args[1] == "-p" && (Integer.parseInt(args[2]) % 1 == 0)) {
// code for Talk –s [–p portnumber]
} else if (args[0] == "-a" && args[2] == "-p" && (Integer.parseInt(args[3]) % 1 == 0)) {
// code for Talk –a [hostname|IPaddress] [–p portnumber]
} else if (args[0] == "-help") {
// code for Talk –help
} else {
System.out.println("Invalid invocation, use -help for instructions on how to use program");
System.exit(-1);
}
Is this the correct way to add command-line options? and their constraints?