@Test(expected = IllegalArgumentException.class) public void testInvalidParameter() { CommandLineArguments.parse(ACCEPT_ALL, "myserial", "other"); }
public static CommandLineArguments parse(int acceptedParameters, String... args) { CommandLineArguments arguments = new CommandLineArguments(); for (int i = 0; i < args.length; ++i) { String arg = args[i]; if ((acceptedParameters & PARAM_DNS_SERVER) != 0 && "-d".equals(arg)) { if (arguments.dnsServers != null) { throw new IllegalArgumentException("DNS servers already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -d parameter"); } arguments.dnsServers = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_ROUTES) != 0 && "-r".equals(arg)) { if (arguments.routes != null) { throw new IllegalArgumentException("Routes already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -r parameter"); } arguments.routes = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_PORT) != 0 && "-p".equals(arg)) { if (arguments.port != 0) { throw new IllegalArgumentException("Port already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -p parameter"); } arguments.port = Integer.parseInt(args[i + 1]); if (arguments.port <= 0 || arguments.port >= 65536) { throw new IllegalArgumentException("Invalid port: " + arguments.port); } ++i; } else if ((acceptedParameters & PARAM_SERIAL) != 0 && arguments.serial == null) { arguments.serial = arg; } else { throw new IllegalArgumentException("Unexpected argument: \"" + arg + "\""); } } if (arguments.port == 0) { arguments.port = DEFAULT_PORT; } return arguments; }
CommandLineArguments { public static CommandLineArguments parse(int acceptedParameters, String... args) { CommandLineArguments arguments = new CommandLineArguments(); for (int i = 0; i < args.length; ++i) { String arg = args[i]; if ((acceptedParameters & PARAM_DNS_SERVER) != 0 && "-d".equals(arg)) { if (arguments.dnsServers != null) { throw new IllegalArgumentException("DNS servers already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -d parameter"); } arguments.dnsServers = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_ROUTES) != 0 && "-r".equals(arg)) { if (arguments.routes != null) { throw new IllegalArgumentException("Routes already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -r parameter"); } arguments.routes = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_PORT) != 0 && "-p".equals(arg)) { if (arguments.port != 0) { throw new IllegalArgumentException("Port already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -p parameter"); } arguments.port = Integer.parseInt(args[i + 1]); if (arguments.port <= 0 || arguments.port >= 65536) { throw new IllegalArgumentException("Invalid port: " + arguments.port); } ++i; } else if ((acceptedParameters & PARAM_SERIAL) != 0 && arguments.serial == null) { arguments.serial = arg; } else { throw new IllegalArgumentException("Unexpected argument: \"" + arg + "\""); } } if (arguments.port == 0) { arguments.port = DEFAULT_PORT; } return arguments; } }
CommandLineArguments { public static CommandLineArguments parse(int acceptedParameters, String... args) { CommandLineArguments arguments = new CommandLineArguments(); for (int i = 0; i < args.length; ++i) { String arg = args[i]; if ((acceptedParameters & PARAM_DNS_SERVER) != 0 && "-d".equals(arg)) { if (arguments.dnsServers != null) { throw new IllegalArgumentException("DNS servers already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -d parameter"); } arguments.dnsServers = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_ROUTES) != 0 && "-r".equals(arg)) { if (arguments.routes != null) { throw new IllegalArgumentException("Routes already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -r parameter"); } arguments.routes = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_PORT) != 0 && "-p".equals(arg)) { if (arguments.port != 0) { throw new IllegalArgumentException("Port already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -p parameter"); } arguments.port = Integer.parseInt(args[i + 1]); if (arguments.port <= 0 || arguments.port >= 65536) { throw new IllegalArgumentException("Invalid port: " + arguments.port); } ++i; } else if ((acceptedParameters & PARAM_SERIAL) != 0 && arguments.serial == null) { arguments.serial = arg; } else { throw new IllegalArgumentException("Unexpected argument: \"" + arg + "\""); } } if (arguments.port == 0) { arguments.port = DEFAULT_PORT; } return arguments; }  }
CommandLineArguments { public static CommandLineArguments parse(int acceptedParameters, String... args) { CommandLineArguments arguments = new CommandLineArguments(); for (int i = 0; i < args.length; ++i) { String arg = args[i]; if ((acceptedParameters & PARAM_DNS_SERVER) != 0 && "-d".equals(arg)) { if (arguments.dnsServers != null) { throw new IllegalArgumentException("DNS servers already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -d parameter"); } arguments.dnsServers = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_ROUTES) != 0 && "-r".equals(arg)) { if (arguments.routes != null) { throw new IllegalArgumentException("Routes already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -r parameter"); } arguments.routes = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_PORT) != 0 && "-p".equals(arg)) { if (arguments.port != 0) { throw new IllegalArgumentException("Port already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -p parameter"); } arguments.port = Integer.parseInt(args[i + 1]); if (arguments.port <= 0 || arguments.port >= 65536) { throw new IllegalArgumentException("Invalid port: " + arguments.port); } ++i; } else if ((acceptedParameters & PARAM_SERIAL) != 0 && arguments.serial == null) { arguments.serial = arg; } else { throw new IllegalArgumentException("Unexpected argument: \"" + arg + "\""); } } if (arguments.port == 0) { arguments.port = DEFAULT_PORT; } return arguments; }  static CommandLineArguments parse(int acceptedParameters, String... args); String getSerial(); String getDnsServers(); String getRoutes(); int getPort(); }
CommandLineArguments { public static CommandLineArguments parse(int acceptedParameters, String... args) { CommandLineArguments arguments = new CommandLineArguments(); for (int i = 0; i < args.length; ++i) { String arg = args[i]; if ((acceptedParameters & PARAM_DNS_SERVER) != 0 && "-d".equals(arg)) { if (arguments.dnsServers != null) { throw new IllegalArgumentException("DNS servers already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -d parameter"); } arguments.dnsServers = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_ROUTES) != 0 && "-r".equals(arg)) { if (arguments.routes != null) { throw new IllegalArgumentException("Routes already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -r parameter"); } arguments.routes = args[i + 1]; ++i; } else if ((acceptedParameters & PARAM_PORT) != 0 && "-p".equals(arg)) { if (arguments.port != 0) { throw new IllegalArgumentException("Port already set"); } if (i == args.length - 1) { throw new IllegalArgumentException("Missing -p parameter"); } arguments.port = Integer.parseInt(args[i + 1]); if (arguments.port <= 0 || arguments.port >= 65536) { throw new IllegalArgumentException("Invalid port: " + arguments.port); } ++i; } else if ((acceptedParameters & PARAM_SERIAL) != 0 && arguments.serial == null) { arguments.serial = arg; } else { throw new IllegalArgumentException("Unexpected argument: \"" + arg + "\""); } } if (arguments.port == 0) { arguments.port = DEFAULT_PORT; } return arguments; }  static CommandLineArguments parse(int acceptedParameters, String... args); String getSerial(); String getDnsServers(); String getRoutes(); int getPort(); static final int PARAM_NONE; static final int PARAM_SERIAL; static final int PARAM_DNS_SERVER; static final int PARAM_ROUTES; static final int PARAM_PORT; static final int DEFAULT_PORT; }