11package com .anas .jdosattacker .args ;
22
3+ import com .anas .jdosattacker .FieldException ;
34import com .anas .jdosattacker .MainController ;
45import com .anas .jdosattacker .request .Requester ;
56import org .apache .commons .cli .*;
67
8+ /**
9+ * It parses the command line arguments and sets the corresponding variables, and if there are any errors, it prints the
10+ * help and exits
11+ */
712public class ArgumentsProcessor {
8- private Options options ;
9- private CommandLineParser parser ;
13+ private final Options options ;
1014
15+ /**
16+ * The constructor.
17+ */
1118 public ArgumentsProcessor () {
12- initialize ();
19+ options = new Options ();
1320 setupOptions ();
1421 }
1522
23+ /**
24+ * Set up the command line options.
25+ */
1626 private void setupOptions () {
1727 options .addOption ("h" , "help" , false , "Print this help" );
1828 options .addOption ("v" , "version" , false , "Print version" );
@@ -24,45 +34,58 @@ private void setupOptions() {
2434 options .addOption ("connectTimeout" , true , "Connection timeout (default: 5000)" );
2535 }
2636
27- private void initialize () {
28- options = new Options ();
29- parser = new DefaultParser ();
30- }
3137
32- public void process (String [] args ) {
38+ /**
39+ * It parses the command line arguments and sets the corresponding variables,
40+ * and if there are any errors, it prints the help and exits.
41+ * and if the arguments have a help or version flag, it prints the info and exits.
42+ *
43+ * @param args the command line arguments
44+ */
45+ public void process (final String [] args ) {
3346 try {
34- CommandLine commandLine = parser .parse (options , args );
47+ final var commandLine = new DefaultParser () .parse (options , args );
3548 if (commandLine .hasOption ("help" )) {
36- printHelp ();
49+ printHelp (0 );
3750 } else if (commandLine .hasOption ("version" )) {
3851 printVersion ();
39- } else if (commandLine .hasOption ("url" ))
40- Requester .setUrl (commandLine .getOptionValue ("url" ));
52+ }
4153
54+ if (commandLine .hasOption ("url" ))
55+ Requester .setUrl (commandLine .getOptionValue ("url" ));
4256 if (commandLine .hasOption ("threads" ))
43- MainController .setThreadsNum (Integer . parseInt ( commandLine .getOptionValue ("threads" ) ));
57+ MainController .setThreadsNum (commandLine .getOptionValue ("threads" ));
4458 if (commandLine .hasOption ("number" ))
45- Requester .setReqNumber (Integer . parseInt ( commandLine .getOptionValue ("number" ) ));
59+ Requester .setReqNumber (commandLine .getOptionValue ("number" ));
4660 if (commandLine .hasOption ("connectTimeout" ))
47- Requester .setConnectTimeout (Integer . parseInt ( commandLine .getOptionValue ("connectTimeout" ) ));
61+ Requester .setConnectTimeout (commandLine .getOptionValue ("connectTimeout" ));
4862 if (commandLine .hasOption ("useragent" ))
4963 Requester .setUserAgent (commandLine .getOptionValue ("useragent" ));
5064 if (commandLine .hasOption ("requestMethod" ))
5165 Requester .setRequestMethod (commandLine .getOptionValue ("requestMethod" ));
5266
53- } catch (Exception e ) {
67+ } catch (final ParseException | FieldException e ) {
68+ // If an error occurs, print the error message and a help message and exit.
5469 System .err .println ("Error: " + e .getMessage ());
55- printHelp ();
56- System .exit (1 );
70+ printHelp (1 );
5771 }
5872 }
5973
74+ /**
75+ * If the user types in the command line argument -v, this method will be called.
76+ */
6077 private void printVersion () {
61- System .out .println ("Version: " + MainController .version );
78+ System .out .println ("Version: " + MainController .VERSION );
6279 System .exit (0 );
6380 }
6481
65- private void printHelp () {
66- new HelpFormatter ().printHelp ("java -jar jdosattacker.jar -u <URL>" , options );
82+ /**
83+ * Prints the help message and exits the program.
84+ *
85+ * @param exitCode The exit code to use when exiting the application.
86+ */
87+ private void printHelp (final int exitCode ) {
88+ new HelpFormatter ().printHelp ("java -jar jdosattacker.jar [options]" , options );
89+ System .exit (exitCode );
6790 }
6891}
0 commit comments