From 5177c14624f7450f29e85dce9bc4f7f7e9dcd0de Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 19:25:44 +0100 Subject: [PATCH 01/14] readme update --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e59a1c3..dc96e5e 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ Why not using a small and easy library, which does exactly THAT task? Which give This is the goal of this project. +The second goals of this project are a personal ones: +- to have fun programming similiar behaving code in different languages +- learning new programming languages through adding them to this repository + + cmdlineoptions =============== @@ -34,12 +39,14 @@ Current work and future plans =============== Working on: -- JUnit tests +- ~~JUnit tests~~ - Java: better errorhandling -- Java: Examples -- C version [done] +- ~~Java: Examples~~ +- ~~C version~~ - Getting started +- Some java improvements Future plans: - C++ class version + - haskell - ... From 469a5009a890b1925c535873a3ee5b48f24c055c Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 19:32:29 +0100 Subject: [PATCH 02/14] added contains() and indexOf() for values for faster access --- java/src/to/mmo/cmdlineoptions/CmdOptions.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 23ba8e1..3d73bb2 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -83,6 +83,14 @@ public class CmdOptions { return set; } + public boolean valuesContains(String value) { + return values.contains(value); + } + + public int getIndexOf(String value) { + return values.indexOf(value); + } + public Option setSet(boolean set) { this.set = set; return this; From 6e0e21beca3af84520db8bf5d23a5b26d6490c0b Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 19:33:50 +0100 Subject: [PATCH 03/14] added shorter 'get()' method for Option values --- java/src/to/mmo/cmdlineoptions/CmdOptions.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 3d73bb2..c27755e 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -155,6 +155,10 @@ public class CmdOptions { return o; } + public String[] get(String name) { + return getOption(name); + } + public String[] getOption(String name) { if (options.get(name).values.size() > 0) return options.get(name).values.toArray(new String[0]); From 81c80c37500706400e3af32cb375c3781df8249c Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 19:36:05 +0100 Subject: [PATCH 04/14] retrieve values as list --- java/src/to/mmo/cmdlineoptions/CmdOptions.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index c27755e..046f1a0 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; +import java.util.List; /** * Written by Marcel M. Otte, (c) 2013 For use under the BSD 2-clause License, @@ -167,6 +168,13 @@ public class CmdOptions { return null; } + public List getValuesAsList(String name) { + if (options.get(name).getValues().size() > 0) { + return options.get(name).getValues(); + } + return null; + } + public Integer[] getOptionAsInt(String name) { if (options.get(name).values.size() > 0) { ArrayList list = new ArrayList(); From 57c46e2c03e43db1807d54cda303e0dc7bf491ea Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 19:51:32 +0100 Subject: [PATCH 05/14] adding a bunch of methos for faster access and more customization --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 72 +++++++++++++++++-- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 046f1a0..1fa46cc 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -17,15 +17,22 @@ public class CmdOptions { private static CmdOptions instance; + private static String optionChar; + + private boolean combineSwitches; + public static class Option { private String name; private ArrayList cmd; + private ArrayList cmdLong; private String description; private ArrayList defaultParameter; private ArrayList possibleParams; private boolean set; private ArrayList values; + private int maxParameters, minParameters; + private ArrayList examples; public ArrayList getValues() { return values; @@ -38,15 +45,26 @@ public class CmdOptions { public Option() { values = new ArrayList(); cmd = new ArrayList(); + cmdLong = new ArrayList(); defaultParameter = new ArrayList(); possibleParams = new ArrayList(); + examples = new ArrayList(); } public Option addCommand(String cmd) { + if (cmd.contains(optionChar)) + cmd.replaceAll(optionChar, ""); this.cmd.add(cmd); return this; } + public Option addLongCommand(String cmd) { + if (cmd.contains(optionChar)) + cmd.replaceAll(optionChar, ""); + this.cmdLong.add(cmd); + return this; + } + public Option addDefaultParameter(String d) { this.defaultParameter.add(d); return this; @@ -71,6 +89,12 @@ public class CmdOptions { return this; } + public Option setParameterCount(int min, int max) { + this.minParameters = min; + this.maxParameters = max; + return this; + } + public String getDescription() { return description; } @@ -97,6 +121,11 @@ public class CmdOptions { return this; } + public Option addExample(String example) { + this.examples.add(example); + return this; + } + public String toString() { String ret = name + " ("; for (String s : cmd) { @@ -135,6 +164,8 @@ public class CmdOptions { private HashMap options; private CmdOptions() { + optionChar = "-"; + this.setSwitchCombination(false); options = new HashMap(); this.createOption("help") .setDescription( @@ -149,6 +180,20 @@ public class CmdOptions { return instance; } + public void setSwitchCombination(boolean on) { + this.combineSwitches = on; + } + + public void setOptionCharacter(String c) { + this.optionChar = c; + } + + public void setHelpGeneration(boolean on) { + if (!on) { + options.remove("help"); + } + } + public Option createOption(String name) { Option o = new Option(); o.setName(name); @@ -156,6 +201,10 @@ public class CmdOptions { return o; } + public Option getBareOption(String name) { + return options.get(name); + } + public String[] get(String name) { return getOption(name); } @@ -192,6 +241,14 @@ public class CmdOptions { return null; } + public Integer getOptionAsInt(String name, int index) { + Integer[] array = getOptionAsInt(name); + if (index >= 0 && index < array.length) { + return array[index]; + } + return null; + } + public Double[] getOptionAsDouble(String name) { if (options.get(name).values.size() > 0) { ArrayList list = new ArrayList(); @@ -209,17 +266,20 @@ public class CmdOptions { return null; } + public Double getOptionAsDouble(String name, int index) { + Double[] array = getOptionAsDouble(name); + if (index >= 0 && index < array.length) { + return array[index]; + } + 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; + return this.getValuesAsList(option).contains(parameter); } public String toString(boolean help) { From 19af70a03b0721e91861e3068dd6c4a4177ab9b9 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 20:24:01 +0100 Subject: [PATCH 06/14] reworking parsing --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 126 ++++++++++++------ 1 file changed, 83 insertions(+), 43 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 1fa46cc..79e2913 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -30,6 +30,7 @@ public class CmdOptions { private ArrayList possibleParams; private boolean set; + private boolean required; private ArrayList values; private int maxParameters, minParameters; private ArrayList examples; @@ -104,6 +105,11 @@ public class CmdOptions { return this; } + public Option setRequired(boolean required) { + this.required = true; + return this; + } + public boolean isSet() { return set; } @@ -302,47 +308,82 @@ public class CmdOptions { return b.toString(); } - public void parse(String[] args) { - // now parse - if (args.length > 0) { - int i = 0; - String arg = null; - // 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; + private Integer[] getIndices(String[] args) { + List indices = new ArrayList(); + 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) { + // 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) { System.out.println(this.toString(true)); System.exit(0); @@ -350,16 +391,15 @@ public class CmdOptions { // set default for that options that aren't set for (Option o : options.values()) { - if (!o.set && o.defaultParameter != null - && o.defaultParameter.equals("")) { + if (!o.set && o.required) { System.err .println(o.name + " (" + o.getName() + "): 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); } System.out.println(this.toString(false)); From c6daefd033109a9425e8f6f36ee17999c7667a5c Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 20:41:49 +0100 Subject: [PATCH 07/14] min max step parameter checks added --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 64 ++++++++++++++++--- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 79e2913..5e8ffce 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -34,6 +34,7 @@ public class CmdOptions { private ArrayList values; private int maxParameters, minParameters; private ArrayList examples; + private int stepSizeParameters; public ArrayList getValues() { return values; @@ -53,8 +54,13 @@ public class CmdOptions { } public Option addCommand(String cmd) { - if (cmd.contains(optionChar)) - cmd.replaceAll(optionChar, ""); + if (cmd.contains(optionChar)) { + cmd = cmd.replaceAll(optionChar, ""); + } + if (cmd.length() > 1) { + throw new IllegalArgumentException( + "Command longer than 1 character, which is not allowed. Use 'addLongCommand()' instead!"); + } this.cmd.add(cmd); return this; } @@ -91,8 +97,13 @@ public class CmdOptions { } public Option setParameterCount(int min, int max) { + return this.setParameterCount(min, max, 0); + } + + public Option setParameterCount(int min, int max, int step) { this.minParameters = min; this.maxParameters = max; + this.stepSizeParameters = step; return this; } @@ -334,7 +345,19 @@ public class CmdOptions { return false; } + private boolean switchExists(char c) { + for (Option op : this.options.values()) { + for (String s : op.cmd) { + if (s.toCharArray()[0] == c) { + return true; + } + } + } + return false; + } + public void parse(String[] args) { + int exit = 0; // get indices Integer[] indices = getIndices(args); // check for correct options @@ -342,13 +365,23 @@ public class CmdOptions { for (Integer i : indices) { String o = args[i].replaceAll(optionChar, ""); if (!optionExists(o)) { - System.err.println("Unrecognized option '" + o + "'"); - ok = false; + if (this.combineSwitches) { + for (char c : o.toCharArray()) { + if (!switchExists(c)) { + System.err.println("Unrecognized option '" + o + + "'"); + ok = false; + } + } + } else { + System.err.println("Unrecognized option '" + o + "'"); + ok = false; + } } } // quit if there are unknown options if (!ok) { - System.exit(1); + exit = 1; } // now parse for (int a = 0; a < indices.length; ++a) { @@ -381,12 +414,19 @@ public class CmdOptions { } } if (!ok) { - System.exit(2); + exit = 2; } - if (options.get("help").set) { - System.out.println(this.toString(true)); - System.exit(0); + // check parameter counts + for (Option o : options.values()) { + if (o.getValues().size() < o.minParameters + || o.getValues().size() > o.maxParameters + || o.stepSizeParameters != 0 + && o.getValues().size() % o.stepSizeParameters != 0) { + System.err.println(o.name + + ": Parameter count not correct! Check help."); + exit = 3; + } } // set default for that options that aren't set @@ -397,11 +437,15 @@ public class CmdOptions { + " (" + o.getName() + "): has no default parameter and has to be set on commandline!"); - System.exit(3); + exit = 4; } if (!o.set && o.defaultParameter.size() != 0) o.values.addAll(o.defaultParameter); } + if (options.get("help").set) { + System.out.println(this.toString(true)); + System.exit(exit); + } System.out.println(this.toString(false)); } } From cc15618dcaeb0b5756844a6092ac3780672aedaa Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 20:46:50 +0100 Subject: [PATCH 08/14] reworked Option.toString() --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 5e8ffce..876c7c5 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -146,13 +146,20 @@ public class CmdOptions { public String toString() { String ret = name + " ("; for (String s : cmd) { - ret += s + ", "; + ret += optionChar + s + ", "; } - ret += ")" - + (defaultParameter != null ? ": default=" - + defaultParameter : "") - + (description != null ? "\n\t\t" + description : ""); - if (possibleParams != null) { + for (String s : cmdLong) { + ret += optionChar + s + ", "; + } + ret += ")"; + if (defaultParameter.size() > 0) { + ret += ": default="; + for (String s : defaultParameter) { + ret += s + ","; + } + } + ret += (description != null ? "\n\t\t" + description : ""); + if (possibleParams.size() > 0) { boolean start = true; ret += "\n\t\t(Possible parameters: "; for (String s : possibleParams) { From 6f01a1287be8d512d37e0c03642a202427b6159f Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 20:55:44 +0100 Subject: [PATCH 09/14] fixed some issues --- java/src/to/mmo/cmdlineoptions/CmdOptions.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index 876c7c5..f1f0f83 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -55,7 +55,7 @@ public class CmdOptions { public Option addCommand(String cmd) { if (cmd.contains(optionChar)) { - cmd = cmd.replaceAll(optionChar, ""); + cmd = cmd.replace(optionChar, ""); } if (cmd.length() > 1) { throw new IllegalArgumentException( @@ -67,7 +67,7 @@ public class CmdOptions { public Option addLongCommand(String cmd) { if (cmd.contains(optionChar)) - cmd.replaceAll(optionChar, ""); + cmd = cmd.replace(optionChar, ""); this.cmdLong.add(cmd); return this; } @@ -149,7 +149,7 @@ public class CmdOptions { ret += optionChar + s + ", "; } for (String s : cmdLong) { - ret += optionChar + s + ", "; + ret += optionChar + optionChar + s + ", "; } ret += ")"; if (defaultParameter.size() > 0) { @@ -194,7 +194,7 @@ public class CmdOptions { this.createOption("help") .setDescription( "Show all possible options and their parameters.") - .addCommand("--help").addCommand("-h").addCommand("-?"); + .addLongCommand("--help").addCommand("-h").addCommand("-?"); } public static CmdOptions i() { @@ -370,7 +370,7 @@ public class CmdOptions { // check for correct options boolean ok = true; for (Integer i : indices) { - String o = args[i].replaceAll(optionChar, ""); + String o = args[i].replace(optionChar, ""); if (!optionExists(o)) { if (this.combineSwitches) { for (char c : o.toCharArray()) { @@ -392,7 +392,7 @@ public class CmdOptions { } // now parse for (int a = 0; a < indices.length; ++a) { - String o = args[indices[a]].replaceAll(optionChar, ""); + String o = args[indices[a]].replace(optionChar, ""); // the option is set! this.getBareOption(o).setSet(true); // are there parameters? From 7d0aad767ed0479c8aa7fe5e347db3a70d4150b2 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 20:55:57 +0100 Subject: [PATCH 10/14] made example current --- .../mmo/cmdlineoptions/examples/Example.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/examples/Example.java b/java/src/to/mmo/cmdlineoptions/examples/Example.java index c0308e2..33c58c7 100644 --- a/java/src/to/mmo/cmdlineoptions/examples/Example.java +++ b/java/src/to/mmo/cmdlineoptions/examples/Example.java @@ -8,26 +8,27 @@ public class Example { CmdOptions.i().createOption("name").// give the option a name which you // can // remember within your project - addCommand("--name") // it's always good to define a long - // commandline parameter with a - // describing name + addLongCommand("--name"). // it's always good to define a + // long + // commandline parameter with a + // describing name - .addCommand("-n") // of course, programmer are always lazy so - // give them an possibility to shortcut your - // option - .addDefaultParameter("whatever")// a default parameter is good + addCommand("-n"). // of course, programmer are always lazy so + // give them an possibility to shortcut your + // option + addDefaultParameter("whatever").// a default parameter is good // if you need the option within // your program, but don't care // when it's omitted on // execution - .setDescription("an example option.");// And of course a + setDescription("an example option.");// And of course a // description... for // the help readers, the // few among us... CmdOptions .i() .createOption("complex") - .addCommand("--complex") + .addLongCommand("--complex") // see above // yes you can omit a default parameter, but you have to // check later in your project if that option is set. @@ -42,7 +43,7 @@ public class Example { // here you are able to add some more alternatives for that // option, so here you can use "--complex", "-c", "-i" and // "--ccp" to trigger this option. - .addCommand("-i").addCommand("--ccp"); + .addCommand("-i").addLongCommand("--ccp"); } From b7665c90eb0575c664b4b4b10b5e948ca94a4f71 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 9 Dec 2014 22:19:01 +0100 Subject: [PATCH 11/14] bunch of changes to be test conform. --- .../src/to/mmo/cmdlineoptions/CmdOptions.java | 123 ++++++++++++++---- 1 file changed, 95 insertions(+), 28 deletions(-) diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java index f1f0f83..274046e 100644 --- a/java/src/to/mmo/cmdlineoptions/CmdOptions.java +++ b/java/src/to/mmo/cmdlineoptions/CmdOptions.java @@ -18,8 +18,10 @@ public class CmdOptions { private static CmdOptions instance; private static String optionChar; - + private HashMap options; + private boolean showOptions; private boolean combineSwitches; + private boolean dontQuitOnError; public static class Option { private String name; @@ -144,7 +146,12 @@ public class CmdOptions { } public String toString() { - String ret = name + " ("; + return toString(false); + } + + public String toString(boolean help) { + String ret = name; + ret += " ("; for (String s : cmd) { ret += optionChar + s + ", "; } @@ -152,14 +159,14 @@ public class CmdOptions { ret += optionChar + optionChar + s + ", "; } ret += ")"; - if (defaultParameter.size() > 0) { + if (help && defaultParameter.size() > 0) { ret += ": default="; for (String s : defaultParameter) { ret += s + ","; } } - ret += (description != null ? "\n\t\t" + description : ""); - if (possibleParams.size() > 0) { + ret += (help && description != null ? "\n\t\t" + description : ""); + if (help && possibleParams.size() > 0) { boolean start = true; ret += "\n\t\t(Possible parameters: "; for (String s : possibleParams) { @@ -185,11 +192,11 @@ public class CmdOptions { } } - private HashMap options; - private CmdOptions() { optionChar = "-"; this.setSwitchCombination(false); + this.setShowOptions(false); + this.setDontQuitOnError(false); options = new HashMap(); this.createOption("help") .setDescription( @@ -212,6 +219,14 @@ public class CmdOptions { this.optionChar = c; } + public void setDontQuitOnError(boolean set) { + this.dontQuitOnError = set; + } + + public void setShowOptions(boolean show) { + this.showOptions = show; + } + public void setHelpGeneration(boolean on) { if (!on) { options.remove("help"); @@ -233,6 +248,14 @@ public class CmdOptions { return getOption(name); } + public String get(String name, int index) { + String[] values = getOption(name); + if (values.length > index && index > 0) { + return values[index]; + } + return null; + } + public String[] getOption(String name) { if (options.get(name).values.size() > 0) return options.get(name).values.toArray(new String[0]); @@ -306,6 +329,16 @@ public class CmdOptions { return this.getValuesAsList(option).contains(parameter); } + public void resetValues() { + for (Option o : options.values()) { + o.values.clear(); + } + } + + public void reset() { + options.clear(); + } + public String toString(boolean help) { StringBuilder b = new StringBuilder(); if (help) { @@ -320,7 +353,7 @@ public class CmdOptions { } }); for (Option o : vars) { - b.append("\t").append(o.toString()).append("\n"); + b.append("\t").append(o.toString(help)).append("\n"); } b.append("/options\n"); return b.toString(); @@ -336,20 +369,24 @@ public class CmdOptions { return indices.toArray(new Integer[0]); } - private boolean optionExists(String option) { + private Option getOptionByCommand(String cmd) { for (Option o : this.options.values()) { for (String s : o.cmd) { - if (option.equals(s)) { - return true; + if (cmd.equals(s)) { + return o; } } for (String s : o.cmdLong) { - if (option.equals(s)) { - return true; + if (cmd.equals(s)) { + return o; } } } - return false; + return null; + } + + private boolean cmdExists(String cmd) { + return getOptionByCommand(cmd) != null; } private boolean switchExists(char c) { @@ -363,7 +400,21 @@ public class CmdOptions { return false; } - public void parse(String[] args) { + private Option[] getOptionBySwitches(String switches) { + List