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();
} }
public void parse(String[] args) { private Integer[] getIndices(String[] args) {
// now parse List<Integer> indices = new ArrayList<Integer>();
if (args.length > 0) { for (int i = 0; i < args.length; ++i) {
int i = 0; if (args[i].startsWith(optionChar)) {
String arg = null; indices.add(i);
// iterate through all options
for (; i < args.length; ++i) {
arg = args[i];
Option o = null;
// search for correct option
for (Option op : options.values()) {
for (String s : op.cmd)
if (arg.equals(s)) {
o = op;
break;
}
}
// if it is unknown
if (o == null) {
System.err.println("Unrecognized option '" + arg + "'");
continue;
}
o.set = true;
// now iterate through the parameters
int j = i + 1;
while (args.length > j && !args[j].startsWith("-")) {
if (o.possibleParams.size() > 0) {
if (o.possibleParams.contains(args[j]))
o.values.add(args[j]);
else
System.err.println("Parameter \"" + args[j]
+ "\" for Option \"" + o.name
+ "\" not allowed!");
} else {
o.values.add(args[j]);
}
++j;
}
i = j - 1;
} }
} }
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) {
// 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
for (int a = 0; a < indices.length; ++a) {
String o = args[indices[a]].replaceAll(optionChar, "");
// the option is set!
this.getBareOption(o).setSet(true);
// are there parameters?
if (indices[a] < args.length - 1 && a < indices.length - 1
&& indices[a + 1] - indices[a] > 1) {
// parameters between options
for (int b = indices[a] + 1; b < indices[a + 1]; ++b) {
this.getBareOption(o).getValues().add(args[b]);
}
} else if (a == indices.length - 1 && args.length - 1 > indices[a]) {
// parameters at the last option
for (int b = indices[a] + 1; b < args.length; ++b) {
this.getBareOption(o).getValues().add(args[b]);
}
}
}
// check for possible parameters
for (Option o : options.values()) {
for (String s : o.getValues()) {
if (!o.possibleParams.contains(s)) {
System.err.println("Parameter \"" + s + "\" for Option \""
+ o.name + "\" not allowed!");
ok = false;
}
}
}
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));