2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
14 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
15 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
17 public class DirectoryParameter extends Parameter {
19 private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
21 public DirectoryParameter(final String description) {
22 super(description, ArgumentCount.SINGLE);
26 public DirectoryParameter addAliases(final String... aliasArray) {
27 super.addAliasesProtected(aliasArray);
32 public java.lang.String describeFormat() {
33 return existanceType.description + "directory";
37 public File getValue() {
39 if (arguments.size() != 1)
40 throw new RuntimeException("Parameter " + description
41 + " shall have exactly 1 argument.");
43 return new File(arguments.get(0));
46 public DirectoryParameter mustExist() {
47 existanceType = ExistanceType.MUST_EXIST;
51 public DirectoryParameter mustNotExist() {
52 existanceType = ExistanceType.MUST_NOT_EXIST;
57 public DirectoryParameter setMandatory() {
58 setMandatoryProtected();
63 public boolean validate(final java.lang.String value) {
64 final File file = new File(value);
66 if (existanceType == ExistanceType.MUST_EXIST) {
67 if (file.exists() && file.isDirectory())
72 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
78 if (existanceType == ExistanceType.DOES_NOT_MATTER) {