From 2a966fb9c130b68a47c1a7d2d49c6b2bc409a420 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Tue, 31 Dec 2013 16:53:25 +0000 Subject: [PATCH] refactored java version (untested...) --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 443 ++++++++++-------- .../mmo/cmdlineoptions/examples/Example.java | 60 ++- 2 files changed, 295 insertions(+), 208 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 3021ac8..23ba8e1 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -6,209 +6,282 @@ import java.util.Comparator; import java.util.HashMap; /** - * Written by Marcel M. Otte, (c) 2013 - * For use under the BSD 2-clause License, or in other words: - * Do what you want with it as long as you leave all copyright notices where they are - * and don't bother me when you break your pc. :) + * Written by Marcel M. Otte, (c) 2013 For use under the BSD 2-clause License, + * or in other words: Do what you want with it as long as you leave all + * copyright notices where they are and don't bother me when you break your pc. + * :) */ - public class CmdOptions { - private static CmdOptions instance; + private static CmdOptions instance; - private 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 static class Option { + private String name; + private ArrayList cmd; + private String description; + private ArrayList defaultParameter; - public Option() { - values = new ArrayList(); - } + private ArrayList possibleParams; + private boolean set; + private ArrayList values; - 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; - } - } + public ArrayList getValues() { + return values; + } - private HashMap options; + public ArrayList getDefaultParameter() { + return defaultParameter; + } - private CmdOptions() { - options = new HashMap(); - this.setOption("help", "--help", "-h", null, "Show all possible options and their parameters.", null, new String[] { "-?" }); - } + public Option() { + values = new ArrayList(); + cmd = new ArrayList(); + defaultParameter = new ArrayList(); + possibleParams = new ArrayList(); + } - public static CmdOptions i() { - if (instance == null) { - instance = new CmdOptions(); - } - return instance; - } + public Option addCommand(String cmd) { + this.cmd.add(cmd); + return this; + } - 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 Option addDefaultParameter(String d) { + this.defaultParameter.add(d); + return this; + } - 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 Option addPossibleParameter(String p) { + this.possibleParams.add(p); + return this; + } - 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 Option addValue(String value) { + this.values.add(value); + return this; + } - public boolean isSet(String option) { - return options.get(option).set; - } + public String getName() { + return name; + } - public boolean isSet(String option, String parameter) { - for (String o : options.get(option).values) { - if (o.equals(parameter)) { - return true; - } - } - return false; - } + public Option setName(String name) { + this.name = name; + return this; + } - public void setOption(String name, String longcmd, String cmd, String defaultparam, String description) { - this.setOption(name, longcmd, cmd, defaultparam, description, null, null); - } + public String getDescription() { + return description; + } - 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 Option setDescription(String description) { + this.description = description; + return this; + } - 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