11package me .replydev .qubo ;
22
3+ import lombok .Getter ;
4+ import lombok .Setter ;
35import lombok .Value ;
46import lombok .experimental .NonFinal ;
57import me .replydev .utils .IpList ;
68import me .replydev .utils .PortList ;
7- import me .replydev .utils .SearchFilter ;
89import org .apache .commons .cli .*;
910
10- @ Value
11+ /**
12+ * This class is responsible for parsing the command line arguments.
13+ * @author ReplyDev, Swofty
14+ */
15+ @ Getter
1116public class CommandLineArgs {
1217
13- Options options ;
14- IpList ipList ;
15- PortList portRange ;
16- boolean skipCommon ;
17- int timeout ;
18- SearchFilter searchFilter ;
19- int count ;
20-
21- @ NonFinal
22- CommandLine cmd ;
23-
18+ private final Options options ;
19+ private final IpList ipList ;
20+ private final PortList portRange ;
21+ private final boolean skipCommon ;
22+ private final int timeout ;
23+ private final SearchFilter searchFilter ;
24+ private final int count ;
25+
26+ @ Setter
27+ private CommandLine cmd ;
28+
29+ /**
30+ * Constructor for CommandLineArgs.
31+ * @param command The array of command line arguments to be parsed.
32+ * @throws NumberFormatException If parsing of numeric values fails.
33+ */
2434 public CommandLineArgs (String [] command ) throws NumberFormatException {
2535 options = buildOptions ();
2636 CommandLineParser parser = new DefaultParser ();
@@ -32,7 +42,7 @@ public CommandLineArgs(String[] command) throws NumberFormatException {
3242 ipList = new IpList (cmd .getOptionValue ("i" ));
3343 portRange = new PortList (cmd .getOptionValue ("p" ));
3444 skipCommon = !cmd .hasOption ("all" );
35- timeout = Integer .parseInt (cmd .getOptionValue ("t" ));
45+ timeout = Integer .parseInt (cmd .getOptionValue ("t" , "1000" ));
3646
3747 searchFilter =
3848 SearchFilter
@@ -45,57 +55,33 @@ public CommandLineArgs(String[] command) throws NumberFormatException {
4555 count = Integer .parseInt (cmd .getOptionValue ("c" , "1" ));
4656 }
4757
58+ /**
59+ * Builds the command line options.
60+ * @see Options
61+ * @return Options The command line options.
62+ */
4863 private static Options buildOptions () {
49- Option iprange = new Option ("i" , "iprange" , true , "The IP range to scan" );
50- iprange .setRequired (true );
51-
52- Option portrange = new Option ("p" , "portrange" , true , "The range of ports to scan" );
53- portrange .setRequired (true );
54-
55- Option timeout = new Option ("t" , "timeout" , true , "TCP connection timeout" );
56- timeout .setRequired (true );
57-
58- Option count = new Option ("c" , "pingcount" , true , "Number of ping retries" );
59- count .setRequired (false );
60-
61- Option all = new Option ("a" , false , "Force Qubo to scan broadcast IPs and common ports" );
62- all .setRequired (false );
63-
64- Option filterVersion = new Option (
65- "v" ,
66- "filterversion" ,
67- true ,
68- "Show only hits with given version"
69- );
70- filterVersion .setRequired (false );
71-
72- Option filterMotd = new Option ("m" , "filtermotd" , true , "Show only hits with given motd" );
73- filterMotd .setRequired (false );
74-
75- Option filterOn = new Option (
76- "o" ,
77- "minonline" ,
78- true ,
79- "Show only hits with at least <arg> players online"
80- );
81- filterOn .setRequired (false );
82-
8364 Options options = new Options ();
84- options .addOption (iprange );
85- options .addOption (portrange );
86- options .addOption (timeout );
87- options .addOption (count );
88- options .addOption (all );
89- options .addOption (filterVersion );
90- options .addOption (filterMotd );
91- options .addOption (filterOn );
65+
66+ options .addRequiredOption ("i" , "iprange" , true , "The IP range to scan" );
67+ options .addRequiredOption ("p" , "portrange" , true , "The range of ports to scan" );
68+ options .addOption ("t" , "timeout" , true , "TCP connection timeout" );
69+ options .addOption ("c" , "pingcount" , true , "Number of ping retries" );
70+ options .addOption ("a" , "all" , false , "Force to scan broadcast IPs and common ports" );
71+ options .addOption ("v" , "filterversion" , true , "Show only hits with given version" );
72+ options .addOption ("m" , "filtermotd" , true , "Show only hits with given motd" );
73+ options .addOption ("o" , "minonline" , true , "Show only hits with at least <arg> players online" );
9274
9375 return options ;
9476 }
9577
78+ /**
79+ * Prints help information using the command line options and exits the program
80+ * with exit code -1.
81+ */
9682 public void showHelpAndExit () {
9783 HelpFormatter formatter = new HelpFormatter ();
98- formatter .printHelp ("-range <arg > -ports <arg > -th <arg> -ti <arg> " , options );
84+ formatter .printHelp ("Usage: -i <iprange > -p <portrange > -t <timeout> [-c <pingcount>] [...] " , options );
9985 System .exit (-1 );
10086 }
10187}
0 commit comments