+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;
+ }
+
+}