2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
7 import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter;
12 public class FileParameter extends Parameter<File, FileParameter> {
14 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
16 public FileParameter(final String description) {
17 super(description, ArgumentCount.SINGLE);
20 protected static boolean validateFile(ExistenceType existenceType, String value) {
21 final File file = new File(value);
23 if (existenceType == ExistenceType.MUST_EXIST) {
24 return file.exists() && file.isFile();
27 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
28 return !file.exists();
31 if (existenceType == ExistenceType.DOES_NOT_MATTER) {
33 if (file.isDirectory())
43 public java.lang.String describeFormat() {
44 return existenceType.description + " file";
48 public File getValue() {
50 if (arguments.size() != 1)
51 throw new RuntimeException("Parameter " + description
52 + " shall have exactly 1 argument.");
54 return new File(arguments.get(0));
57 public FileParameter mustExist() {
58 existenceType = ExistenceType.MUST_EXIST;
62 public FileParameter mustNotExist() {
63 existenceType = ExistenceType.MUST_NOT_EXIST;
68 public boolean validate(final java.lang.String value) {
69 return validateFile(existenceType, value);