2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
12 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
13 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
17 public class FileParameter extends Parameter<File, FileParameter> {
19 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
21 public FileParameter(final String description) {
22 super(description, ArgumentCount.SINGLE);
26 public java.lang.String describeFormat() {
27 return existenceType.description + " file";
31 public File getValue() {
33 if (arguments.size() != 1)
34 throw new RuntimeException("Parameter " + description
35 + " shall have exactly 1 argument.");
37 return new File(arguments.get(0));
40 public FileParameter mustExist() {
41 existenceType = ExistenceType.MUST_EXIST;
45 public FileParameter mustNotExist() {
46 existenceType = ExistenceType.MUST_NOT_EXIST;
51 public boolean validate(final java.lang.String value) {
52 return validateFile(existenceType, value);
55 protected static boolean validateFile(ExistenceType existenceType, String value) {
56 final File file = new File(value);
58 if (existenceType == ExistenceType.MUST_EXIST) {
59 return file.exists() && file.isFile();
62 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
63 return !file.exists();
66 if (existenceType == ExistenceType.DOES_NOT_MATTER) {
68 if (file.isDirectory())