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.ParameterCount;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
12 public class FileOption extends Option<File, FileOption> {
14 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
16 public FileOption(final String description) {
17 super(description, ParameterCount.ONE);
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 switch (existenceType) {
46 return "Existing file.";
48 return "Non-existing file.";
55 public File getValue() {
57 if (parameters.size() != 1)
58 throw new RuntimeException("Parameter " + description
59 + " shall have exactly 1 argument.");
61 return new File(parameters.get(0));
64 public FileOption mustExist() {
65 existenceType = ExistenceType.MUST_EXIST;
69 public FileOption mustNotExist() {
70 existenceType = ExistenceType.MUST_NOT_EXIST;
75 public boolean isValid(final java.lang.String value) {
76 return validateFile(existenceType, value);