min max step parameter checks added

This commit is contained in:
Marcel Otte 2014-12-09 20:41:49 +01:00
parent 19af70a03b
commit c6daefd033
1 changed files with 54 additions and 10 deletions

View File

@ -34,6 +34,7 @@ public class CmdOptions {
private ArrayList<String> values; private ArrayList<String> values;
private int maxParameters, minParameters; private int maxParameters, minParameters;
private ArrayList<String> examples; private ArrayList<String> examples;
private int stepSizeParameters;
public ArrayList<String> getValues() { public ArrayList<String> getValues() {
return values; return values;
@ -53,8 +54,13 @@ public class CmdOptions {
} }
public Option addCommand(String cmd) { public Option addCommand(String cmd) {
if (cmd.contains(optionChar)) if (cmd.contains(optionChar)) {
cmd.replaceAll(optionChar, ""); cmd = cmd.replaceAll(optionChar, "");
}
if (cmd.length() > 1) {
throw new IllegalArgumentException(
"Command longer than 1 character, which is not allowed. Use 'addLongCommand()' instead!");
}
this.cmd.add(cmd); this.cmd.add(cmd);
return this; return this;
} }
@ -91,8 +97,13 @@ public class CmdOptions {
} }
public Option setParameterCount(int min, int max) { 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.minParameters = min;
this.maxParameters = max; this.maxParameters = max;
this.stepSizeParameters = step;
return this; return this;
} }
@ -334,7 +345,19 @@ public class CmdOptions {
return false; return false;
} }
private boolean switchExists(char c) {
for (Option op : this.options.values()) {
for (String s : op.cmd) {
if (s.toCharArray()[0] == c) {
return true;
}
}
}
return false;
}
public void parse(String[] args) { public void parse(String[] args) {
int exit = 0;
// get indices // get indices
Integer[] indices = getIndices(args); Integer[] indices = getIndices(args);
// check for correct options // check for correct options
@ -342,13 +365,23 @@ public class CmdOptions {
for (Integer i : indices) { for (Integer i : indices) {
String o = args[i].replaceAll(optionChar, ""); String o = args[i].replaceAll(optionChar, "");
if (!optionExists(o)) { if (!optionExists(o)) {
if (this.combineSwitches) {
for (char c : o.toCharArray()) {
if (!switchExists(c)) {
System.err.println("Unrecognized option '" + o
+ "'");
ok = false;
}
}
} else {
System.err.println("Unrecognized option '" + o + "'"); System.err.println("Unrecognized option '" + o + "'");
ok = false; ok = false;
} }
} }
}
// quit if there are unknown options // quit if there are unknown options
if (!ok) { if (!ok) {
System.exit(1); exit = 1;
} }
// now parse // now parse
for (int a = 0; a < indices.length; ++a) { for (int a = 0; a < indices.length; ++a) {
@ -381,12 +414,19 @@ public class CmdOptions {
} }
} }
if (!ok) { if (!ok) {
System.exit(2); exit = 2;
} }
if (options.get("help").set) { // check parameter counts
System.out.println(this.toString(true)); for (Option o : options.values()) {
System.exit(0); if (o.getValues().size() < o.minParameters
|| o.getValues().size() > o.maxParameters
|| o.stepSizeParameters != 0
&& o.getValues().size() % o.stepSizeParameters != 0) {
System.err.println(o.name
+ ": Parameter count not correct! Check help.");
exit = 3;
}
} }
// set default for that options that aren't set // set default for that options that aren't set
@ -397,11 +437,15 @@ public class CmdOptions {
+ " (" + " ("
+ o.getName() + o.getName()
+ "): has no default parameter and has to be set on commandline!"); + "): has no default parameter and has to be set on commandline!");
System.exit(3); exit = 4;
} }
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) {
System.out.println(this.toString(true));
System.exit(exit);
}
System.out.println(this.toString(false)); System.out.println(this.toString(false));
} }
} }