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;
14 * This class is used to define commandline option which accepts directory as parameter.
16 public class DirectoryOption extends Option<File, DirectoryOption> {
19 * This enum is used to define if directory shall exist in filesystem or not.
21 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
26 * @param description Description of the option.
28 public DirectoryOption(final String description) {
29 super(description, ParameterCount.ONE);
33 public java.lang.String describeFormat() {
34 switch (existenceType) {
36 return "Existing directory.";
38 return "Non-existing directory.";
45 * Retrieves the value of the option as a {@link File} object.
47 * @return The value of the option as a {@link File} object.
48 * @throws RuntimeException if the option does not have exactly 1 argument.
51 public File getValue() {
53 if (parameters.size() != 1)
54 throw new RuntimeException("Parameter " + description
55 + " shall have exactly 1 argument.");
57 return new File(parameters.get(0));
61 * This method sets that directory shall exist.
63 * @return This object.
65 public DirectoryOption mustExist() {
66 existenceType = ExistenceType.MUST_EXIST;
71 * This method sets that directory shall not exist.
73 * @return This object.
75 public DirectoryOption mustNotExist() {
76 existenceType = ExistenceType.MUST_NOT_EXIST;
81 * This method checks if a provided directory path is valid based on the specified existence type.
83 * @param value The directory path to validate.
84 * @return True if the directory path is valid according to the existence type, otherwise false.
87 public boolean isValid(final java.lang.String value) {
88 final File file = new File(value);
90 if (existenceType == ExistenceType.MUST_EXIST) {
91 return file.exists() && file.isDirectory();
94 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
95 return !file.exists();
98 if (existenceType == ExistenceType.DOES_NOT_MATTER) {