Compare commits

...

19 Commits

Author SHA1 Message Date
Marcel M. Otte a55cf4c731 fixed tag name 2nd 2016-06-02 18:01:46 +02:00
Marcel M. Otte ad95efe412 fixed image tag 2016-06-02 17:59:17 +02:00
Marcel M. Otte 5596af7f69 updated gitignore with .gradle and build folders 2016-06-02 17:52:48 +02:00
Marcel M. Otte 38ab9c47a1 added gradle build 2016-04-19 07:43:19 +02:00
Marcel Otte d3ddc89e7b commented out java build 2016-04-18 20:37:29 +02:00
Marcel Otte ce6cabe7b9 Merge branch 'master' of ssh://gitlab.mmo.to:42420/qwc/CmdLineOptions 2016-04-18 20:35:22 +02:00
Marcel Otte 86c1ea126a latest changes for java 2016-04-18 20:30:41 +02:00
Marcel M. Otte 2fe9ad371e Merge branch 'master' of ssh://gitlab.mmo.to:42420/qwc/CmdLineOptions 2016-03-29 17:14:24 +02:00
Marcel Otte 25e6e78881 fixed quite old typo... 2016-03-27 20:58:07 +02:00
Marcel Otte 89f7c53e4d corrected silly path error 2016-03-27 20:53:44 +02:00
Marcel Otte 8529bff989 playing with artifacts... 2016-03-27 20:52:58 +02:00
Marcel Otte 8b8b0f8d70 oops.. 2016-03-27 20:49:33 +02:00
Marcel Otte d9958cb7b2 removing test stage due to problems of moving artifacts from one docker instance to another... 2016-03-27 20:47:13 +02:00
Marcel Otte 334e0f0b21 corrected artifacts... 2016-03-27 20:45:51 +02:00
Marcel Otte 6230cb9309 added artifacts to build-c job 2016-03-27 20:43:15 +02:00
Marcel Otte 97b1fbbb3d added dependency to test job 2016-03-27 20:32:43 +02:00
Marcel Otte e4f46235f3 added first .gitlab-ci.yml 2016-03-27 20:26:54 +02:00
Marcel Otte 26a6efe57c some comments, and starting to implement regex possibilities. 2014-12-29 16:57:29 +01:00
Marcel Otte d09f159ca8 'Outsourced' option class. 2014-12-29 16:28:26 +01:00
8 changed files with 334 additions and 223 deletions

3
.gitignore vendored
View File

@ -5,3 +5,6 @@
*.war *.war
*.ear *.ear
/bin /bin
*/.settings/
*/.gradle
*/build*

30
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,30 @@
stages:
- build
build-c:
image: archlinux_gcc_qt5
stage: build
script:
- cd c; make
artifacts:
paths:
- c/libcmdlineoptions.*
- c/tests-*
build-java:
image: archlinux_jdk8
stage: build
script:
- cd java
- gradle build
artifacts:
paths:
- java/build/libs/cmdlineoptions*.jar
#test-c:
#stage: test
#script:
#- cd c; make test
#dependencies:
#- build-c

View File

@ -23,7 +23,7 @@ void configureDefaultSet() {
CmdLO_AddPossibleParameter("example", "test2"); CmdLO_AddPossibleParameter("example", "test2");
} }
#define ASSERT(x,y) if(!(x)) printf("ASSERT FAILED! '%s'.\n",y); else printf("ASSERT '%s' succeded.\n",y) #define ASSERT(x,y) if(!(x)) printf("ASSERT FAILED! '%s'.\n",y); else printf("ASSERT '%s' succeeded.\n",y)
int main(int argc, char** argv) { int main(int argc, char** argv) {
// configure the options // configure the options
@ -182,4 +182,3 @@ int main(int argc, char** argv) {
"something value count"); "something value count");
return 0; return 0;
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -12,6 +12,7 @@
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.springsource.ide.eclipse.gradle.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

6
java/build.gradle Normal file
View File

@ -0,0 +1,6 @@
apply plugin: 'java'
jar {
baseName = 'cmdlineoptions'
version = '0.1.0'
}

View File

@ -18,186 +18,17 @@ public class CmdOptions {
private static CmdOptions instance; private static CmdOptions instance;
private static String optionChar; private static String optionChar;
private HashMap<String, Option> options; private HashMap<String, CommandLineOption> options;
private boolean showOptions; private boolean showOptions;
private boolean combineSwitches; private boolean combineSwitches;
private boolean dontQuitOnError; private boolean dontQuitOnError;
public static class Option {
private String name;
private ArrayList<String> cmd;
private ArrayList<String> cmdLong;
private String description;
private ArrayList<String> defaultParameter;
private ArrayList<String> possibleParams;
private boolean set;
private boolean required;
private ArrayList<String> values;
private int maxParameters, minParameters;
private ArrayList<String> examples;
private int stepSizeParameters;
public ArrayList<String> getValues() {
return values;
}
public ArrayList<String> getDefaultParameter() {
return defaultParameter;
}
public Option() {
values = new ArrayList<String>();
cmd = new ArrayList<String>();
cmdLong = new ArrayList<String>();
defaultParameter = new ArrayList<String>();
possibleParams = new ArrayList<String>();
examples = new ArrayList<String>();
}
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;
}
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 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;
}
public Option setDescription(String description) {
this.description = description;
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() {
return toString(false);
}
public String toString(boolean help) {
String ret = name;
ret += " (";
for (String s : cmd) {
ret += optionChar + s + ", ";
}
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) {
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 CmdOptions() { private CmdOptions() {
optionChar = "-"; optionChar = "-";
this.setSwitchCombination(false); this.setSwitchCombination(false);
this.setShowOptions(false); this.setShowOptions(false);
this.setDontQuitOnError(false); this.setDontQuitOnError(false);
options = new HashMap<String, Option>(); options = new HashMap<String, CommandLineOption>();
this.createOption("help") this.createOption("help")
.setDescription( .setDescription(
"Show all possible options and their parameters.") "Show all possible options and their parameters.")
@ -215,6 +46,7 @@ public class CmdOptions {
this.combineSwitches = on; this.combineSwitches = on;
} }
@SuppressWarnings("static-access")
public void setOptionCharacter(String c) { public void setOptionCharacter(String c) {
this.optionChar = c; this.optionChar = c;
} }
@ -233,14 +65,13 @@ public class CmdOptions {
} }
} }
public Option createOption(String name) { public CommandLineOption createOption(String name) {
Option o = new Option(); CommandLineOption o = new CommandLineOption(name);
o.setName(name);
this.options.put(name, o); this.options.put(name, o);
return o; return o;
} }
public Option getBareOption(String name) { public CommandLineOption getBareOption(String name) {
return options.get(name); return options.get(name);
} }
@ -257,10 +88,11 @@ public class CmdOptions {
} }
public String[] getOption(String name) { public String[] getOption(String name) {
if (options.get(name).values.size() > 0) if (options.get(name).getValues().size() > 0)
return options.get(name).values.toArray(new String[0]);
else if (options.get(name).defaultParameter != null)
return options.get(name).getValues().toArray(new String[0]); return options.get(name).getValues().toArray(new String[0]);
else if (options.get(name).getDefaultParameter().size() > 0)
return options.get(name).getDefaultParameter()
.toArray(new String[0]);
return null; return null;
} }
@ -272,9 +104,9 @@ public class CmdOptions {
} }
public Integer[] getOptionAsInt(String name) { public Integer[] getOptionAsInt(String name) {
if (options.get(name).values.size() > 0) { if (options.get(name).getValues().size() > 0) {
ArrayList<Integer> list = new ArrayList<Integer>(); ArrayList<Integer> list = new ArrayList<Integer>();
for (String o : options.get(name).values) { for (String o : options.get(name).getValues()) {
list.add(Integer.parseInt(o)); list.add(Integer.parseInt(o));
} }
return list.toArray(new Integer[0]); return list.toArray(new Integer[0]);
@ -297,9 +129,9 @@ public class CmdOptions {
} }
public Double[] getOptionAsDouble(String name) { public Double[] getOptionAsDouble(String name) {
if (options.get(name).values.size() > 0) { if (options.get(name).getValues().size() > 0) {
ArrayList<Double> list = new ArrayList<Double>(); ArrayList<Double> list = new ArrayList<Double>();
for (String o : options.get(name).values) { for (String o : options.get(name).getValues()) {
list.add(Double.parseDouble(o)); list.add(Double.parseDouble(o));
} }
return list.toArray(new Double[0]); return list.toArray(new Double[0]);
@ -322,7 +154,7 @@ public class CmdOptions {
} }
public boolean isSet(String option) { public boolean isSet(String option) {
return options.get(option).set; return options.get(option).isSet();
} }
public boolean isSet(String option, String parameter) { public boolean isSet(String option, String parameter) {
@ -330,8 +162,8 @@ public class CmdOptions {
} }
public void resetValues() { public void resetValues() {
for (Option o : options.values()) { for (CommandLineOption o : options.values()) {
o.values.clear(); o.getValues().clear();
} }
} }
@ -345,14 +177,15 @@ public class CmdOptions {
b.append("Possible options:\n"); b.append("Possible options:\n");
} }
b.append("-options\n"); b.append("-options\n");
Option[] vars = options.values().toArray(new Option[0]); CommandLineOption[] vars = options.values().toArray(
Arrays.sort(vars, new Comparator<Option>() { new CommandLineOption[0]);
Arrays.sort(vars, new Comparator<CommandLineOption>() {
@Override @Override
public int compare(Option o1, Option o2) { public int compare(CommandLineOption o1, CommandLineOption o2) {
return o1.name.compareTo(o2.name); return o1.getName().compareTo(o2.getName());
} }
}); });
for (Option o : vars) { for (CommandLineOption o : vars) {
b.append("\t").append(o.toString(help)).append("\n"); b.append("\t").append(o.toString(help)).append("\n");
} }
b.append("/options\n"); b.append("/options\n");
@ -369,14 +202,14 @@ public class CmdOptions {
return indices.toArray(new Integer[0]); return indices.toArray(new Integer[0]);
} }
private Option getOptionByCommand(String cmd) { private CommandLineOption getOptionByCommand(String cmd) {
for (Option o : this.options.values()) { for (CommandLineOption o : this.options.values()) {
for (String s : o.cmd) { for (String s : o.getCmd()) {
if (cmd.equals(s)) { if (cmd.equals(s)) {
return o; return o;
} }
} }
for (String s : o.cmdLong) { for (String s : o.getCmdLong()) {
if (cmd.equals(s)) { if (cmd.equals(s)) {
return o; return o;
} }
@ -390,8 +223,8 @@ public class CmdOptions {
} }
private boolean switchExists(char c) { private boolean switchExists(char c) {
for (Option op : this.options.values()) { for (CommandLineOption op : this.options.values()) {
for (String s : op.cmd) { for (String s : op.getCmd()) {
if (s.toCharArray()[0] == c) { if (s.toCharArray()[0] == c) {
return true; return true;
} }
@ -400,10 +233,10 @@ public class CmdOptions {
return false; return false;
} }
private Option[] getOptionBySwitches(String switches) { private CommandLineOption[] getOptionBySwitches(String switches) {
List<Option> o = new ArrayList<CmdOptions.Option>(); List<CommandLineOption> o = new ArrayList<CommandLineOption>();
for (Option op : this.options.values()) { for (CommandLineOption op : this.options.values()) {
for (String s : op.cmd) { for (String s : op.getCmd()) {
for (char c : switches.toCharArray()) { for (char c : switches.toCharArray()) {
if (s.toCharArray()[0] == c) { if (s.toCharArray()[0] == c) {
o.add(op); o.add(op);
@ -411,7 +244,7 @@ public class CmdOptions {
} }
} }
} }
return o.toArray(new Option[0]); return o.toArray(new CommandLineOption[0]);
} }
public int parse(String[] args) { public int parse(String[] args) {
@ -442,14 +275,14 @@ public class CmdOptions {
exit = 1; exit = 1;
} }
// now parse // now parse
Option op; CommandLineOption op;
for (int a = 0; a < indices.length; ++a) { for (int a = 0; a < indices.length; ++a) {
String o = args[indices[a]].replace(optionChar, ""); String o = args[indices[a]].replace(optionChar, "");
op = this.getOptionByCommand(o); op = this.getOptionByCommand(o);
if (op == null) { if (op == null) {
if (this.combineSwitches) { if (this.combineSwitches) {
Option[] mop = getOptionBySwitches(o); CommandLineOption[] mop = getOptionBySwitches(o);
for (Option opt : mop) { for (CommandLineOption opt : mop) {
opt.setSet(true); opt.setSet(true);
} }
} }
@ -473,12 +306,12 @@ public class CmdOptions {
} }
// check for possible parameters // check for possible parameters
for (Option o : options.values()) { for (CommandLineOption o : options.values()) {
for (String s : o.getValues()) { for (String s : o.getValues()) {
if (o.possibleParams.size() > 0 if (o.getPossibleParams().size() > 0
&& !o.possibleParams.contains(s)) { && !o.getPossibleParams().contains(s)) {
System.err.println("Parameter \"" + s + "\" for Option \"" System.err.println("Parameter \"" + s + "\" for Option \""
+ o.name + "\" not allowed!"); + o.getName() + "\" not allowed!");
ok = false; ok = false;
} }
} }
@ -488,31 +321,33 @@ public class CmdOptions {
} }
// check parameter counts // check parameter counts
for (Option o : options.values()) { for (CommandLineOption o : options.values()) {
if (o.getValues().size() < o.minParameters && o.minParameters != 0 if (o.getValues().size() < o.getMinParameters()
|| o.getValues().size() > o.maxParameters && o.getMinParameters() != 0
&& o.maxParameters != 0 || o.stepSizeParameters != 0 || o.getValues().size() > o.getMaxParameters()
&& o.getValues().size() % o.stepSizeParameters != 0) { && o.getMaxParameters() != 0
System.err.println(o.name || o.getStepSizeParameters() != 0
&& o.getValues().size() % o.getStepSizeParameters() != 0) {
System.err.println(o.getName()
+ ": Parameter count not correct! Check help."); + ": Parameter count not correct! Check help.");
exit = 3; exit = 3;
} }
} }
// set default for that options that aren't set // set default for that options that aren't set
for (Option o : options.values()) { for (CommandLineOption o : options.values()) {
if (!o.set && o.required) { if (!o.isSet() && o.isRequired()) {
System.err System.err
.println(o.name .println(o.getName()
+ " (" + " ("
+ o.getName() + o.getName()
+ "): has no default parameter and has to be set on commandline!"); + "): has no default parameter and has to be set on commandline!");
exit = 4; exit = 4;
} }
if (!o.set && o.defaultParameter.size() != 0) if (!o.isSet() && o.getDefaultParameter().size() != 0)
o.values.addAll(o.defaultParameter); o.getValues().addAll(o.getDefaultParameter());
} }
if (options.get("help").set || exit > 0) { if (options.get("help").isSet() || exit > 0) {
System.out.println(this.toString(true)); System.out.println(this.toString(true));
if (!this.dontQuitOnError) if (!this.dontQuitOnError)
System.exit(exit); System.exit(exit);
@ -522,4 +357,8 @@ public class CmdOptions {
} }
return exit; return exit;
} }
public static String getOptionChar() {
return optionChar;
}
} }

View File

@ -0,0 +1,233 @@
package to.mmo.cmdlineoptions;
import java.util.ArrayList;
public class CommandLineOption {
private String name;
private ArrayList<String> cmd; // short command, only 1 character!
private ArrayList<String> cmdLong; // long command, several characters
private String description; // description of the cmd line parameter
private ArrayList<String> examples; // provide examples?!
private ArrayList<String> defaultParameters; // parameter not set but you
// need input?
private ArrayList<String> possibleParams; // 'enum' of options which can be
// specified
private ArrayList<String> values; // the real values given on the command
// line
private ArrayList<String> parameterRegexes; // regex fun, here you can
// specify regex strings for
// multiple parameters
private boolean set; // is it set or not?
private boolean required; // required or not?
private int maxParameters, minParameters; // count of parameters
private int stepSizeParameters; // parameter count has always to be a
// multiple of ... step size parameter!
// large footprint here ... future optimization: create arrays only when
// needed...
public CommandLineOption() {
values = new ArrayList<String>();
cmd = new ArrayList<String>();
cmdLong = new ArrayList<String>();
defaultParameters = new ArrayList<String>();
possibleParams = new ArrayList<String>();
examples = new ArrayList<String>();
parameterRegexes = new ArrayList<String>();
}
public CommandLineOption(String name) {
this();
this.name = name;
}
public ArrayList<String> getValues() {
return values;
}
public ArrayList<String> getDefaultParameter() {
return defaultParameters;
}
public CommandLineOption addCommand(String cmd) {
if (cmd.contains(CmdOptions.getOptionChar())) {
cmd = cmd.replace(CmdOptions.getOptionChar(), "");
}
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 CommandLineOption addLongCommand(String cmd) {
if (cmd.contains(CmdOptions.getOptionChar()))
cmd = cmd.replace(CmdOptions.getOptionChar(), "");
this.cmdLong.add(cmd);
return this;
}
public CommandLineOption addDefaultParameter(String d) {
this.defaultParameters.add(d);
return this;
}
public CommandLineOption addPossibleParameter(String p) {
this.possibleParams.add(p);
return this;
}
public CommandLineOption addValue(String value) {
this.values.add(value);
return this;
}
public String getName() {
return name;
}
public CommandLineOption setName(String name) {
this.name = name;
return this;
}
public CommandLineOption setParameterCount(int min, int max) {
return this.setParameterCount(min, max, 0);
}
public CommandLineOption setParameterCount(int min, int max, int step) {
this.minParameters = min;
this.maxParameters = max;
this.stepSizeParameters = step;
return this;
}
public String getDescription() {
return description;
}
public CommandLineOption setDescription(String description) {
this.description = description;
return this;
}
public CommandLineOption 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 CommandLineOption setSet(boolean set) {
this.set = set;
return this;
}
public CommandLineOption addExample(String example) {
this.examples.add(example);
return this;
}
public String toString() {
return toString(false);
}
public String toString(boolean help) {
String ret = name;
ret += " (";
for (String s : cmd) {
ret += CmdOptions.getOptionChar() + s + ", ";
}
for (String s : cmdLong) {
ret += CmdOptions.getOptionChar() + CmdOptions.getOptionChar() + s
+ ", ";
}
ret += ")";
if (help && defaultParameters.size() > 0) {
ret += ": default=";
for (String s : defaultParameters) {
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) {
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 int getMaxParameters() {
return maxParameters;
}
public void setMaxParameters(int maxParameters) {
this.maxParameters = maxParameters;
}
public int getMinParameters() {
return minParameters;
}
public void setMinParameters(int minParameters) {
this.minParameters = minParameters;
}
public boolean isRequired() {
return required;
}
public int getStepSizeParameters() {
return stepSizeParameters;
}
public void setStepSizeParameters(int stepSizeParameters) {
this.stepSizeParameters = stepSizeParameters;
}
public ArrayList<String> getCmd() {
return cmd;
}
public ArrayList<String> getCmdLong() {
return cmdLong;
}
public ArrayList<String> getPossibleParams() {
return possibleParams;
}
public ArrayList<String> getExamples() {
return examples;
}
}