Skip to content

Commit

Permalink
Successful parsing of arguments
Browse files Browse the repository at this point in the history
On going basic launch of bot in CLI
  • Loading branch information
alwyn974 committed Jul 23, 2021
1 parent 838ea7a commit af9995f
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 5 deletions.
38 changes: 34 additions & 4 deletions src/main/java/re/alwyn974/minecraft/bot/cli/CLIParser.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package re.alwyn974.minecraft.bot.cli;

import org.apache.commons.cli.*;
import re.alwyn974.minecraft.bot.MinecraftBOT;

/**
* The command line parser
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.0
* @since 1.0.0
* @version 1.0.9
* @since 1.0.9
*/
public class CLIParser {

private static final Options options = new Options();
private static CommandLine cmd;

/**
* Parse the command line arguments
Expand All @@ -21,13 +23,41 @@ public class CLIParser {
public static void parse(String... args) throws ParseException {
addOptions();
CommandLineParser parser = new DefaultParser();
parser.parse(options, args);
cmd = parser.parse(options, args);
ParseResult result = parseResult();
if (result.shouldPrintHelp())
printHelp();
}

/**
* Add the options
*/
private static void addOptions() {
options.addOption("h", "host", true, "Setup the host value (Default=127.0.0.1)");
options.addOption("p", "port", true, "Setup the port value (Default=25565)");
options.addOption("u", "user", true, "email of the user");
options.addOption("u", "user", true, "Email of the user");
options.addOption(null, "password", true, "Password of the user");
options.addOption("d", "debug", false, "Activate debug");
options.addOption("s", "status", false, "Display only the status of the server");
options.addOption(null, "help", false, "Show this help page");
}

private static ParseResult parseResult() {
ParseResult result = new ParseResult();
result.setHost(cmd.hasOption("h") ? cmd.getOptionValue("h") : MinecraftBOT.getHost());
result.setPort(Integer.parseInt(cmd.hasOption("p") ? cmd.getOptionValue("p") : MinecraftBOT.getPort()));
result.setEmail(cmd.hasOption("u") ? cmd.getOptionValue("u") : MinecraftBOT.getUsername());
result.setPassword(cmd.hasOption("password") ? cmd.getOptionValue("password") : MinecraftBOT.getPassword());
result.setStatus(cmd.hasOption("s"));
result.setDebug(cmd.hasOption("d"));
result.setHelp(cmd.hasOption("help"));
return result;
}

public static void printHelp() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(MinecraftBOT.getProjectName(), options);
System.exit(0);
}

}
158 changes: 158 additions & 0 deletions src/main/java/re/alwyn974/minecraft/bot/cli/ParseResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package re.alwyn974.minecraft.bot.cli;

/**
* The resulf of parsed args
*
* @author <a href="https://github.com/alwyn974">Alwyn974</a>
* @version 1.0.9
* @since 1.0.9
*/
public class ParseResult {

private String host;
private Integer port;
private String email;
private String password;
private Boolean debug;
private Boolean status;
private Boolean help;

/**
* Get the host
*
* @return the host
*/
public String getHost() {
return host;
}

/**
* Set the host
*
* @param host the host
*/
public void setHost(String host) {
this.host = host;
}

/**
* Get the port
*
* @return the port
*/
public Integer getPort() {
return port;
}

/**
* Set the port
*
* @param port the port
*/
public void setPort(Integer port) {
this.port = port;
}

/**
* Get the email
*
* @return the email
*/
public String getEmail() {
return email;
}

/**
* Set the email
*
* @param email the email
*/
public void setEmail(String email) {
this.email = email;
}

/**
* Get the password
*
* @return the password
*/
public String getPassword() {
return password;
}

/**
* Set the password
*
* @param password the password
*/
public void setPassword(String password) {
this.password = password;
}

/**
* Get if debug is activate
*
* @return debug value
*/
public Boolean isDebug() {
return debug;
}

/**
* Set debug
*
* @param debug debug value
*/
public void setDebug(Boolean debug) {
this.debug = debug;
}

/**
* Get if status is activate
*
* @return status value
*/
public Boolean shouldPrintStatus() {
return status;
}

/**
* The status value
*
* @param status status value
*/
public void setStatus(Boolean status) {
this.status = status;
}

/**
* Get if help is activate
*
* @return help value
*/
public Boolean shouldPrintHelp() {
return help;
}

/**
* Set help value
*
* @param help help value
*/
public void setHelp(Boolean help) {
this.help = help;
}

@Override
public String toString() {
return "ParseResult{" +
"host='" + host + '\'' +
", port=" + port +
", email='" + email + '\'' +
", password='" + password + '\'' +
", debug=" + debug +
", status=" + status +
", help=" + help +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class MCBOTPanel extends JPanel implements ActionListener {

private final JTextField usernameField = new JTextField(MinecraftBOT.getUsername());
private final JPasswordField passwordField = new JPasswordField(MinecraftBOT.getPassword());
private final JTextField hostField = new JTextField( MinecraftBOT.getHost());
private final JTextField hostField = new JTextField(MinecraftBOT.getHost());
private final JTextField portField = new JTextField(MinecraftBOT.getPort());
private final JTextField outputField = new JTextField();

Expand Down

0 comments on commit af9995f

Please sign in to comment.