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;
13 * This class represents commandline option which accepts exactly one parameter
16 public class FileOption extends Option<File, FileOption> {
18 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
20 public FileOption(final String description) {
21 super(description, ParameterCount.ONE);
24 protected static boolean validateFile(ExistenceType existenceType, String value) {
25 final File file = new File(value);
27 if (existenceType == ExistenceType.MUST_EXIST) {
28 return file.exists() && file.isFile();
31 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
32 return !file.exists();
35 if (existenceType == ExistenceType.DOES_NOT_MATTER) {
37 if (file.isDirectory())
47 public java.lang.String describeFormat() {
48 switch (existenceType) {
50 return "Existing file.";
52 return "Non-existing file.";
59 public File getValue() {
61 if (parameters.size() != 1)
62 throw new RuntimeException("Parameter " + description
63 + " shall have exactly 1 argument.");
65 return new File(parameters.get(0));
69 * This method sets that file shall exist.
71 * @return This object.
73 public FileOption mustExist() {
74 existenceType = ExistenceType.MUST_EXIST;
79 * This method sets that file shall not exist.
81 * @return This object.
83 public FileOption mustNotExist() {
84 existenceType = ExistenceType.MUST_NOT_EXIST;
89 public boolean isValid(final java.lang.String value) {
90 return validateFile(existenceType, value);