From b7665c90eb0575c664b4b4b10b5e948ca94a4f71 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 22:19:01 +0100 Subject: [PATCH] bunch of changes to be test conform. --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 123 ++++++++++++++---- 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index f1f0f83..274046e 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -18,8 +18,10 @@ public class CmdOptions { private static CmdOptions instance; private static String optionChar; - + private HashMap options; + private boolean showOptions; private boolean combineSwitches; + private boolean dontQuitOnError; public static class Option { private String name; @@ -144,7 +146,12 @@ public class CmdOptions { } public String toString() { - String ret = name + " ("; + return toString(false); + } + + public String toString(boolean help) { + String ret = name; + ret += " ("; for (String s : cmd) { ret += optionChar + s + ", "; } @@ -152,14 +159,14 @@ public class CmdOptions { ret += optionChar + optionChar + s + ", "; } ret += ")"; - if (defaultParameter.size() > 0) { + if (help && defaultParameter.size() > 0) { ret += ": default="; for (String s : defaultParameter) { ret += s + ","; } } - ret += (description != null ? "\n\t\t" + description : ""); - if (possibleParams.size() > 0) { + 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) { @@ -185,11 +192,11 @@ public class CmdOptions { } } - private HashMap options; - private CmdOptions() { optionChar = "-"; this.setSwitchCombination(false); + this.setShowOptions(false); + this.setDontQuitOnError(false); options = new HashMap(); this.createOption("help") .setDescription( @@ -212,6 +219,14 @@ public class CmdOptions { this.optionChar = c; } + public void setDontQuitOnError(boolean set) { + this.dontQuitOnError = set; + } + + public void setShowOptions(boolean show) { + this.showOptions = show; + } + public void setHelpGeneration(boolean on) { if (!on) { options.remove("help"); @@ -233,6 +248,14 @@ public class CmdOptions { return getOption(name); } + public String get(String name, int index) { + String[] values = getOption(name); + if (values.length > index && index > 0) { + return values[index]; + } + return null; + } + public String[] getOption(String name) { if (options.get(name).values.size() > 0) return options.get(name).values.toArray(new String[0]); @@ -306,6 +329,16 @@ public class CmdOptions { return this.getValuesAsList(option).contains(parameter); } + public void resetValues() { + for (Option o : options.values()) { + o.values.clear(); + } + } + + public void reset() { + options.clear(); + } + public String toString(boolean help) { StringBuilder b = new StringBuilder(); if (help) { @@ -320,7 +353,7 @@ public class CmdOptions { } }); for (Option o : vars) { - b.append("\t").append(o.toString()).append("\n"); + b.append("\t").append(o.toString(help)).append("\n"); } b.append("/options\n"); return b.toString(); @@ -336,20 +369,24 @@ public class CmdOptions { return indices.toArray(new Integer[0]); } - private boolean optionExists(String option) { + private Option getOptionByCommand(String cmd) { for (Option o : this.options.values()) { for (String s : o.cmd) { - if (option.equals(s)) { - return true; + if (cmd.equals(s)) { + return o; } } for (String s : o.cmdLong) { - if (option.equals(s)) { - return true; + if (cmd.equals(s)) { + return o; } } } - return false; + return null; + } + + private boolean cmdExists(String cmd) { + return getOptionByCommand(cmd) != null; } private boolean switchExists(char c) { @@ -363,7 +400,21 @@ public class CmdOptions { return false; } - public void parse(String[] args) { + private Option[] getOptionBySwitches(String switches) { + List