bunch of changes to be test conform.

This commit is contained in:
Marcel Otte 2014-12-09 22:19:01 +01:00
parent 7d0aad767e
commit b7665c90eb
1 changed files with 95 additions and 28 deletions

View File

@ -18,8 +18,10 @@ public class CmdOptions {
private static CmdOptions instance; private static CmdOptions instance;
private static String optionChar; private static String optionChar;
private HashMap<String, Option> options;
private boolean showOptions;
private boolean combineSwitches; private boolean combineSwitches;
private boolean dontQuitOnError;
public static class Option { public static class Option {
private String name; private String name;
@ -144,7 +146,12 @@ public class CmdOptions {
} }
public String toString() { public String toString() {
String ret = name + " ("; return toString(false);
}
public String toString(boolean help) {
String ret = name;
ret += " (";
for (String s : cmd) { for (String s : cmd) {
ret += optionChar + s + ", "; ret += optionChar + s + ", ";
} }
@ -152,14 +159,14 @@ public class CmdOptions {
ret += optionChar + optionChar + s + ", "; ret += optionChar + optionChar + s + ", ";
} }
ret += ")"; ret += ")";
if (defaultParameter.size() > 0) { if (help && defaultParameter.size() > 0) {
ret += ": default="; ret += ": default=";
for (String s : defaultParameter) { for (String s : defaultParameter) {
ret += s + ","; ret += s + ",";
} }
} }
ret += (description != null ? "\n\t\t" + description : ""); ret += (help && description != null ? "\n\t\t" + description : "");
if (possibleParams.size() > 0) { if (help && possibleParams.size() > 0) {
boolean start = true; boolean start = true;
ret += "\n\t\t(Possible parameters: "; ret += "\n\t\t(Possible parameters: ";
for (String s : possibleParams) { for (String s : possibleParams) {
@ -185,11 +192,11 @@ public class CmdOptions {
} }
} }
private HashMap<String, Option> options;
private CmdOptions() { private CmdOptions() {
optionChar = "-"; optionChar = "-";
this.setSwitchCombination(false); this.setSwitchCombination(false);
this.setShowOptions(false);
this.setDontQuitOnError(false);
options = new HashMap<String, Option>(); options = new HashMap<String, Option>();
this.createOption("help") this.createOption("help")
.setDescription( .setDescription(
@ -212,6 +219,14 @@ public class CmdOptions {
this.optionChar = c; this.optionChar = c;
} }
public void setDontQuitOnError(boolean set) {
this.dontQuitOnError = set;
}
public void setShowOptions(boolean show) {
this.showOptions = show;
}
public void setHelpGeneration(boolean on) { public void setHelpGeneration(boolean on) {
if (!on) { if (!on) {
options.remove("help"); options.remove("help");
@ -233,6 +248,14 @@ public class CmdOptions {
return getOption(name); 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) { public String[] getOption(String name) {
if (options.get(name).values.size() > 0) if (options.get(name).values.size() > 0)
return options.get(name).values.toArray(new String[0]); return options.get(name).values.toArray(new String[0]);
@ -306,6 +329,16 @@ public class CmdOptions {
return this.getValuesAsList(option).contains(parameter); 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) { public String toString(boolean help) {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
if (help) { if (help) {
@ -320,7 +353,7 @@ public class CmdOptions {
} }
}); });
for (Option o : vars) { 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"); b.append("/options\n");
return b.toString(); return b.toString();
@ -336,20 +369,24 @@ public class CmdOptions {
return indices.toArray(new Integer[0]); return indices.toArray(new Integer[0]);
} }
private boolean optionExists(String option) { private Option getOptionByCommand(String cmd) {
for (Option o : this.options.values()) { for (Option o : this.options.values()) {
for (String s : o.cmd) { for (String s : o.cmd) {
if (option.equals(s)) { if (cmd.equals(s)) {
return true; return o;
} }
} }
for (String s : o.cmdLong) { for (String s : o.cmdLong) {
if (option.equals(s)) { if (cmd.equals(s)) {
return true; return o;
} }
} }
} }
return false; return null;
}
private boolean cmdExists(String cmd) {
return getOptionByCommand(cmd) != null;
} }
private boolean switchExists(char c) { private boolean switchExists(char c) {
@ -363,7 +400,21 @@ public class CmdOptions {
return false; return false;
} }
public void parse(String[] args) { private Option[] getOptionBySwitches(String switches) {
List<Option> o = new ArrayList<CmdOptions.Option>();
for (Option op : this.options.values()) {
for (String s : op.cmd) {
for (char c : switches.toCharArray()) {
if (s.toCharArray()[0] == c) {
o.add(op);
}
}
}
}
return o.toArray(new Option[0]);
}
public int parse(String[] args) {
int exit = 0; int exit = 0;
// get indices // get indices
Integer[] indices = getIndices(args); Integer[] indices = getIndices(args);
@ -371,11 +422,11 @@ public class CmdOptions {
boolean ok = true; boolean ok = true;
for (Integer i : indices) { for (Integer i : indices) {
String o = args[i].replace(optionChar, ""); String o = args[i].replace(optionChar, "");
if (!optionExists(o)) { if (!cmdExists(o)) {
if (this.combineSwitches) { if (this.combineSwitches) {
for (char c : o.toCharArray()) { for (char c : o.toCharArray()) {
if (!switchExists(c)) { if (!switchExists(c)) {
System.err.println("Unrecognized option '" + o System.err.println("Unrecognized switch '" + c
+ "'"); + "'");
ok = false; ok = false;
} }
@ -387,25 +438,36 @@ public class CmdOptions {
} }
} }
// quit if there are unknown options // quit if there are unknown options
if (!ok) { if (!ok && exit == 0) {
exit = 1; exit = 1;
} }
// now parse // now parse
Option op;
for (int a = 0; a < indices.length; ++a) { for (int a = 0; a < indices.length; ++a) {
String o = args[indices[a]].replace(optionChar, ""); String o = args[indices[a]].replace(optionChar, "");
op = this.getOptionByCommand(o);
if (op == null) {
if (this.combineSwitches) {
Option[] mop = getOptionBySwitches(o);
for (Option opt : mop) {
opt.setSet(true);
}
}
continue;
}
// the option is set! // the option is set!
this.getBareOption(o).setSet(true); op.setSet(true);
// are there parameters? // are there parameters?
if (indices[a] < args.length - 1 && a < indices.length - 1 if (indices[a] < args.length - 1 && a < indices.length - 1
&& indices[a + 1] - indices[a] > 1) { && indices[a + 1] - indices[a] > 1) {
// parameters between options // parameters between options
for (int b = indices[a] + 1; b < indices[a + 1]; ++b) { for (int b = indices[a] + 1; b < indices[a + 1]; ++b) {
this.getBareOption(o).getValues().add(args[b]); op.getValues().add(args[b]);
} }
} else if (a == indices.length - 1 && args.length - 1 > indices[a]) { } else if (a == indices.length - 1 && args.length - 1 > indices[a]) {
// parameters at the last option // parameters at the last option
for (int b = indices[a] + 1; b < args.length; ++b) { for (int b = indices[a] + 1; b < args.length; ++b) {
this.getBareOption(o).getValues().add(args[b]); op.getValues().add(args[b]);
} }
} }
} }
@ -413,22 +475,23 @@ public class CmdOptions {
// check for possible parameters // check for possible parameters
for (Option o : options.values()) { for (Option o : options.values()) {
for (String s : o.getValues()) { for (String s : o.getValues()) {
if (!o.possibleParams.contains(s)) { if (o.possibleParams.size() > 0
&& !o.possibleParams.contains(s)) {
System.err.println("Parameter \"" + s + "\" for Option \"" System.err.println("Parameter \"" + s + "\" for Option \""
+ o.name + "\" not allowed!"); + o.name + "\" not allowed!");
ok = false; ok = false;
} }
} }
} }
if (!ok) { if (!ok && exit == 0) {
exit = 2; exit = 2;
} }
// check parameter counts // check parameter counts
for (Option o : options.values()) { for (Option o : options.values()) {
if (o.getValues().size() < o.minParameters if (o.getValues().size() < o.minParameters && o.minParameters != 0
|| o.getValues().size() > o.maxParameters || o.getValues().size() > o.maxParameters
|| o.stepSizeParameters != 0 && o.maxParameters != 0 || o.stepSizeParameters != 0
&& o.getValues().size() % o.stepSizeParameters != 0) { && o.getValues().size() % o.stepSizeParameters != 0) {
System.err.println(o.name System.err.println(o.name
+ ": Parameter count not correct! Check help."); + ": Parameter count not correct! Check help.");
@ -449,10 +512,14 @@ public class CmdOptions {
if (!o.set && o.defaultParameter.size() != 0) if (!o.set && o.defaultParameter.size() != 0)
o.values.addAll(o.defaultParameter); o.values.addAll(o.defaultParameter);
} }
if (options.get("help").set) { if (options.get("help").set || exit > 0) {
System.out.println(this.toString(true)); System.out.println(this.toString(true));
if (!this.dontQuitOnError)
System.exit(exit); System.exit(exit);
} }
if (this.showOptions) {
System.out.println(this.toString(false)); System.out.println(this.toString(false));
} }
return exit;
}
} }