reworking parsing

This commit is contained in:
Marcel Otte 2014-12-09 20:24:01 +01:00
parent 57c46e2c03
commit 19af70a03b
1 changed files with 83 additions and 43 deletions

View File

@ -30,6 +30,7 @@ public class CmdOptions {
private ArrayList<String> possibleParams; private ArrayList<String> possibleParams;
private boolean set; private boolean set;
private boolean required;
private ArrayList<String> values; private ArrayList<String> values;
private int maxParameters, minParameters; private int maxParameters, minParameters;
private ArrayList<String> examples; private ArrayList<String> examples;
@ -104,6 +105,11 @@ public class CmdOptions {
return this; return this;
} }
public Option setRequired(boolean required) {
this.required = true;
return this;
}
public boolean isSet() { public boolean isSet() {
return set; return set;
} }
@ -302,47 +308,82 @@ public class CmdOptions {
return b.toString(); return b.toString();
} }
private Integer[] getIndices(String[] args) {
List<Integer> indices = new ArrayList<Integer>();
for (int i = 0; i < args.length; ++i) {
if (args[i].startsWith(optionChar)) {
indices.add(i);
}
}
return indices.toArray(new Integer[0]);
}
private boolean optionExists(String option) {
for (Option o : this.options.values()) {
for (String s : o.cmd) {
if (option.equals(s)) {
return true;
}
}
for (String s : o.cmdLong) {
if (option.equals(s)) {
return true;
}
}
}
return false;
}
public void parse(String[] args) { public void parse(String[] args) {
// get indices
Integer[] indices = getIndices(args);
// check for correct options
boolean ok = true;
for (Integer i : indices) {
String o = args[i].replaceAll(optionChar, "");
if (!optionExists(o)) {
System.err.println("Unrecognized option '" + o + "'");
ok = false;
}
}
// quit if there are unknown options
if (!ok) {
System.exit(1);
}
// now parse // now parse
if (args.length > 0) { for (int a = 0; a < indices.length; ++a) {
int i = 0; String o = args[indices[a]].replaceAll(optionChar, "");
String arg = null; // the option is set!
// iterate through all options this.getBareOption(o).setSet(true);
for (; i < args.length; ++i) { // are there parameters?
arg = args[i]; if (indices[a] < args.length - 1 && a < indices.length - 1
Option o = null; && indices[a + 1] - indices[a] > 1) {
// search for correct option // parameters between options
for (Option op : options.values()) { for (int b = indices[a] + 1; b < indices[a + 1]; ++b) {
for (String s : op.cmd) this.getBareOption(o).getValues().add(args[b]);
if (arg.equals(s)) { }
o = op; } else if (a == indices.length - 1 && args.length - 1 > indices[a]) {
break; // parameters at the last option
for (int b = indices[a] + 1; b < args.length; ++b) {
this.getBareOption(o).getValues().add(args[b]);
} }
} }
// if it is unknown
if (o == null) {
System.err.println("Unrecognized option '" + arg + "'");
continue;
} }
o.set = true;
// now iterate through the parameters // check for possible parameters
int j = i + 1; for (Option o : options.values()) {
while (args.length > j && !args[j].startsWith("-")) { for (String s : o.getValues()) {
if (o.possibleParams.size() > 0) { if (!o.possibleParams.contains(s)) {
if (o.possibleParams.contains(args[j])) System.err.println("Parameter \"" + s + "\" for Option \""
o.values.add(args[j]); + o.name + "\" not allowed!");
else ok = false;
System.err.println("Parameter \"" + args[j]
+ "\" for Option \"" + o.name
+ "\" not allowed!");
} else {
o.values.add(args[j]);
}
++j;
}
i = j - 1;
} }
} }
}
if (!ok) {
System.exit(2);
}
if (options.get("help").set) { if (options.get("help").set) {
System.out.println(this.toString(true)); System.out.println(this.toString(true));
System.exit(0); System.exit(0);
@ -350,16 +391,15 @@ public class CmdOptions {
// set default for that options that aren't set // set default for that options that aren't set
for (Option o : options.values()) { for (Option o : options.values()) {
if (!o.set && o.defaultParameter != null if (!o.set && o.required) {
&& o.defaultParameter.equals("")) {
System.err System.err
.println(o.name .println(o.name
+ " (" + " ("
+ 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(1); System.exit(3);
} }
if (!o.set && o.defaultParameter != null) if (!o.set && o.defaultParameter.size() != 0)
o.values.addAll(o.defaultParameter); o.values.addAll(o.defaultParameter);
} }
System.out.println(this.toString(false)); System.out.println(this.toString(false));