From d09f159ca82d74350e3611c46337abdcd203e105 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Mon, 29 Dec 2014 16:28:26 +0100 Subject: [PATCH 01/11] 'Outsourced' option class. --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 278 ++++-------------- .../mmo/cmdlineoptions/CommandLineOption.java | 222 ++++++++++++++ 2 files changed, 280 insertions(+), 220 deletions(-) create mode 100644 java/src/to/mmo/cmdlineoptions/CommandLineOption.java diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 274046e..7eb12f0 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -18,186 +18,17 @@ public class CmdOptions { private static CmdOptions instance; private static String optionChar; - private HashMap options; + private HashMap options; private boolean showOptions; private boolean combineSwitches; private boolean dontQuitOnError; - public static class Option { - private String name; - private ArrayList cmd; - private ArrayList cmdLong; - private String description; - private ArrayList defaultParameter; - - private ArrayList possibleParams; - private boolean set; - private boolean required; - private ArrayList values; - private int maxParameters, minParameters; - private ArrayList examples; - private int stepSizeParameters; - - public ArrayList getValues() { - return values; - } - - public ArrayList getDefaultParameter() { - return defaultParameter; - } - - public Option() { - values = new ArrayList(); - cmd = new ArrayList(); - cmdLong = new ArrayList(); - defaultParameter = new ArrayList(); - possibleParams = new ArrayList(); - examples = new ArrayList(); - } - - public Option addCommand(String cmd) { - if (cmd.contains(optionChar)) { - cmd = cmd.replace(optionChar, ""); - } - if (cmd.length() > 1) { - throw new IllegalArgumentException( - "Command longer than 1 character, which is not allowed. Use 'addLongCommand()' instead!"); - } - this.cmd.add(cmd); - return this; - } - - public Option addLongCommand(String cmd) { - if (cmd.contains(optionChar)) - cmd = cmd.replace(optionChar, ""); - this.cmdLong.add(cmd); - return this; - } - - public Option addDefaultParameter(String d) { - this.defaultParameter.add(d); - return this; - } - - public Option addPossibleParameter(String p) { - this.possibleParams.add(p); - return this; - } - - public Option addValue(String value) { - this.values.add(value); - return this; - } - - public String getName() { - return name; - } - - public Option setName(String name) { - this.name = name; - return this; - } - - public Option setParameterCount(int min, int max) { - return this.setParameterCount(min, max, 0); - } - - public Option setParameterCount(int min, int max, int step) { - this.minParameters = min; - this.maxParameters = max; - this.stepSizeParameters = step; - return this; - } - - public String getDescription() { - return description; - } - - public Option setDescription(String description) { - this.description = description; - return this; - } - - public Option setRequired(boolean required) { - this.required = true; - return this; - } - - public boolean isSet() { - return set; - } - - public boolean valuesContains(String value) { - return values.contains(value); - } - - public int getIndexOf(String value) { - return values.indexOf(value); - } - - public Option setSet(boolean set) { - this.set = set; - return this; - } - - public Option addExample(String example) { - this.examples.add(example); - return this; - } - - public String toString() { - return toString(false); - } - - public String toString(boolean help) { - String ret = name; - ret += " ("; - for (String s : cmd) { - ret += optionChar + s + ", "; - } - for (String s : cmdLong) { - ret += optionChar + optionChar + s + ", "; - } - ret += ")"; - if (help && defaultParameter.size() > 0) { - ret += ": default="; - for (String s : defaultParameter) { - ret += s + ","; - } - } - ret += (help && description != null ? "\n\t\t" + description : ""); - if (help && possibleParams.size() > 0) { - boolean start = true; - ret += "\n\t\t(Possible parameters: "; - for (String s : possibleParams) { - ret += (start ? "" : ", ") + s; - start = false; - } - ret += ")"; - } - if (set) { - ret += "\n\t\t--> current Setting: "; - if (values.size() > 0) { - boolean start = true; - for (String s : values) { - ret += (start ? "" : ",") + s; - start = false; - } - } else { - ret += "true"; - } - // ret += "\n"; - } - return ret; - } - } - private CmdOptions() { optionChar = "-"; this.setSwitchCombination(false); this.setShowOptions(false); this.setDontQuitOnError(false); - options = new HashMap(); + options = new HashMap(); this.createOption("help") .setDescription( "Show all possible options and their parameters.") @@ -233,14 +64,13 @@ public class CmdOptions { } } - public Option createOption(String name) { - Option o = new Option(); - o.setName(name); + public CommandLineOption createOption(String name) { + CommandLineOption o = new CommandLineOption(name); this.options.put(name, o); return o; } - public Option getBareOption(String name) { + public CommandLineOption getBareOption(String name) { return options.get(name); } @@ -257,10 +87,11 @@ public class CmdOptions { } public String[] getOption(String name) { - if (options.get(name).values.size() > 0) - return options.get(name).values.toArray(new String[0]); - else if (options.get(name).defaultParameter != null) + if (options.get(name).getValues().size() > 0) return options.get(name).getValues().toArray(new String[0]); + else if (options.get(name).getDefaultParameter().size() > 0) + return options.get(name).getDefaultParameter() + .toArray(new String[0]); return null; } @@ -272,9 +103,9 @@ public class CmdOptions { } public Integer[] getOptionAsInt(String name) { - if (options.get(name).values.size() > 0) { + if (options.get(name).getValues().size() > 0) { ArrayList list = new ArrayList(); - for (String o : options.get(name).values) { + for (String o : options.get(name).getValues()) { list.add(Integer.parseInt(o)); } return list.toArray(new Integer[0]); @@ -297,9 +128,9 @@ public class CmdOptions { } public Double[] getOptionAsDouble(String name) { - if (options.get(name).values.size() > 0) { + if (options.get(name).getValues().size() > 0) { ArrayList list = new ArrayList(); - for (String o : options.get(name).values) { + for (String o : options.get(name).getValues()) { list.add(Double.parseDouble(o)); } return list.toArray(new Double[0]); @@ -322,7 +153,7 @@ public class CmdOptions { } public boolean isSet(String option) { - return options.get(option).set; + return options.get(option).isSet(); } public boolean isSet(String option, String parameter) { @@ -330,8 +161,8 @@ public class CmdOptions { } public void resetValues() { - for (Option o : options.values()) { - o.values.clear(); + for (CommandLineOption o : options.values()) { + o.getValues().clear(); } } @@ -345,14 +176,15 @@ public class CmdOptions { b.append("Possible options:\n"); } b.append("-options\n"); - Option[] vars = options.values().toArray(new Option[0]); - Arrays.sort(vars, new Comparator