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.addAliases(aliasArray);
32 public java.lang.String describeFormat() {
33 return existanceType.description + "directory";
36 public File getValue() {
38 if (arguments.size() != 1)
39 throw new RuntimeException("Parameter " + description
40 + " shall have exactly 1 argument.");
42 return new File(arguments.get(0));
45 public DirectoryParameter mustExist() {
46 existanceType = ExistanceType.MUST_EXIST;
50 public DirectoryParameter mustNotExist() {
51 existanceType = ExistanceType.MUST_NOT_EXIST;
56 public DirectoryParameter setMandatory() {
62 public boolean validate(final java.lang.String value) {
63 final File file = new File(value);
65 if (existanceType == ExistanceType.MUST_EXIST) {
66 if (file.exists() && file.isDirectory())
71 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
77 if (existanceType == ExistanceType.DOES_NOT_MATTER) {