Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Belle2018-08-29 05:28:36 -0400
committerJan Belle2018-08-29 06:12:55 -0400
commit3403977e874c1c6de888c66568227ccf8241ecfc (patch)
tree4d0b416685459db9d9ca2391fbc2968974927d1c
parent7b98005ca20f56ac60807ee6c47c6c701d674a64 (diff)
downloadorg.eclipse.etrice-3403977e874c1c6de888c66568227ccf8241ecfc.tar.gz
org.eclipse.etrice-3403977e874c1c6de888c66568227ccf8241ecfc.tar.xz
org.eclipse.etrice-3403977e874c1c6de888c66568227ccf8241ecfc.zip
[generator.base] generator options abstraction
* simple command line parser for options Change-Id: I711184d68b00cc2854cf0be489e6a95808bc8cdd
-rw-r--r--.gitignore1
-rw-r--r--plugins/org.eclipse.etrice.generator.base/.classpath7
-rw-r--r--plugins/org.eclipse.etrice.generator.base/.project28
-rw-r--r--plugins/org.eclipse.etrice.generator.base/.settings/org.eclipse.jdt.core.prefs7
-rw-r--r--plugins/org.eclipse.etrice.generator.base/META-INF/MANIFEST.MF16
-rw-r--r--plugins/org.eclipse.etrice.generator.base/build.gradle4
-rw-r--r--plugins/org.eclipse.etrice.generator.base/build.properties4
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Arguments.java115
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/BooleanOption.java31
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/EnumOption.java42
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IOptionsModule.java32
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IntegerOption.java40
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Option.java119
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Options.java88
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/StringOption.java34
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParseException.java36
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParser.java95
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/HelpFormatter.java47
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/ICommandLineParser.java46
-rw-r--r--plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/IHelpFormatter.java36
-rw-r--r--pom.xml1
-rw-r--r--settings.gradle1
22 files changed, 830 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 07d680877..d5fa40b80 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,6 +4,7 @@ gerritKeys.pub
*._trace
*.xtendbin
language.settings.xml
+bin/
target/
xtend-gen/
build/
diff --git a/plugins/org.eclipse.etrice.generator.base/.classpath b/plugins/org.eclipse.etrice.generator.base/.classpath
new file mode 100644
index 000000000..eca7bdba8
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/plugins/org.eclipse.etrice.generator.base/.project b/plugins/org.eclipse.etrice.generator.base/.project
new file mode 100644
index 000000000..583625861
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.etrice.generator.base</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/plugins/org.eclipse.etrice.generator.base/.settings/org.eclipse.jdt.core.prefs b/plugins/org.eclipse.etrice.generator.base/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 000000000..0c68a61dc
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/plugins/org.eclipse.etrice.generator.base/META-INF/MANIFEST.MF b/plugins/org.eclipse.etrice.generator.base/META-INF/MANIFEST.MF
new file mode 100644
index 000000000..f0c909e0c
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/META-INF/MANIFEST.MF
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-SymbolicName: org.eclipse.etrice.generator.base
+Bundle-Version: 2.0.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Require-Bundle: org.eclipse.emf.ecore,
+ com.google.inject;bundle-version="3.0.0",
+ org.eclipse.xtext.generator,
+ org.eclipse.xtext.util,
+ org.eclipse.xtext,
+ org.eclipse.core.resources,
+ org.eclipse.core.runtime,
+ org.apache.commons.io
+Export-Package: org.eclipse.etrice.generator.base.args,
+ org.eclipse.etrice.generator.base.cli
+Bundle-Name: Base Generator
diff --git a/plugins/org.eclipse.etrice.generator.base/build.gradle b/plugins/org.eclipse.etrice.generator.base/build.gradle
new file mode 100644
index 000000000..3789c53e8
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/build.gradle
@@ -0,0 +1,4 @@
+dependencies {
+ compile "commons-io:commons-io:$versions.commons_io"
+ compile "org.eclipse.xtext:org.eclipse.xtext:$versions.xtext"
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.etrice.generator.base/build.properties b/plugins/org.eclipse.etrice.generator.base/build.properties
new file mode 100644
index 000000000..34d2e4d2d
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Arguments.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Arguments.java
new file mode 100644
index 000000000..bc49ddbde
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Arguments.java
@@ -0,0 +1,115 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Encapsulates a set of arguments.
+ */
+public class Arguments {
+ private Options options;
+ private HashMap<String, Object> option2Arg;
+ private List<String> fileList;
+
+ /**
+ * Creates a new set of arguments initialized to their default values.
+ *
+ * @param options the options to create the arguments from
+ */
+ public Arguments(Options options) {
+ this.options = options;
+ option2Arg = new HashMap<>();
+ fileList = new ArrayList<>();
+
+ for(Option<?> option: options) {
+ option2Arg.put(option.getName(), option.getDefaultValue());
+ }
+ }
+
+ /**
+ * Queries an argument by its name.
+ *
+ * @param name the name of the option
+ * @return the argument
+ */
+ public Object get(String name) throws IllegalArgumentException {
+ Object value = option2Arg.get(name);
+ if(value == null) {
+ throw new IllegalArgumentException("option " + name + " not recognized");
+ }
+ return value;
+ }
+
+ /**
+ * Queries an argument.
+ *
+ * @param option the option of the argument
+ * @return the argument
+ */
+ public <T> T get(Option<T> option) throws IllegalArgumentException {
+ return option.getType().cast(get(option.getName()));
+ }
+
+ /**
+ * Sets an argument to the passed value.
+ *
+ * @param name the name of the option
+ * @param value the value to be set
+ */
+ public void set(String name, Object value) throws IllegalArgumentException {
+ Option<?> opt = options.get(name);
+ if(!opt.checkValue(value)) {
+ throw new IllegalArgumentException("value " + value.toString() + " is not assignable to Option " + opt.toString());
+ }
+ option2Arg.put(name, value);
+ }
+
+ /**
+ * Sets an argument to the passed value.
+ *
+ * @param option the option
+ * @param value the value to be set
+ */
+ public <T> void set(Option<T> option, T value) throws IllegalArgumentException {
+ set(option.getName(), value);
+ }
+
+ /**
+ * Returns the list of input file paths.
+ *
+ * @return the list of paths
+ */
+ public List<String> getFiles() {
+ return fileList;
+ }
+
+ /**
+ * Returns the options for the arguments.
+ *
+ * @return options the options
+ */
+ public Options getOptions() {
+ return options;
+ }
+
+ @Override
+ public String toString() {
+ return option2Arg.toString() + ", " + fileList.toString();
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/BooleanOption.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/BooleanOption.java
new file mode 100644
index 000000000..65d98d23b
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/BooleanOption.java
@@ -0,0 +1,31 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+/**
+ * Describes an option that takes a {@link Boolean} value.
+ */
+public class BooleanOption extends Option<Boolean> {
+
+ public BooleanOption(String name, String parameterName, String description, boolean defaultValue) {
+ super(Boolean.class, name, parameterName, description, defaultValue);
+ }
+
+ @Override
+ public Boolean parseValue(String str) {
+ return Boolean.parseBoolean(str);
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/EnumOption.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/EnumOption.java
new file mode 100644
index 000000000..c6033b591
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/EnumOption.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+/**
+ * Describes an option that takes an {@link Enum} value.
+ */
+public class EnumOption<T extends Enum<?>> extends Option<T> {
+
+ private T[] enumConstants;
+
+ /**
+ * @see Option#Option
+ */
+ public EnumOption(Class<T> enumClass, String name, String parameterName, String description, T defaultValue) {
+ super(enumClass, name, parameterName, description, defaultValue);
+ enumConstants = enumClass.getEnumConstants();
+ }
+
+ @Override
+ public T parseValue(String str) throws IllegalArgumentException {
+ for(T c: enumConstants) {
+ if(c.toString().equalsIgnoreCase(str)) {
+ return c;
+ }
+ }
+ throw new IllegalArgumentException("Argument " + str + " not allowed for option " + getName());
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IOptionsModule.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IOptionsModule.java
new file mode 100644
index 000000000..eff703a55
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IOptionsModule.java
@@ -0,0 +1,32 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+import java.util.List;
+
+/**
+ * Describes options that can be used to create a new {@link Options} set.
+ */
+public interface IOptionsModule {
+
+ /**
+ * Configures options on the passed collection.
+ *
+ * @param options the collection of options to be configured
+ */
+ void configure(List<Option<?>> options);
+
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IntegerOption.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IntegerOption.java
new file mode 100644
index 000000000..1ac1c6f97
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/IntegerOption.java
@@ -0,0 +1,40 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+/**
+ * Describes an option that takes an {@link Integer} value.
+ */
+public class IntegerOption extends Option<Integer> {
+
+ /**
+ * @see Option#Option
+ */
+ public IntegerOption(String name, String parameterName, String description, int defaultValue) {
+ super(Integer.class, name, parameterName, description, defaultValue);
+ }
+
+ @Override
+ public Integer parseValue(String str) throws IllegalArgumentException {
+ try {
+ return Integer.parseInt(str);
+ }
+ catch(NumberFormatException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Option.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Option.java
new file mode 100644
index 000000000..7ee77c00d
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Option.java
@@ -0,0 +1,119 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+/**
+ * Describes an option that takes a value of the passed type.
+ * Options are immutable.
+ *
+ * @param T the type of the values for this option
+ */
+public class Option<T> {
+
+ private final Class<T> type;
+ private final String name;
+ private final String parameterName;
+ private final String description;
+ private final T defaultValue;
+
+ /**
+ * Creates a new option.
+ *
+ * @param type the type of the values for this option
+ * @param name the name of this option
+ * @param parameterName the name of the parameter of this option
+ * @param description a description for this option
+ * @param defaultValue a default value for this option
+ */
+ public Option(Class<T> type, String name, String parameterName, String description, T defaultValue) {
+ this.type = type;
+ this.name = name;
+ this.parameterName = parameterName;
+ this.description = description;
+ this.defaultValue = defaultValue;
+ }
+
+ /**
+ * Returns the type of the values for this option.
+ *
+ * @return the type
+ */
+ public final Class<T> getType() {
+ return type;
+ }
+
+ /**
+ * Returns the name of this option.
+ * Its name uniquely identifies an option.
+ *
+ * @return the name
+ */
+ public final String getName() {
+ return name;
+ }
+
+ /**
+ * Returns the parameter name of this option.
+ *
+ * @return the parameter name
+ */
+ public final String getParameterName() {
+ return parameterName;
+ }
+
+ /**
+ * Returns a description of this option.
+ *
+ * @return the description
+ */
+ public final String getDescription() {
+ return description;
+ }
+
+ /**
+ * Returns the default value for this option.
+ *
+ * @return the default value
+ */
+ public final T getDefaultValue() {
+ return defaultValue;
+ }
+
+ /**
+ * Checks whether the passed value is assignable to this option.
+ *
+ * @param value the value to be checked
+ * @return whether the value is assignable to this option
+ */
+ public boolean checkValue(Object value) {
+ return type.isInstance(value);
+ }
+
+ /**
+ * Parses a value for this option from the passed string.
+ *
+ * @param str the string to be parsed
+ * @return the parsed value
+ */
+ public T parseValue(String str) throws IllegalArgumentException, UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public String toString() {
+ return getName();
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Options.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Options.java
new file mode 100644
index 000000000..bb3cd7d73
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/Options.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Encapsulates an immutable set of options.
+ */
+public class Options implements Iterable<Option<?>> {
+
+ private LinkedHashMap<String, Option<?>> name2Option;
+
+ /**
+ * Creates a new set of options that contains the passed options.
+ *
+ * @param options the options
+ */
+ public Options(Option<?>... options) {
+ init(Arrays.asList(options));
+ }
+
+ /**
+ * Creates a new set of options that contains the passed options.
+ *
+ * @param options the options
+ */
+ public Options(List<Option<?>> options) {
+ init(options);
+ }
+
+ /**
+ * Creates a new set of options that contains the options configured by the passed module.
+ *
+ * @param module the options module
+ */
+ public Options(IOptionsModule module) {
+ List<Option<?>> options = new LinkedList<>();
+ module.configure(options);
+ init(options);
+ }
+
+ /**
+ * Queries an option by its name.
+ *
+ * @param name the name of the requested option
+ */
+ public final Option<?> get(String name) throws IllegalArgumentException {
+ Option<?> opt = name2Option.get(name);
+ if(opt == null) {
+ throw new IllegalArgumentException("Option " + name + " not recognized");
+ }
+ return opt;
+ }
+
+ @Override
+ public final Iterator<Option<?>> iterator() {
+ return Collections.unmodifiableCollection(name2Option.values()).iterator();
+ }
+
+ @Override
+ public String toString() {
+ return name2Option.values().toString();
+ }
+
+ private final void init(List<Option<?>> options) {
+ name2Option = new LinkedHashMap<>(options.size());
+ options.forEach(opt -> name2Option.put(opt.getName(), opt));
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/StringOption.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/StringOption.java
new file mode 100644
index 000000000..dbecaca95
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/args/StringOption.java
@@ -0,0 +1,34 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.args;
+
+/**
+ * Describes an option that takes a {@link String} value.
+ */
+public class StringOption extends Option<String> {
+
+ /**
+ * @see Option#Option
+ */
+ public StringOption(String name, String parameterName, String description, String defaultValue) {
+ super(String.class, name, parameterName, description, defaultValue);
+ }
+
+ @Override
+ public String parseValue(String str) {
+ return str;
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParseException.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParseException.java
new file mode 100644
index 000000000..4c0163a4d
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParseException.java
@@ -0,0 +1,36 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.cli;
+
+/**
+ * An exception that indicates a command line parsing error.
+ */
+public class CommandLineParseException extends IllegalArgumentException {
+
+ private static final long serialVersionUID = 1L;
+
+ public CommandLineParseException(String message) {
+ super(message);
+ }
+
+ public CommandLineParseException(Throwable cause) {
+ super(cause);
+ }
+
+ public CommandLineParseException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParser.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParser.java
new file mode 100644
index 000000000..3ad8a5efa
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/CommandLineParser.java
@@ -0,0 +1,95 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.cli;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.eclipse.etrice.generator.base.args.Arguments;
+import org.eclipse.etrice.generator.base.args.Option;
+import org.eclipse.etrice.generator.base.args.Options;
+
+/**
+ * Simple implementation of a command line parser.
+ * Options must start with a prefixed single dash and parameters must be separated with a space.
+ * Everything remaining is interpreted as input files.
+ * <br>
+ * Example:
+ * -genDir myGenDir MyFile.txt
+ *
+ */
+public class CommandLineParser implements ICommandLineParser {
+
+ public CommandLineParser() {
+ }
+
+ @Override
+ public Arguments parseArgs(Options options, List<String> args) throws CommandLineParseException {
+ Arguments parameters = new Arguments(options);
+ List<String> nArgs = normalize(args);
+ ListIterator<String> iterator = nArgs.listIterator();
+
+ while(iterator.hasNext()) {
+ String str = iterator.next().trim();
+ if(str.startsWith("-")) {
+ Option<?> opt = parseOption(options, str);
+ Object value = parseValue(opt, iterator);
+ parameters.set(opt.getName(), value);
+ }
+ else {
+ parameters.getFiles().add(str);
+ }
+ }
+
+ return parameters;
+ }
+
+ private List<String> normalize(List<String> args) throws CommandLineParseException {
+ List<String> nArgs = new LinkedList<>(args);
+ return nArgs;
+ }
+
+ private Option<?> parseOption(Options options, String str) throws CommandLineParseException {
+ try {
+ Option<?> opt = options.get(str.substring(1));
+ return opt;
+ }
+ catch(IllegalArgumentException e) {
+ throw new CommandLineParseException(e);
+ }
+ }
+
+ private Object parseValue(Option<?> opt, ListIterator<String> iterator) throws CommandLineParseException {
+ if(opt.getType().equals(Boolean.class)) {
+ return true;
+ }
+ else if(iterator.hasNext()) {
+ try {
+ return opt.parseValue(iterator.next());
+ }
+ catch(IllegalArgumentException e) {
+ throw new CommandLineParseException(e);
+ }
+ catch(UnsupportedOperationException e) {
+ throw new CommandLineParseException(e);
+ }
+ }
+ else {
+ throw new CommandLineParseException("Expected one argument for option " + opt.getName());
+ }
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/HelpFormatter.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/HelpFormatter.java
new file mode 100644
index 000000000..777930841
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/HelpFormatter.java
@@ -0,0 +1,47 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.cli;
+
+import java.util.Formatter;
+
+import org.eclipse.etrice.generator.base.args.Option;
+import org.eclipse.etrice.generator.base.args.Options;
+
+/**
+ * Simple implementation for command line help formatting.
+ */
+public class HelpFormatter implements IHelpFormatter {
+
+ public HelpFormatter() {
+ }
+
+ @Override
+ public String getHelp(Options options) {
+ try(Formatter formatter = new Formatter()) {
+ formatter.format("Usage: [options] files...%n");
+ formatter.format("Options:%n");
+ for(Option<?> opt: options) {
+ String optStr = "-" + opt.getName();
+ if(!opt.getType().equals(Boolean.class)) {
+ optStr += " <" + opt.getParameterName() + ">";
+ }
+ formatter.format(" %-30s %s%n", optStr, opt.getDescription());
+ }
+ return formatter.toString();
+ }
+ }
+
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/ICommandLineParser.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/ICommandLineParser.java
new file mode 100644
index 000000000..ec7a49aa7
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/ICommandLineParser.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.cli;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.etrice.generator.base.args.Arguments;
+import org.eclipse.etrice.generator.base.args.Options;
+
+import com.google.inject.ImplementedBy;
+
+/**
+ * A command line parser provides methods to parse command line arguments.
+ */
+@ImplementedBy(CommandLineParser.class)
+public interface ICommandLineParser {
+
+ /**
+ * Parses the specified command line arguments using the passed options.
+ *
+ * @param options the options
+ * @param args the command line arguments
+ */
+ Arguments parseArgs(Options options, List<String> args) throws CommandLineParseException;
+
+ /**
+ * @see ICommandLineParser#parseArgs(Options, List)
+ */
+ default Arguments parseArgs(Options options, String[] args) throws CommandLineParseException {
+ return parseArgs(options, Arrays.asList(args));
+ }
+}
diff --git a/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/IHelpFormatter.java b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/IHelpFormatter.java
new file mode 100644
index 000000000..9d592ecd4
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator.base/src/org/eclipse/etrice/generator/base/cli/IHelpFormatter.java
@@ -0,0 +1,36 @@
+/*******************************************************************************
+* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
+* All rights reserved.
+*
+* This program and the accompanying materials are made
+* available under the terms of the Eclipse Public License 2.0
+* which is available at https://www.eclipse.org/legal/epl-2.0/
+*
+* SPDX-License-Identifier: EPL-2.0
+*
+* CONTRIBUTORS:
+* Jan Belle (initial contribution)
+*
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base.cli;
+
+import org.eclipse.etrice.generator.base.args.Options;
+
+import com.google.inject.ImplementedBy;
+
+/**
+ * Formats help messages for {@link Options}.
+ */
+@ImplementedBy(HelpFormatter.class)
+public interface IHelpFormatter {
+
+ /**
+ * Produces a help message for the passed options.
+ *
+ * @param options the options
+ * @return the help message
+ */
+ String getHelp(Options options);
+
+}
diff --git a/pom.xml b/pom.xml
index 43bf008e7..f3c238a27 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,7 @@
<module>plugins/org.eclipse.etrice.etunit.converter</module>
<module>plugins/org.eclipse.etrice.expressions.ui</module>
<module>plugins/org.eclipse.etrice.generator</module>
+ <module>plugins/org.eclipse.etrice.generator.base</module>
<module>plugins/org.eclipse.etrice.generator.c</module>
<module>plugins/org.eclipse.etrice.generator.config</module>
<module>plugins/org.eclipse.etrice.generator.cpp</module>
diff --git a/settings.gradle b/settings.gradle
index ea0b4852a..91bd36c77 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -8,6 +8,7 @@ include 'plugins:org.eclipse.etrice.core.common',
'plugins:org.eclipse.etrice.core.etmap',
'plugins:org.eclipse.etrice.generator.fsm',
'plugins:org.eclipse.etrice.generator',
+ 'plugins:org.eclipse.etrice.generator.base',
'plugins:org.eclipse.etrice.generator.config',
'plugins:org.eclipse.etrice.generator.doc',
'plugins:org.eclipse.etrice.generator.gnuplot',

Back to the top