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 final File file = new File(value);
54 if (existenceType == ExistenceType.MUST_EXIST) {
55 return file.exists() && file.isFile();
58 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
59 return !file.exists();
62 if (existenceType == ExistenceType.DOES_NOT_MATTER) {
64 if (file.isDirectory())