more usable commandline parser API
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / DirectoryParameter.java
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;
+       }
+
+}