diff --git a/README.md b/README.md
index e59a1c3..562400b 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,12 @@ Why not using a small and easy library, which does exactly THAT task? Which give
-This is the goal of this project.
+This is a goal of this project.
+
+
+The main 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,16 @@ Current work and future plans
===============
Working on:
-- JUnit tests
-- Java: better errorhandling
-- Java: Examples
-- C version [done]
+- ~~JUnit tests~~
+- ~~Java: better errorhandling~~
+- ~~Java: Examples~~
+- ~~C version~~
- Getting started
+- Unit tests
+- ~~Some java improvements~~
+ - get the improvments into the C version
Future plans:
- C++ class version
+ - haskell
- ...
diff --git a/java/.classpath b/java/.classpath
index fb50116..3e0fb27 100644
--- a/java/.classpath
+++ b/java/.classpath
@@ -2,5 +2,6 @@
+
diff --git a/java/src/to/mmo/cmdlineoptions/CmdOptions.java b/java/src/to/mmo/cmdlineoptions/CmdOptions.java
index 23ba8e1..274046e 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,
@@ -16,15 +17,26 @@ 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;
private ArrayList cmd;
+ private ArrayList cmdLong;
private String description;
private ArrayList defaultParameter;
private ArrayList possibleParams;
private boolean set;
+ private boolean required;
private ArrayList values;
+ private int maxParameters, minParameters;
+ private ArrayList examples;
+ private int stepSizeParameters;
public ArrayList getValues() {
return values;
@@ -37,15 +49,31 @@ 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 = cmd.replace(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;
}
+ public Option addLongCommand(String cmd) {
+ if (cmd.contains(optionChar))
+ cmd = cmd.replace(optionChar, "");
+ this.cmdLong.add(cmd);
+ return this;
+ }
+
public Option addDefaultParameter(String d) {
this.defaultParameter.add(d);
return this;
@@ -70,6 +98,17 @@ public class CmdOptions {
return this;
}
+ 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;
+ }
+
public String getDescription() {
return description;
}
@@ -79,25 +118,55 @@ public class CmdOptions {
return this;
}
+ public Option setRequired(boolean required) {
+ this.required = true;
+ return this;
+ }
+
public boolean isSet() {
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;
}
+ public Option addExample(String example) {
+ this.examples.add(example);
+ return this;
+ }
+
public String toString() {
- String ret = name + " (";
+ return toString(false);
+ }
+
+ public String toString(boolean help) {
+ String ret = name;
+ ret += " (";
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 + optionChar + s + ", ";
+ }
+ ret += ")";
+ if (help && defaultParameter.size() > 0) {
+ ret += ": default=";
+ for (String s : defaultParameter) {
+ ret += s + ",";
+ }
+ }
+ 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) {
@@ -123,14 +192,16 @@ 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(
"Show all possible options and their parameters.")
- .addCommand("--help").addCommand("-h").addCommand("-?");
+ .addLongCommand("--help").addCommand("-h").addCommand("-?");
}
public static CmdOptions i() {
@@ -140,6 +211,28 @@ public class CmdOptions {
return instance;
}
+ public void setSwitchCombination(boolean on) {
+ this.combineSwitches = on;
+ }
+
+ public void setOptionCharacter(String c) {
+ 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");
+ }
+ }
+
public Option createOption(String name) {
Option o = new Option();
o.setName(name);
@@ -147,6 +240,22 @@ public class CmdOptions {
return o;
}
+ public Option getBareOption(String name) {
+ return options.get(name);
+ }
+
+ public String[] get(String name) {
+ 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]);
@@ -155,6 +264,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();
@@ -172,6 +288,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();
@@ -189,17 +313,30 @@ 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 this.getValuesAsList(option).contains(parameter);
+ }
+
+ public void resetValues() {
+ for (Option o : options.values()) {
+ o.values.clear();
}
- return false;
+ }
+
+ public void reset() {
+ options.clear();
}
public String toString(boolean help) {
@@ -216,72 +353,173 @@ 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();
}
- 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);
}
}
- if (options.get("help").set) {
- System.out.println(this.toString(true));
- System.exit(0);
+ return indices.toArray(new Integer[0]);
+ }
+
+ private Option getOptionByCommand(String cmd) {
+ for (Option o : this.options.values()) {
+ for (String s : o.cmd) {
+ if (cmd.equals(s)) {
+ return o;
+ }
+ }
+ for (String s : o.cmdLong) {
+ if (cmd.equals(s)) {
+ return o;
+ }
+ }
+ }
+ return null;
+ }
+
+ private boolean cmdExists(String cmd) {
+ return getOptionByCommand(cmd) != null;
+ }
+
+ 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;
+ }
+
+ private Option[] getOptionBySwitches(String switches) {
+ List