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);
25 protected static boolean validateFile(ExistenceType existenceType, String value) {
26 final File file = new File(value);
28 if (existenceType == ExistenceType.MUST_EXIST) {
29 return file.exists() && file.isFile();
32 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
33 return !file.exists();
36 if (existenceType == ExistenceType.DOES_NOT_MATTER) {
38 if (file.isDirectory())
48 public java.lang.String describeFormat() {
49 return existenceType.description + " file";
53 public File getValue() {
55 if (arguments.size() != 1)
56 throw new RuntimeException("Parameter " + description
57 + " shall have exactly 1 argument.");
59 return new File(arguments.get(0));
62 public FileParameter mustExist() {
63 existenceType = ExistenceType.MUST_EXIST;
67 public FileParameter mustNotExist() {
68 existenceType = ExistenceType.MUST_NOT_EXIST;
73 public boolean validate(final java.lang.String value) {
74 return validateFile(existenceType, value);