From 17c76cac7a59cfdf0b5244c080d59fb5953fb305 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Thu, 12 Dec 2013 20:08:52 +0100 Subject: [PATCH] inital commit --- CmdOptions.java | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 CmdOptions.java diff --git a/CmdOptions.java b/CmdOptions.java new file mode 100644 index 0000000..b3881cb --- /dev/null +++ b/CmdOptions.java @@ -0,0 +1,206 @@ +package at.mkw.inlocs.blinksetassembler.server; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; + +public class CmdOptions { + + private static CmdOptions instance; + + public static class Option { + public String name; + public String cmdOption; + public String longCmdOption; + public String description; + public String defaultParameter; + public String[] possibleParams; + public String[] alternatives; + public boolean set; + public ArrayList values; + + public Option() { + values = new ArrayList(); + } + + public String toString() { + String ret = name + " (" + longCmdOption + (cmdOption != null ? "/" + cmdOption : "") + ")" + + (defaultParameter != null ? ": default=" + defaultParameter : "") + (description != null ? "\n\t\t" + description : ""); + if (possibleParams != null) { + boolean start = true; + ret += "\n\t\t(Possible parameters: "; + for (String s : possibleParams) { + ret += (start ? "" : ", ") + s; + start = false; + } + ret += ")"; + } + if (set) { + ret += "\n\t\t--> current Setting: "; + if (values.size() > 0) { + boolean start = true; + for (String s : values) { + ret += (start ? "" : ",") + s; + start = false; + } + } else { + ret += "true"; + } + // ret += "\n"; + } + return ret; + } + } + + private HashMap options; + + private CmdOptions() { + options = new HashMap(); + this.setOption("help", "--help", "-h", null, "Show all possible options and their parameters.", null, new String[] { "-?" }); + } + + public static CmdOptions i() { + if (instance == null) { + instance = new CmdOptions(); + } + return instance; + } + + public String[] getOption(String name) { + if (options.get(name).values.size() > 0) + return options.get(name).values.toArray(new String[0]); + else if (options.get(name).defaultParameter != null) + return new String[] { options.get(name).defaultParameter }; + return null; + } + + public Integer[] getOptionAsInt(String name) { + if (options.get(name).values.size() > 0) { + ArrayList list = new ArrayList(); + for (String o : options.get(name).values) { + list.add(Integer.parseInt(o)); + } + return list.toArray(new Integer[0]); + } else if (options.get(name).defaultParameter != null) + return new Integer[] { Integer.parseInt(options.get(name).defaultParameter) }; + return null; + } + + public Double[] getOptionAsDouble(String name) { + if (options.get(name).values.size() > 0) { + ArrayList list = new ArrayList(); + for (String o : options.get(name).values) { + list.add(Double.parseDouble(o)); + } + return list.toArray(new Double[0]); + } else if (options.get(name).defaultParameter != null) + return new Double[] { Double.parseDouble(options.get(name).defaultParameter) }; + return null; + } + + public boolean isSet(String option) { + return options.get(option).set; + } + + public boolean isSet(String option, String parameter) { + for (String o : options.get(option).values) { + if (o.equals(parameter)) { + return true; + } + } + 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) { + StringBuilder b = new StringBuilder(); + if (help) { + b.append("Possible options:\n"); + } + b.append("-options\n"); + Option[] vars = options.values().toArray(new Option[0]); + Arrays.sort(vars, new Comparator