adding a bunch of methos for faster access and more customization

This commit is contained in:
Marcel Otte 2014-12-09 19:51:32 +01:00
parent 81c80c3750
commit 57c46e2c03
1 changed files with 66 additions and 6 deletions

View File

@ -17,15 +17,22 @@ public class CmdOptions {
private static CmdOptions instance; private static CmdOptions instance;
private static String optionChar;
private boolean combineSwitches;
public static class Option { public static class Option {
private String name; private String name;
private ArrayList<String> cmd; private ArrayList<String> cmd;
private ArrayList<String> cmdLong;
private String description; private String description;
private ArrayList<String> defaultParameter; private ArrayList<String> defaultParameter;
private ArrayList<String> possibleParams; private ArrayList<String> possibleParams;
private boolean set; private boolean set;
private ArrayList<String> values; private ArrayList<String> values;
private int maxParameters, minParameters;
private ArrayList<String> examples;
public ArrayList<String> getValues() { public ArrayList<String> getValues() {
return values; return values;
@ -38,15 +45,26 @@ public class CmdOptions {
public Option() { public Option() {
values = new ArrayList<String>(); values = new ArrayList<String>();
cmd = new ArrayList<String>(); cmd = new ArrayList<String>();
cmdLong = new ArrayList<String>();
defaultParameter = new ArrayList<String>(); defaultParameter = new ArrayList<String>();
possibleParams = new ArrayList<String>(); possibleParams = new ArrayList<String>();
examples = new ArrayList<String>();
} }
public Option addCommand(String cmd) { public Option addCommand(String cmd) {
if (cmd.contains(optionChar))
cmd.replaceAll(optionChar, "");
this.cmd.add(cmd); this.cmd.add(cmd);
return this; return this;
} }
public Option addLongCommand(String cmd) {
if (cmd.contains(optionChar))
cmd.replaceAll(optionChar, "");
this.cmdLong.add(cmd);
return this;
}
public Option addDefaultParameter(String d) { public Option addDefaultParameter(String d) {
this.defaultParameter.add(d); this.defaultParameter.add(d);
return this; return this;
@ -71,6 +89,12 @@ public class CmdOptions {
return this; return this;
} }
public Option setParameterCount(int min, int max) {
this.minParameters = min;
this.maxParameters = max;
return this;
}
public String getDescription() { public String getDescription() {
return description; return description;
} }
@ -97,6 +121,11 @@ public class CmdOptions {
return this; return this;
} }
public Option addExample(String example) {
this.examples.add(example);
return this;
}
public String toString() { public String toString() {
String ret = name + " ("; String ret = name + " (";
for (String s : cmd) { for (String s : cmd) {
@ -135,6 +164,8 @@ public class CmdOptions {
private HashMap<String, Option> options; private HashMap<String, Option> options;
private CmdOptions() { private CmdOptions() {
optionChar = "-";
this.setSwitchCombination(false);
options = new HashMap<String, Option>(); options = new HashMap<String, Option>();
this.createOption("help") this.createOption("help")
.setDescription( .setDescription(
@ -149,6 +180,20 @@ public class CmdOptions {
return instance; return instance;
} }
public void setSwitchCombination(boolean on) {
this.combineSwitches = on;
}
public void setOptionCharacter(String c) {
this.optionChar = c;
}
public void setHelpGeneration(boolean on) {
if (!on) {
options.remove("help");
}
}
public Option createOption(String name) { public Option createOption(String name) {
Option o = new Option(); Option o = new Option();
o.setName(name); o.setName(name);
@ -156,6 +201,10 @@ public class CmdOptions {
return o; return o;
} }
public Option getBareOption(String name) {
return options.get(name);
}
public String[] get(String name) { public String[] get(String name) {
return getOption(name); return getOption(name);
} }
@ -192,6 +241,14 @@ public class CmdOptions {
return null; return null;
} }
public Integer getOptionAsInt(String name, int index) {
Integer[] array = getOptionAsInt(name);
if (index >= 0 && index < array.length) {
return array[index];
}
return null;
}
public Double[] getOptionAsDouble(String name) { public Double[] getOptionAsDouble(String name) {
if (options.get(name).values.size() > 0) { if (options.get(name).values.size() > 0) {
ArrayList<Double> list = new ArrayList<Double>(); ArrayList<Double> list = new ArrayList<Double>();
@ -209,17 +266,20 @@ public class CmdOptions {
return null; return null;
} }
public Double getOptionAsDouble(String name, int index) {
Double[] array = getOptionAsDouble(name);
if (index >= 0 && index < array.length) {
return array[index];
}
return null;
}
public boolean isSet(String option) { public boolean isSet(String option) {
return options.get(option).set; return options.get(option).set;
} }
public boolean isSet(String option, String parameter) { public boolean isSet(String option, String parameter) {
for (String o : options.get(option).values) { return this.getValuesAsList(option).contains(parameter);
if (o.equals(parameter)) {
return true;
}
}
return false;
} }
public String toString(boolean help) { public String toString(boolean help) {