further improvements to commandline parser API
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 27 Oct 2013 19:17:21 +0000 (21:17 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 27 Oct 2013 19:17:21 +0000 (21:17 +0200)
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java

index 22cc927..d9e78da 100755 (executable)
@@ -117,13 +117,6 @@ public class Parameter {
                return buffer.toString();
        }
 
-       public File getArgumentAsFile() {
-               if (arguments.size() != 1)
-                       throw new RuntimeException("Parameter " + description
-                                       + " shall have exactly 1 argument.");
-               return new File(arguments.get(0));
-       }
-
        public List<File> getArgumentsAsFiles() {
                final ArrayList<File> result = new ArrayList<File>();
 
index 075ca11..f5ecf72 100755 (executable)
@@ -14,6 +14,7 @@ import java.util.ArrayList;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.IntegerParameter;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
 
 /**
@@ -29,6 +30,14 @@ public class Parser {
                parameters.add(parameter);
        }
 
+       public Parameter createCustomParameter(final Argument paramaterType,
+                       final String description) {
+               final Parameter parameter = new Parameter(paramaterType, description,
+                               ArgumentCount.SINGLE);
+               parameters.add(parameter);
+               return parameter;
+       }
+
        public DirectoryParameter createDirectoryParameter(final String description) {
                final DirectoryParameter parameter = new DirectoryParameter(description);
                parameters.add(parameter);
@@ -47,6 +56,12 @@ public class Parser {
                return parameter;
        }
 
+       public NullParameter createNullParameter(final String description) {
+               final NullParameter parameter = new NullParameter(description);
+               parameters.add(parameter);
+               return parameter;
+       }
+
        public StringParameter createStringParameter(final String description) {
                final StringParameter parameter = new StringParameter(description);
                parameters.add(parameter);
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java
new file mode 100644 (file)
index 0000000..cadadd1
--- /dev/null
@@ -0,0 +1,27 @@
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+
+public class NullParameter extends Parameter {
+
+       public NullParameter(final String description) {
+               super(null, description, ArgumentCount.SINGLE);
+       }
+
+       @Override
+       public NullParameter addAliases(final String... aliasArray) {
+               super.addAliases(aliasArray);
+               return this;
+       }
+
+       public boolean getValue() {
+               return isParameterSpecified();
+       }
+
+       @Override
+       public NullParameter setMandatory() {
+               mandatory = true;
+               return this;
+       }
+}