From: Svjatoslav Agejenko Date: Fri, 11 Feb 2022 06:37:26 +0000 (+0200) Subject: Added ability to provide multiple directories as commandline argument X-Git-Tag: svjatoslavcommons-1.9~4 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=cf53b86cd21c6d25df672dd717f2425755dd88eb;p=svjatoslav_commons.git Added ability to provide multiple directories as commandline argument --- 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 index c890f7d..a7b872a 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java @@ -19,7 +19,7 @@ public class DirectoryParameter extends Parameter { @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 index 0000000..a35976d --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java @@ -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, 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 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; + } + +}