more usable commandline parser API
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / DirectoryParameter.java
1 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
2
3 import java.io.File;
4
5 import eu.svjatoslav.commons.commandline.parameterparser.Argument;
6 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
7 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
8 import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
9
10 public class DirectoryParameter extends Parameter implements Argument {
11
12         private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
13
14         public DirectoryParameter(final String description) {
15                 super(new StringArgument(), description, ArgumentCount.SINGLE);
16         }
17
18         @Override
19         public DirectoryParameter addAliases(final String... aliasArray) {
20                 super.addAliases(aliasArray);
21                 return this;
22         }
23
24         @Override
25         public java.lang.String describeFormat() {
26                 return existanceType.description + "directory";
27         }
28
29         public File getValue() {
30
31                 if (arguments.size() != 1)
32                         throw new RuntimeException("Parameter " + description
33                                         + " shall have exactly 1 argument.");
34
35                 return new File(arguments.get(0));
36         }
37
38         public DirectoryParameter mustExist() {
39                 existanceType = ExistanceType.MUST_EXIST;
40                 return this;
41         }
42
43         public DirectoryParameter mustNotExist() {
44                 existanceType = ExistanceType.MUST_NOT_EXIST;
45                 return this;
46         }
47
48         @Override
49         public DirectoryParameter setMandatory() {
50                 mandatory = true;
51                 return this;
52         }
53
54         @Override
55         public boolean validate(final java.lang.String value) {
56                 final File file = new File(value);
57
58                 if (existanceType == ExistanceType.MUST_EXIST) {
59                         if (file.exists() && file.isDirectory())
60                                 return true;
61                         return false;
62                 }
63
64                 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
65                         if (file.exists())
66                                 return false;
67                         return true;
68                 }
69
70                 if (existanceType == ExistanceType.DOES_NOT_MATTER) {
71                         if (file.exists())
72                                 if (file.isFile())
73                                         return false;
74
75                         return true;
76                 }
77
78                 return false;
79         }
80
81 }