summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/networkScanner/CliParser.java
blob: 6f7c4b16fb65946814208b987e890420131a0a83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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);
    }

}