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 FileParameter extends Parameter {
19 private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
21 public FileParameter(final String description) {
22 super(description, ArgumentCount.SINGLE);
26 public FileParameter addAliases(final String... aliasArray) {
27 super.addAliasesProtected(aliasArray);
32 public java.lang.String describeFormat() {
33 return existanceType.description + " file";
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 FileParameter mustExist() {
47 existanceType = ExistanceType.MUST_EXIST;
51 public FileParameter mustNotExist() {
52 existanceType = ExistanceType.MUST_NOT_EXIST;
57 public FileParameter 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.isFile())
72 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
78 if (existanceType == ExistanceType.DOES_NOT_MATTER) {
80 if (file.isDirectory())