more usable commandline parser API
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 27 Oct 2013 15:26:43 +0000 (17:26 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 27 Oct 2013 15:26:43 +0000 (17:26 +0200)
12 files changed:
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/DirectoryParameter.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java [deleted file]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java [deleted file]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java [deleted file]
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java [deleted file]
src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java

index 831806d..22cc927 100755 (executable)
@@ -18,7 +18,7 @@ public class Parameter {
        /**
         * Indicates that at least one argument is mandatory for this parameter.
         */
-       private boolean mandatory;
+       protected boolean mandatory;
 
        private final Argument argumentType;
 
@@ -124,13 +124,6 @@ public class Parameter {
                return new File(arguments.get(0));
        }
 
-       public int getArgumentAsInteger() {
-               if (arguments.size() != 1)
-                       throw new RuntimeException("Parameter " + description
-                                       + " shall have exactly 1 argument.");
-               return Integer.parseInt(arguments.get(0));
-       }
-
        public List<File> getArgumentsAsFiles() {
                final ArrayList<File> result = new ArrayList<File>();
 
index 4ab529d..075ca11 100755 (executable)
@@ -11,6 +11,9 @@ package eu.svjatoslav.commons.commandline.parameterparser;
 
 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.StringParameter;
 
 /**
@@ -26,6 +29,24 @@ public class Parser {
                parameters.add(parameter);
        }
 
+       public DirectoryParameter createDirectoryParameter(final String description) {
+               final DirectoryParameter parameter = new DirectoryParameter(description);
+               parameters.add(parameter);
+               return parameter;
+       }
+
+       public FileParameter createFileParameter(final String description) {
+               final FileParameter parameter = new FileParameter(description);
+               parameters.add(parameter);
+               return parameter;
+       }
+
+       public IntegerParameter createIntegerParameter(final String description) {
+               final IntegerParameter parameter = new IntegerParameter(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/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
new file mode 100644 (file)
index 0000000..8607234
--- /dev/null
@@ -0,0 +1,81 @@
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
+
+public class DirectoryParameter extends Parameter implements Argument {
+
+       private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
+
+       public DirectoryParameter(final String description) {
+               super(new StringArgument(), description, ArgumentCount.SINGLE);
+       }
+
+       @Override
+       public DirectoryParameter addAliases(final String... aliasArray) {
+               super.addAliases(aliasArray);
+               return this;
+       }
+
+       @Override
+       public java.lang.String describeFormat() {
+               return existanceType.description + "directory";
+       }
+
+       public File getValue() {
+
+               if (arguments.size() != 1)
+                       throw new RuntimeException("Parameter " + description
+                                       + " shall have exactly 1 argument.");
+
+               return new File(arguments.get(0));
+       }
+
+       public DirectoryParameter mustExist() {
+               existanceType = ExistanceType.MUST_EXIST;
+               return this;
+       }
+
+       public DirectoryParameter mustNotExist() {
+               existanceType = ExistanceType.MUST_NOT_EXIST;
+               return this;
+       }
+
+       @Override
+       public DirectoryParameter setMandatory() {
+               mandatory = true;
+               return this;
+       }
+
+       @Override
+       public boolean validate(final java.lang.String value) {
+               final File file = new File(value);
+
+               if (existanceType == ExistanceType.MUST_EXIST) {
+                       if (file.exists() && file.isDirectory())
+                               return true;
+                       return false;
+               }
+
+               if (existanceType == ExistanceType.MUST_NOT_EXIST) {
+                       if (file.exists())
+                               return false;
+                       return true;
+               }
+
+               if (existanceType == ExistanceType.DOES_NOT_MATTER) {
+                       if (file.exists())
+                               if (file.isFile())
+                                       return false;
+
+                       return true;
+               }
+
+               return false;
+       }
+
+}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java
new file mode 100644 (file)
index 0000000..3b150aa
--- /dev/null
@@ -0,0 +1,14 @@
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+public enum ExistanceType {
+
+       MUST_EXIST("existing "), MUST_NOT_EXIST("not existing "), DOES_NOT_MATTER(
+                       "");
+
+       public final String description;
+
+       ExistanceType(final String description) {
+               this.description = description;
+       }
+
+}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
new file mode 100644 (file)
index 0000000..7a48e2c
--- /dev/null
@@ -0,0 +1,81 @@
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
+
+public class FileParameter extends Parameter implements Argument {
+
+       private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
+
+       public FileParameter(final String description) {
+               super(new StringArgument(), description, ArgumentCount.SINGLE);
+       }
+
+       @Override
+       public FileParameter addAliases(final String... aliasArray) {
+               super.addAliases(aliasArray);
+               return this;
+       }
+
+       @Override
+       public java.lang.String describeFormat() {
+               return existanceType.description + "file";
+       }
+
+       public File getValue() {
+
+               if (arguments.size() != 1)
+                       throw new RuntimeException("Parameter " + description
+                                       + " shall have exactly 1 argument.");
+
+               return new File(arguments.get(0));
+       }
+
+       public FileParameter mustExist() {
+               existanceType = ExistanceType.MUST_EXIST;
+               return this;
+       }
+
+       public FileParameter mustNotExist() {
+               existanceType = ExistanceType.MUST_NOT_EXIST;
+               return this;
+       }
+
+       @Override
+       public FileParameter setMandatory() {
+               mandatory = true;
+               return this;
+       }
+
+       @Override
+       public boolean validate(final java.lang.String value) {
+               final File file = new File(value);
+
+               if (existanceType == ExistanceType.MUST_EXIST) {
+                       if (file.exists() && file.isFile())
+                               return true;
+                       return false;
+               }
+
+               if (existanceType == ExistanceType.MUST_NOT_EXIST) {
+                       if (file.exists())
+                               return false;
+                       return true;
+               }
+
+               if (existanceType == ExistanceType.DOES_NOT_MATTER) {
+                       if (file.exists())
+                               if (file.isDirectory())
+                                       return false;
+
+                       return true;
+               }
+
+               return false;
+       }
+
+}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java
new file mode 100644 (file)
index 0000000..7af305b
--- /dev/null
@@ -0,0 +1,31 @@
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.IntegerArgument;
+
+public class IntegerParameter extends Parameter {
+
+       public IntegerParameter(final String description) {
+               super(new IntegerArgument(), description, ArgumentCount.SINGLE);
+       }
+
+       @Override
+       public IntegerParameter addAliases(final String... aliasArray) {
+               super.addAliases(aliasArray);
+               return this;
+       }
+
+       public int getValue() {
+               if (arguments.size() != 1)
+                       throw new RuntimeException("Parameter " + description
+                                       + " shall have exactly 1 argument.");
+               return Integer.parseInt(arguments.get(0));
+       }
+
+       @Override
+       public IntegerParameter setMandatory() {
+               mandatory = true;
+               return this;
+       }
+}
index e04b0f3..a862cbb 100644 (file)
@@ -24,4 +24,10 @@ public class StringParameter extends Parameter {
 
                return arguments.get(0);
        }
+
+       @Override
+       public StringParameter setMandatory() {
+               mandatory = true;
+               return this;
+       }
 }
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java
deleted file mode 100755 (executable)
index 2e66255..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingDirectory implements Argument {
-
-       @Override
-       public java.lang.String describeFormat() {
-               return "existing directory";
-       }
-
-       @Override
-       public boolean validate(final java.lang.String value) {
-               final File file2 = new File(value);
-               if (file2.exists() && file2.isDirectory())
-                       return true;
-               return false;
-       }
-
-}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java
deleted file mode 100755 (executable)
index d2ce6f4..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingFile implements Argument {
-
-       @Override
-       public java.lang.String describeFormat() {
-               return "existing file";
-       }
-
-       @Override
-       public boolean validate(final java.lang.String value) {
-               final File file = new File(value);
-               if (file.exists() && file.isFile())
-                       return true;
-               return false;
-       }
-
-}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java
deleted file mode 100755 (executable)
index d8a02bb..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingDirectory implements Argument {
-
-       @Override
-       public java.lang.String describeFormat() {
-               return "non existing directory";
-       }
-
-       @Override
-       public boolean validate(final java.lang.String value) {
-               final File file4 = new File(value);
-               if (!file4.exists())
-                       return true;
-               return false;
-       }
-
-}
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java
deleted file mode 100755 (executable)
index f3e441e..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingFile implements Argument {
-
-       @Override
-       public java.lang.String describeFormat() {
-               return "non existing file";
-       }
-
-       @Override
-       public boolean validate(final java.lang.String value) {
-               final File file3 = new File(value);
-               if (!file3.exists())
-                       return true;
-               return false;
-       }
-
-}
index e8d1078..c61533e 100755 (executable)
@@ -31,8 +31,9 @@ public class ParserTest {
        public void testParse() {
 
                // define allowed parameters
-               final StringParameter helpParameter = parser.createStringParameter(
-                               "Show help screen").addAliases("--help", "-h");
+               final StringParameter helpParameter = parser
+                               .createStringParameter("Show help screen")
+                               .addAliases("--help", "-h").setMandatory();
 
                final StringParameter compileParameter = parser.createStringParameter(
                                "Compile code").addAliases("--compile", "-c");