1 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
5 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
6 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
8 public class FileParameter extends Parameter {
10 private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
12 public FileParameter(final String description) {
13 super(description, ArgumentCount.SINGLE);
17 public FileParameter addAliases(final String... aliasArray) {
18 super.addAliases(aliasArray);
23 public java.lang.String describeFormat() {
24 return existanceType.description + "file";
27 public File getValue() {
29 if (arguments.size() != 1)
30 throw new RuntimeException("Parameter " + description
31 + " shall have exactly 1 argument.");
33 return new File(arguments.get(0));
36 public FileParameter mustExist() {
37 existanceType = ExistanceType.MUST_EXIST;
41 public FileParameter mustNotExist() {
42 existanceType = ExistanceType.MUST_NOT_EXIST;
47 public FileParameter setMandatory() {
53 public boolean validate(final java.lang.String value) {
54 final File file = new File(value);
56 if (existanceType == ExistanceType.MUST_EXIST) {
57 if (file.exists() && file.isFile())
62 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
68 if (existanceType == ExistanceType.DOES_NOT_MATTER) {
70 if (file.isDirectory())