package com.it_jaros.networkScanner; public class CliParser { public ScanTarget parseArgs(String[] args) { if (args.length < 1 || args[0].trim().equals("")) { throw new IllegalArgumentException("No argument given"); } String target = null; int socketLimit = 1000; int delayInMillis = 0; for (int i = 0; i < args.length; i++) { final String currentArg = args[i]; if (!currentArg.startsWith("-")) { target = currentArg; continue; } try { switch (currentArg) { case "--help", "-h": App.usageAndExit(); case "--socketLimit", "-sl": socketLimit = Integer.parseInt(args[++i]); break; case "--delay", "-d": delayInMillis = Integer.parseInt(args[++i]); break; default: throw new IllegalArgumentException("No such param " + currentArg); } } catch (IndexOutOfBoundsException | NumberFormatException e) { System.out.printf("Missing or wrong argument for option %s", currentArg); System.exit(2); } } System.out.printf( "Scanning target '%s' with socketLimit(%s), delayInMillis(%s) \n", target, socketLimit, delayInMillis); return new ScanTarget(target, socketLimit, delayInMillis); } }