Compare commits
13 Commits
Author | SHA1 | Date |
---|---|---|
Marcel M. Otte | a5ac7be0e9 | |
Marcel M. Otte | aea0ebba87 | |
Marcel Otte | 9527611da8 | |
Marcel M. Otte | 750bf518ce | |
Marcel M. Otte | bd6deca62e | |
Marcel M. Otte | 50b8645f8c | |
Marcel M. Otte | b6a26a17a1 | |
Marcel M. Otte | b23249d5b9 | |
Marcel M. Otte | e4d053e134 | |
Marcel M. Otte | 30b04c079e | |
Marcel M. Otte | cf03f083c0 | |
Marcel M. Otte | 7ed2f20596 | |
Marcel M. Otte | 3d44d607c8 |
|
@ -11,16 +11,17 @@ build-c:
|
|||
- c/libcmdlineoptions.*
|
||||
- c/tests-*
|
||||
|
||||
build-java:
|
||||
image: archlinux_jdk8
|
||||
build-cpp:
|
||||
image: archlinux_gcc5.3_qt5
|
||||
stage: build
|
||||
script:
|
||||
- cd java
|
||||
- gradle build
|
||||
- cd cpp; mkdir build; cd build
|
||||
- cmake ..
|
||||
- make
|
||||
artifacts:
|
||||
paths:
|
||||
- java/build/libs/cmdlineoptions*.jar
|
||||
|
||||
- cpp/build/libCmdLineOptions.so
|
||||
- cpp/build/libCmdLineOptions_static.a
|
||||
|
||||
#test-c:
|
||||
#stage: test
|
||||
|
|
|
@ -23,7 +23,7 @@ void configureDefaultSet() {
|
|||
CmdLO_AddPossibleParameter("example", "test2");
|
||||
}
|
||||
|
||||
#define ASSERT(x,y) if(!(x)) printf("ASSERT FAILED! '%s'.\n",y); else printf("ASSERT '%s' succeeded.\n",y)
|
||||
#define ASSERT(x,y) if(!(x)) printf("ASSERT FAILED! '%s'.\n",y); else printf("ASSERT '%s' succeded.\n",y)
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// configure the options
|
||||
|
@ -182,3 +182,4 @@ int main(int argc, char** argv) {
|
|||
"something value count");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
*.user
|
||||
*.swp
|
||||
build*
|
|
@ -0,0 +1,8 @@
|
|||
cmake_minimum_required(VERSION 2.6)
|
||||
project(CmdLineOptions)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "--std=c++14 -g -Wall")
|
||||
add_library(CmdLineOptions_static STATIC CmdLineOptions.cpp)
|
||||
add_library(CmdLineOptions SHARED CmdLineOptions.cpp)
|
||||
|
||||
#add_executable(CmdLineOptions_test CmdLineOptions_test.cpp)
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* cmdlineoptions.cpp
|
||||
*
|
||||
* Created on: 04. Okt. 2015
|
||||
* Author: qwc
|
||||
*/
|
||||
|
||||
#include "CmdLineOptions.h"
|
||||
|
||||
namespace cmdlineoptions {
|
||||
|
||||
CmdLineOptions::~CmdLineOptions() {}
|
||||
|
||||
const std::unique_ptr<CmdLineOptions> &CmdLineOptions::i() {
|
||||
if (!instance) {
|
||||
instance = std::unique_ptr<CmdLineOptions>(new CmdLineOptions());
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
std::shared_ptr<Option> CmdLineOptions::create(std::string &name) {
|
||||
std::shared_ptr<Option> option = std::make_shared<Option>(name);
|
||||
this->options.insert(std::make_pair(name, option));
|
||||
return option;
|
||||
}
|
||||
|
||||
std::shared_ptr<Option> CmdLineOptions::getOption(std::string &name) {
|
||||
return this->options.at(name);
|
||||
}
|
||||
|
||||
void CmdLineOptions::setCommandCharacter(char &cmdchar) {
|
||||
this->cmdchar = cmdchar;
|
||||
}
|
||||
|
||||
void CmdLineOptions::parse(char **argv, int argc) {}
|
||||
|
||||
void CmdLineOptions::parse(std::list<std::string> argv) {}
|
||||
|
||||
CmdLineOptions::CmdLineOptions() {}
|
||||
|
||||
} /* namespace cmdlineoptions */
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* cmdlineoptions.h
|
||||
*
|
||||
* Created on: 04. Okt. 2015
|
||||
* Author: qwc
|
||||
*/
|
||||
|
||||
#ifndef CMDLINEOPTIONS_H_
|
||||
#define CMDLINEOPTIONS_H_
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
namespace cmdlineoptions {
|
||||
|
||||
// using namespace std;
|
||||
|
||||
class CmdLineOptions;
|
||||
|
||||
class Option {
|
||||
private:
|
||||
// important information
|
||||
std::string name;
|
||||
std::string description;
|
||||
bool set;
|
||||
// lists for attributes of an option
|
||||
std::unique_ptr<std::list<std::string>> options;
|
||||
std::unique_ptr<std::list<std::string>> possibleParameters;
|
||||
std::unique_ptr<std::list<std::string>> defaultParameters;
|
||||
// values, this list can only be filled by parsing the input data
|
||||
std::unique_ptr<std::list<std::string>> values;
|
||||
|
||||
public:
|
||||
Option();
|
||||
Option(std::string name);
|
||||
virtual ~Option();
|
||||
|
||||
// Methods setting configuration attributes
|
||||
std::shared_ptr<Option> setName(std::string name);
|
||||
std::shared_ptr<Option> addCommand(std::string cmd);
|
||||
std::shared_ptr<Option> addPossibleParameter(std::string param);
|
||||
std::shared_ptr<Option> addDefaultParameter(std::string param);
|
||||
std::shared_ptr<Option> setStrictChoice(bool strict);
|
||||
|
||||
// Methods for retrieving information in different formats
|
||||
std::shared_ptr<std::list<std::string>> getValues();
|
||||
std::string getValueAsString(uint index);
|
||||
uint getValueCount();
|
||||
int getValueAsInteger(uint index);
|
||||
double getValueAsDouble(uint index);
|
||||
float getValueAsFloat(uint index);
|
||||
};
|
||||
|
||||
class CmdLineOptions {
|
||||
|
||||
public:
|
||||
virtual ~CmdLineOptions();
|
||||
static const std::unique_ptr<CmdLineOptions> &i();
|
||||
std::shared_ptr<Option> create(std::string &name);
|
||||
std::shared_ptr<Option> getOption(std::string &name);
|
||||
void setCommandCharacter(char &cmdchar);
|
||||
|
||||
void parse(char **argv, int argc);
|
||||
void parse(std::list<std::string> argv);
|
||||
|
||||
private:
|
||||
char cmdchar;
|
||||
CmdLineOptions();
|
||||
static std::unique_ptr<CmdLineOptions> instance;
|
||||
std::map<std::string, std::shared_ptr<Option>> options;
|
||||
};
|
||||
|
||||
} /* namespace cmdlineoptions */
|
||||
|
||||
#endif /* CMDLINEOPTIONS_H_ */
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* cmdlineoptions_test.cpp
|
||||
*
|
||||
* Created on: 04. Okt. 2015
|
||||
* Author: qwc
|
||||
*/
|
||||
|
||||
#include "CmdLineOptions.h"
|
||||
|
||||
namespace cmdlineoptions {
|
||||
|
||||
int main(int argc, char**argv)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
} /* namespace cmdlineoptions */
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<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="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.springsource.ide.eclipse.gradle.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
apply plugin: 'java'
|
||||
|
||||
jar {
|
||||
baseName = 'cmdlineoptions'
|
||||
version = '0.1.0'
|
||||
}
|
|
@ -18,17 +18,186 @@ public class CmdOptions {
|
|||
private static CmdOptions instance;
|
||||
|
||||
private static String optionChar;
|
||||
private HashMap<String, CommandLineOption> options;
|
||||
private HashMap<String, Option> options;
|
||||
private boolean showOptions;
|
||||
private boolean combineSwitches;
|
||||
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() {
|
||||
optionChar = "-";
|
||||
this.setSwitchCombination(false);
|
||||
this.setShowOptions(false);
|
||||
this.setDontQuitOnError(false);
|
||||
options = new HashMap<String, CommandLineOption>();
|
||||
options = new HashMap<String, Option>();
|
||||
this.createOption("help")
|
||||
.setDescription(
|
||||
"Show all possible options and their parameters.")
|
||||
|
@ -46,7 +215,6 @@ public class CmdOptions {
|
|||
this.combineSwitches = on;
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void setOptionCharacter(String c) {
|
||||
this.optionChar = c;
|
||||
}
|
||||
|
@ -65,13 +233,14 @@ public class CmdOptions {
|
|||
}
|
||||
}
|
||||
|
||||
public CommandLineOption createOption(String name) {
|
||||
CommandLineOption o = new CommandLineOption(name);
|
||||
public Option createOption(String name) {
|
||||
Option o = new Option();
|
||||
o.setName(name);
|
||||
this.options.put(name, o);
|
||||
return o;
|
||||
}
|
||||
|
||||
public CommandLineOption getBareOption(String name) {
|
||||
public Option getBareOption(String name) {
|
||||
return options.get(name);
|
||||
}
|
||||
|
||||
|
@ -88,11 +257,10 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
public String[] getOption(String name) {
|
||||
if (options.get(name).getValues().size() > 0)
|
||||
if (options.get(name).values.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]);
|
||||
else if (options.get(name).getDefaultParameter().size() > 0)
|
||||
return options.get(name).getDefaultParameter()
|
||||
.toArray(new String[0]);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -104,9 +272,9 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
public Integer[] getOptionAsInt(String name) {
|
||||
if (options.get(name).getValues().size() > 0) {
|
||||
if (options.get(name).values.size() > 0) {
|
||||
ArrayList<Integer> list = new ArrayList<Integer>();
|
||||
for (String o : options.get(name).getValues()) {
|
||||
for (String o : options.get(name).values) {
|
||||
list.add(Integer.parseInt(o));
|
||||
}
|
||||
return list.toArray(new Integer[0]);
|
||||
|
@ -129,9 +297,9 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
public Double[] getOptionAsDouble(String name) {
|
||||
if (options.get(name).getValues().size() > 0) {
|
||||
if (options.get(name).values.size() > 0) {
|
||||
ArrayList<Double> list = new ArrayList<Double>();
|
||||
for (String o : options.get(name).getValues()) {
|
||||
for (String o : options.get(name).values) {
|
||||
list.add(Double.parseDouble(o));
|
||||
}
|
||||
return list.toArray(new Double[0]);
|
||||
|
@ -154,7 +322,7 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
public boolean isSet(String option) {
|
||||
return options.get(option).isSet();
|
||||
return options.get(option).set;
|
||||
}
|
||||
|
||||
public boolean isSet(String option, String parameter) {
|
||||
|
@ -162,8 +330,8 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
public void resetValues() {
|
||||
for (CommandLineOption o : options.values()) {
|
||||
o.getValues().clear();
|
||||
for (Option o : options.values()) {
|
||||
o.values.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -177,15 +345,14 @@ public class CmdOptions {
|
|||
b.append("Possible options:\n");
|
||||
}
|
||||
b.append("-options\n");
|
||||
CommandLineOption[] vars = options.values().toArray(
|
||||
new CommandLineOption[0]);
|
||||
Arrays.sort(vars, new Comparator<CommandLineOption>() {
|
||||
Option[] vars = options.values().toArray(new Option[0]);
|
||||
Arrays.sort(vars, new Comparator<Option>() {
|
||||
@Override
|
||||
public int compare(CommandLineOption o1, CommandLineOption o2) {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
public int compare(Option o1, Option o2) {
|
||||
return o1.name.compareTo(o2.name);
|
||||
}
|
||||
});
|
||||
for (CommandLineOption o : vars) {
|
||||
for (Option o : vars) {
|
||||
b.append("\t").append(o.toString(help)).append("\n");
|
||||
}
|
||||
b.append("/options\n");
|
||||
|
@ -202,14 +369,14 @@ public class CmdOptions {
|
|||
return indices.toArray(new Integer[0]);
|
||||
}
|
||||
|
||||
private CommandLineOption getOptionByCommand(String cmd) {
|
||||
for (CommandLineOption o : this.options.values()) {
|
||||
for (String s : o.getCmd()) {
|
||||
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.getCmdLong()) {
|
||||
for (String s : o.cmdLong) {
|
||||
if (cmd.equals(s)) {
|
||||
return o;
|
||||
}
|
||||
|
@ -223,8 +390,8 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
private boolean switchExists(char c) {
|
||||
for (CommandLineOption op : this.options.values()) {
|
||||
for (String s : op.getCmd()) {
|
||||
for (Option op : this.options.values()) {
|
||||
for (String s : op.cmd) {
|
||||
if (s.toCharArray()[0] == c) {
|
||||
return true;
|
||||
}
|
||||
|
@ -233,10 +400,10 @@ public class CmdOptions {
|
|||
return false;
|
||||
}
|
||||
|
||||
private CommandLineOption[] getOptionBySwitches(String switches) {
|
||||
List<CommandLineOption> o = new ArrayList<CommandLineOption>();
|
||||
for (CommandLineOption op : this.options.values()) {
|
||||
for (String s : op.getCmd()) {
|
||||
private Option[] getOptionBySwitches(String switches) {
|
||||
List<Option> o = new ArrayList<CmdOptions.Option>();
|
||||
for (Option op : this.options.values()) {
|
||||
for (String s : op.cmd) {
|
||||
for (char c : switches.toCharArray()) {
|
||||
if (s.toCharArray()[0] == c) {
|
||||
o.add(op);
|
||||
|
@ -244,7 +411,7 @@ public class CmdOptions {
|
|||
}
|
||||
}
|
||||
}
|
||||
return o.toArray(new CommandLineOption[0]);
|
||||
return o.toArray(new Option[0]);
|
||||
}
|
||||
|
||||
public int parse(String[] args) {
|
||||
|
@ -275,14 +442,14 @@ public class CmdOptions {
|
|||
exit = 1;
|
||||
}
|
||||
// now parse
|
||||
CommandLineOption op;
|
||||
Option op;
|
||||
for (int a = 0; a < indices.length; ++a) {
|
||||
String o = args[indices[a]].replace(optionChar, "");
|
||||
op = this.getOptionByCommand(o);
|
||||
if (op == null) {
|
||||
if (this.combineSwitches) {
|
||||
CommandLineOption[] mop = getOptionBySwitches(o);
|
||||
for (CommandLineOption opt : mop) {
|
||||
Option[] mop = getOptionBySwitches(o);
|
||||
for (Option opt : mop) {
|
||||
opt.setSet(true);
|
||||
}
|
||||
}
|
||||
|
@ -306,12 +473,12 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
// check for possible parameters
|
||||
for (CommandLineOption o : options.values()) {
|
||||
for (Option o : options.values()) {
|
||||
for (String s : o.getValues()) {
|
||||
if (o.getPossibleParams().size() > 0
|
||||
&& !o.getPossibleParams().contains(s)) {
|
||||
if (o.possibleParams.size() > 0
|
||||
&& !o.possibleParams.contains(s)) {
|
||||
System.err.println("Parameter \"" + s + "\" for Option \""
|
||||
+ o.getName() + "\" not allowed!");
|
||||
+ o.name + "\" not allowed!");
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
|
@ -321,33 +488,31 @@ public class CmdOptions {
|
|||
}
|
||||
|
||||
// check parameter counts
|
||||
for (CommandLineOption o : options.values()) {
|
||||
if (o.getValues().size() < o.getMinParameters()
|
||||
&& o.getMinParameters() != 0
|
||||
|| o.getValues().size() > o.getMaxParameters()
|
||||
&& o.getMaxParameters() != 0
|
||||
|| o.getStepSizeParameters() != 0
|
||||
&& o.getValues().size() % o.getStepSizeParameters() != 0) {
|
||||
System.err.println(o.getName()
|
||||
for (Option o : options.values()) {
|
||||
if (o.getValues().size() < o.minParameters && o.minParameters != 0
|
||||
|| o.getValues().size() > o.maxParameters
|
||||
&& o.maxParameters != 0 || 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
|
||||
for (CommandLineOption o : options.values()) {
|
||||
if (!o.isSet() && o.isRequired()) {
|
||||
for (Option o : options.values()) {
|
||||
if (!o.set && o.required) {
|
||||
System.err
|
||||
.println(o.getName()
|
||||
.println(o.name
|
||||
+ " ("
|
||||
+ o.getName()
|
||||
+ "): has no default parameter and has to be set on commandline!");
|
||||
exit = 4;
|
||||
}
|
||||
if (!o.isSet() && o.getDefaultParameter().size() != 0)
|
||||
o.getValues().addAll(o.getDefaultParameter());
|
||||
if (!o.set && o.defaultParameter.size() != 0)
|
||||
o.values.addAll(o.defaultParameter);
|
||||
}
|
||||
if (options.get("help").isSet() || exit > 0) {
|
||||
if (options.get("help").set || exit > 0) {
|
||||
System.out.println(this.toString(true));
|
||||
if (!this.dontQuitOnError)
|
||||
System.exit(exit);
|
||||
|
@ -357,8 +522,4 @@ public class CmdOptions {
|
|||
}
|
||||
return exit;
|
||||
}
|
||||
|
||||
public static String getOptionChar() {
|
||||
return optionChar;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,233 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue