Skip to content

Commit af9995f

Browse files
committed
Successful parsing of arguments
On going basic launch of bot in CLI
1 parent 838ea7a commit af9995f

File tree

3 files changed

+193
-5
lines changed

3 files changed

+193
-5
lines changed
Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package re.alwyn974.minecraft.bot.cli;
22

33
import org.apache.commons.cli.*;
4+
import re.alwyn974.minecraft.bot.MinecraftBOT;
45

56
/**
67
* The command line parser
78
*
89
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
9-
* @version 1.0.0
10-
* @since 1.0.0
10+
* @version 1.0.9
11+
* @since 1.0.9
1112
*/
1213
public class CLIParser {
1314

1415
private static final Options options = new Options();
16+
private static CommandLine cmd;
1517

1618
/**
1719
* Parse the command line arguments
@@ -21,13 +23,41 @@ public class CLIParser {
2123
public static void parse(String... args) throws ParseException {
2224
addOptions();
2325
CommandLineParser parser = new DefaultParser();
24-
parser.parse(options, args);
26+
cmd = parser.parse(options, args);
27+
ParseResult result = parseResult();
28+
if (result.shouldPrintHelp())
29+
printHelp();
2530
}
2631

32+
/**
33+
* Add the options
34+
*/
2735
private static void addOptions() {
2836
options.addOption("h", "host", true, "Setup the host value (Default=127.0.0.1)");
2937
options.addOption("p", "port", true, "Setup the port value (Default=25565)");
30-
options.addOption("u", "user", true, "email of the user");
38+
options.addOption("u", "user", true, "Email of the user");
39+
options.addOption(null, "password", true, "Password of the user");
40+
options.addOption("d", "debug", false, "Activate debug");
41+
options.addOption("s", "status", false, "Display only the status of the server");
42+
options.addOption(null, "help", false, "Show this help page");
43+
}
44+
45+
private static ParseResult parseResult() {
46+
ParseResult result = new ParseResult();
47+
result.setHost(cmd.hasOption("h") ? cmd.getOptionValue("h") : MinecraftBOT.getHost());
48+
result.setPort(Integer.parseInt(cmd.hasOption("p") ? cmd.getOptionValue("p") : MinecraftBOT.getPort()));
49+
result.setEmail(cmd.hasOption("u") ? cmd.getOptionValue("u") : MinecraftBOT.getUsername());
50+
result.setPassword(cmd.hasOption("password") ? cmd.getOptionValue("password") : MinecraftBOT.getPassword());
51+
result.setStatus(cmd.hasOption("s"));
52+
result.setDebug(cmd.hasOption("d"));
53+
result.setHelp(cmd.hasOption("help"));
54+
return result;
55+
}
56+
57+
public static void printHelp() {
58+
HelpFormatter formatter = new HelpFormatter();
59+
formatter.printHelp(MinecraftBOT.getProjectName(), options);
60+
System.exit(0);
3161
}
3262

3363
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package re.alwyn974.minecraft.bot.cli;
2+
3+
/**
4+
* The resulf of parsed args
5+
*
6+
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
7+
* @version 1.0.9
8+
* @since 1.0.9
9+
*/
10+
public class ParseResult {
11+
12+
private String host;
13+
private Integer port;
14+
private String email;
15+
private String password;
16+
private Boolean debug;
17+
private Boolean status;
18+
private Boolean help;
19+
20+
/**
21+
* Get the host
22+
*
23+
* @return the host
24+
*/
25+
public String getHost() {
26+
return host;
27+
}
28+
29+
/**
30+
* Set the host
31+
*
32+
* @param host the host
33+
*/
34+
public void setHost(String host) {
35+
this.host = host;
36+
}
37+
38+
/**
39+
* Get the port
40+
*
41+
* @return the port
42+
*/
43+
public Integer getPort() {
44+
return port;
45+
}
46+
47+
/**
48+
* Set the port
49+
*
50+
* @param port the port
51+
*/
52+
public void setPort(Integer port) {
53+
this.port = port;
54+
}
55+
56+
/**
57+
* Get the email
58+
*
59+
* @return the email
60+
*/
61+
public String getEmail() {
62+
return email;
63+
}
64+
65+
/**
66+
* Set the email
67+
*
68+
* @param email the email
69+
*/
70+
public void setEmail(String email) {
71+
this.email = email;
72+
}
73+
74+
/**
75+
* Get the password
76+
*
77+
* @return the password
78+
*/
79+
public String getPassword() {
80+
return password;
81+
}
82+
83+
/**
84+
* Set the password
85+
*
86+
* @param password the password
87+
*/
88+
public void setPassword(String password) {
89+
this.password = password;
90+
}
91+
92+
/**
93+
* Get if debug is activate
94+
*
95+
* @return debug value
96+
*/
97+
public Boolean isDebug() {
98+
return debug;
99+
}
100+
101+
/**
102+
* Set debug
103+
*
104+
* @param debug debug value
105+
*/
106+
public void setDebug(Boolean debug) {
107+
this.debug = debug;
108+
}
109+
110+
/**
111+
* Get if status is activate
112+
*
113+
* @return status value
114+
*/
115+
public Boolean shouldPrintStatus() {
116+
return status;
117+
}
118+
119+
/**
120+
* The status value
121+
*
122+
* @param status status value
123+
*/
124+
public void setStatus(Boolean status) {
125+
this.status = status;
126+
}
127+
128+
/**
129+
* Get if help is activate
130+
*
131+
* @return help value
132+
*/
133+
public Boolean shouldPrintHelp() {
134+
return help;
135+
}
136+
137+
/**
138+
* Set help value
139+
*
140+
* @param help help value
141+
*/
142+
public void setHelp(Boolean help) {
143+
this.help = help;
144+
}
145+
146+
@Override
147+
public String toString() {
148+
return "ParseResult{" +
149+
"host='" + host + '\'' +
150+
", port=" + port +
151+
", email='" + email + '\'' +
152+
", password='" + password + '\'' +
153+
", debug=" + debug +
154+
", status=" + status +
155+
", help=" + help +
156+
'}';
157+
}
158+
}

src/main/java/re/alwyn974/minecraft/bot/gui/MCBOTPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class MCBOTPanel extends JPanel implements ActionListener {
4545

4646
private final JTextField usernameField = new JTextField(MinecraftBOT.getUsername());
4747
private final JPasswordField passwordField = new JPasswordField(MinecraftBOT.getPassword());
48-
private final JTextField hostField = new JTextField( MinecraftBOT.getHost());
48+
private final JTextField hostField = new JTextField(MinecraftBOT.getHost());
4949
private final JTextField portField = new JTextField(MinecraftBOT.getPort());
5050
private final JTextField outputField = new JTextField();
5151

0 commit comments

Comments
 (0)