Added ability to provide multiple directories as commandline argument
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 11 Feb 2022 06:37:26 +0000 (08:37 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 11 Feb 2022 06:37:26 +0000 (08:37 +0200)
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java [new file with mode: 0755]

index c890f7d..a7b872a 100755 (executable)
@@ -19,7 +19,7 @@ public class DirectoryParameter extends Parameter<File, DirectoryParameter> {
 
     @Override
     public java.lang.String describeFormat() {
-        return existenceType.description + "directory";
+        return existenceType.description + " directory";
     }
 
     @Override
diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java
new file mode 100755 (executable)
index 0000000..a35976d
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
+ */
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+
+import java.io.File;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class DirectoryParameters extends Parameter<List<File>, DirectoryParameters> {
+
+    private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+    public DirectoryParameters(final String description) {
+        super(description, ArgumentCount.MULTI);
+    }
+
+    @Override
+    public String describeFormat() {
+        return existenceType.description + " directories";
+    }
+
+    @Override
+    public List<File> getValue() {
+        return arguments.stream().map(File::new).collect(Collectors.toList());
+    }
+
+    public DirectoryParameters mustExist() {
+        existenceType = ExistenceType.MUST_EXIST;
+        return this;
+    }
+
+    public DirectoryParameters mustNotExist() {
+        existenceType = ExistenceType.MUST_NOT_EXIST;
+        return this;
+    }
+
+    @Override
+    public boolean validate(final String value) {
+        final File file = new File(value);
+
+        if (existenceType == ExistenceType.MUST_EXIST) {
+            return file.exists() && file.isDirectory();
+        }
+
+        if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+            return !file.exists();
+        }
+
+        if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+            if (file.exists())
+                if (file.isFile())
+                    return false;
+
+            return true;
+        }
+
+        return false;
+    }
+
+}