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 DirectoryOption extends Option<File, DirectoryOption> {
14 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
16 public DirectoryOption(final String description) {
17 super(description, ParameterCount.SINGLE);
21 public java.lang.String describeFormat() {
22 return existenceType.description + " directory";
26 public File getValue() {
28 if (parameters.size() != 1)
29 throw new RuntimeException("Parameter " + description
30 + " shall have exactly 1 argument.");
32 return new File(parameters.get(0));
35 public DirectoryOption mustExist() {
36 existenceType = ExistenceType.MUST_EXIST;
40 public DirectoryOption mustNotExist() {
41 existenceType = ExistenceType.MUST_NOT_EXIST;
46 public boolean isValid(final java.lang.String value) {
47 final File file = new File(value);
49 if (existenceType == ExistenceType.MUST_EXIST) {
50 return file.exists() && file.isDirectory();
53 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
54 return !file.exists();
57 if (existenceType == ExistenceType.DOES_NOT_MATTER) {