refactored java version (untested...)

This commit is contained in:
Marcel M. Otte 2013-12-31 16:53:25 +00:00
parent 4815d6449b
commit 2a966fb9c1
2 changed files with 295 additions and 208 deletions

View File

@ -6,35 +6,97 @@ import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
/** /**
* Written by Marcel M. Otte, (c) 2013 * Written by Marcel M. Otte, (c) 2013 For use under the BSD 2-clause License,
* For use under the BSD 2-clause License, or in other words: * or in other words: Do what you want with it as long as you leave all
* Do what you want with it as long as you leave all copyright notices where they are * copyright notices where they are and don't bother me when you break your pc.
* and don't bother me when you break your pc. :) * :)
*/ */
public class CmdOptions { public class CmdOptions {
private static CmdOptions instance; private static CmdOptions instance;
private static class Option { public static class Option {
public String name; private String name;
public String cmdOption; private ArrayList<String> cmd;
public String longCmdOption; private String description;
public String description; private ArrayList<String> defaultParameter;
public String defaultParameter;
public String[] possibleParams; private ArrayList<String> possibleParams;
public String[] alternatives; private boolean set;
public boolean set; private ArrayList<String> values;
public ArrayList<String> values;
public ArrayList<String> getValues() {
return values;
}
public ArrayList<String> getDefaultParameter() {
return defaultParameter;
}
public Option() { public Option() {
values = new ArrayList<String>(); values = new ArrayList<String>();
cmd = new ArrayList<String>();
defaultParameter = new ArrayList<String>();
possibleParams = new ArrayList<String>();
}
public Option addCommand(String cmd) {
this.cmd.add(cmd);
return this;
}
public Option addDefaultParameter(String d) {
this.defaultParameter.add(d);
return this;
}
public Option addPossibleParameter(String p) {
this.possibleParams.add(p);
return this;
}
public Option addValue(String value) {
this.values.add(value);
return this;
}
public String getName() {
return name;
}
public Option setName(String name) {
this.name = name;
return this;
}
public String getDescription() {
return description;
}
public Option setDescription(String description) {
this.description = description;
return this;
}
public boolean isSet() {
return set;
}
public Option setSet(boolean set) {
this.set = set;
return this;
} }
public String toString() { public String toString() {
String ret = name + " (" + longCmdOption + (cmdOption != null ? "/" + cmdOption : "") + ")" String ret = name + " (";
+ (defaultParameter != null ? ": default=" + defaultParameter : "") + (description != null ? "\n\t\t" + description : ""); for (String s : cmd) {
ret += s + ", ";
}
ret += ")"
+ (defaultParameter != null ? ": default="
+ defaultParameter : "")
+ (description != null ? "\n\t\t" + description : "");
if (possibleParams != null) { if (possibleParams != null) {
boolean start = true; boolean start = true;
ret += "\n\t\t(Possible parameters: "; ret += "\n\t\t(Possible parameters: ";
@ -65,7 +127,10 @@ public class CmdOptions {
private CmdOptions() { private CmdOptions() {
options = new HashMap<String, Option>(); options = new HashMap<String, Option>();
this.setOption("help", "--help", "-h", null, "Show all possible options and their parameters.", null, new String[] { "-?" }); this.createOption("help")
.setDescription(
"Show all possible options and their parameters.")
.addCommand("--help").addCommand("-h").addCommand("-?");
} }
public static CmdOptions i() { public static CmdOptions i() {
@ -75,11 +140,18 @@ public class CmdOptions {
return instance; return instance;
} }
public Option createOption(String name) {
Option o = new Option();
o.setName(name);
this.options.put(name, o);
return o;
}
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]);
else if (options.get(name).defaultParameter != null) else if (options.get(name).defaultParameter != null)
return new String[] { options.get(name).defaultParameter }; return options.get(name).getValues().toArray(new String[0]);
return null; return null;
} }
@ -90,8 +162,13 @@ public class CmdOptions {
list.add(Integer.parseInt(o)); list.add(Integer.parseInt(o));
} }
return list.toArray(new Integer[0]); return list.toArray(new Integer[0]);
} else if (options.get(name).defaultParameter != null) } else if (options.get(name).getDefaultParameter().size() > 0) {
return new Integer[] { Integer.parseInt(options.get(name).defaultParameter) }; ArrayList<Integer> list = new ArrayList<Integer>();
for (String o : options.get(name).getDefaultParameter()) {
list.add(Integer.parseInt(o));
}
return list.toArray(new Integer[0]);
}
return null; return null;
} }
@ -102,8 +179,13 @@ public class CmdOptions {
list.add(Double.parseDouble(o)); list.add(Double.parseDouble(o));
} }
return list.toArray(new Double[0]); return list.toArray(new Double[0]);
} else if (options.get(name).defaultParameter != null) } else if (options.get(name).getDefaultParameter().size() > 0) {
return new Double[] { Double.parseDouble(options.get(name).defaultParameter) }; ArrayList<Double> list = new ArrayList<Double>();
for (String o : options.get(name).getDefaultParameter()) {
list.add(Double.parseDouble(o));
}
return list.toArray(new Double[0]);
}
return null; return null;
} }
@ -120,22 +202,6 @@ public class CmdOptions {
return false; return false;
} }
public void setOption(String name, String longcmd, String cmd, String defaultparam, String description) {
this.setOption(name, longcmd, cmd, defaultparam, description, null, null);
}
public void setOption(String name, String longcmd, String cmd, String defaultparam, String description, String[] possibleParams, String[] alternatives) {
Option o = new Option();
o.cmdOption = cmd;
o.name = name;
o.longCmdOption = longcmd;
o.defaultParameter = defaultparam;
o.description = description;
o.alternatives = alternatives;
o.possibleParams = possibleParams;
options.put(name, o);
}
public String toString(boolean help) { public String toString(boolean help) {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
if (help) { if (help) {
@ -167,8 +233,8 @@ public class CmdOptions {
Option o = null; Option o = null;
// search for correct option // search for correct option
for (Option op : options.values()) { for (Option op : options.values()) {
if (arg.equals(op.longCmdOption) || op.cmdOption != null && arg.equals(op.cmdOption) || op.alternatives != null for (String s : op.cmd)
&& Arrays.asList(op.alternatives).contains(arg)) { if (arg.equals(s)) {
o = op; o = op;
break; break;
} }
@ -182,11 +248,13 @@ public class CmdOptions {
// now iterate through the parameters // now iterate through the parameters
int j = i + 1; int j = i + 1;
while (args.length > j && !args[j].startsWith("-")) { while (args.length > j && !args[j].startsWith("-")) {
if (o.possibleParams != null) { if (o.possibleParams.size() > 0) {
if (Arrays.asList(o.possibleParams).contains(args[j])) if (o.possibleParams.contains(args[j]))
o.values.add(args[j]); o.values.add(args[j]);
else else
System.err.println("Parameter \"" + args[j] + "\" for Option \"" + o.name + "\" not allowed!"); System.err.println("Parameter \"" + args[j]
+ "\" for Option \"" + o.name
+ "\" not allowed!");
} else { } else {
o.values.add(args[j]); o.values.add(args[j]);
} }
@ -202,12 +270,17 @@ 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 && o.defaultParameter.equals("")) { if (!o.set && o.defaultParameter != null
System.err.println(o.name + " (" + o.cmdOption + "): has no default parameter and has to be set on commandline!"); && o.defaultParameter.equals("")) {
System.err
.println(o.name
+ " ("
+ o.getName()
+ "): has no default parameter and has to be set on commandline!");
System.exit(1); System.exit(1);
} }
if (!o.set && o.defaultParameter != null) if (!o.set && o.defaultParameter != null)
o.values.add(o.defaultParameter); o.values.addAll(o.defaultParameter);
} }
System.out.println(this.toString(false)); System.out.println(this.toString(false));
} }

View File

@ -5,38 +5,52 @@ import to.mmo.cmdlineoptions.CmdOptions;
public class Example { public class Example {
public static void initOptions() { public static void initOptions() {
CmdOptions.i().setOption("name", // give the option a name which you can CmdOptions.i().createOption("name").// give the option a name which you
// can
// remember within your project // remember within your project
"--name", // it's always good to define a long commandline addCommand("--name") // it's always good to define a long
// parameter with a describing name // commandline parameter with a
"-n", // of course, programmer are always lazy so give them an // describing name
// possibility to shortcut your option
"whatever", // a default parameter is good if you need the .addCommand("-n") // of course, programmer are always lazy so
// option within your program, but don't care when // give them an possibility to shortcut your
// it's omitted on execution // option
"an example option"); // And of course a description... for the .addDefaultParameter("whatever")// a default parameter is good
// help readers, the few among us... // if you need the option within
CmdOptions.i().setOption( // your program, but don't care
"complex", // see above // when it's omitted on
"--complex", "-c", // execution
null, // yes you can omit a default parameter, but you have to .setDescription("an example option.");// And of course a
// description... for
// the help readers, the
// few among us...
CmdOptions
.i()
.createOption("name")
.addCommand("--complex")
// see above
// yes you can omit a default parameter, but you have to
// check later in your project if that option is set. // check later in your project if that option is set.
"a complex commandline parameter", .addCommand("-c")
new String[] { "something", "another_something" }, .setDescription("a complex commandline parameter")
.addPossibleParameter("something")
// here the fun begins, you can set as much possible parameters // here the fun begins, you can set as much possible parameters
// as you want, keep in mind to leave out whitespaces! // as you want, keep in mind to leave out whitespaces!
// and remember that you limit your option to exactly that // and remember that you limit your option to exactly that
// parameters! // parameters!
new String[] { "-i", "--ccp" }); .addPossibleParameter("another_something")
// here you are able to add some more alternatives for that option, so // here you are able to add some more alternatives for that
// here you can use "--complex", "-c", "-i" and "--ccp" to trigger this // option, so here you can use "--complex", "-c", "-i" and
// option. // "--ccp" to trigger this option.
.addCommand("-i").addCommand("--ccp");
} }
public static void main(String[] args) { public static void main(String[] args) {
initOptions(); // init the commandline options initOptions(); // init the commandline options
CmdOptions.i().parse(args); // parse them, and your done. --help is automatically set, so you can use it right away. CmdOptions.i().parse(args); // parse them, and your done. --help is
// automatically set, so you can use it
// right away.
// now your program follows, using this options... // now your program follows, using this options...