2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
7 import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
11 import java.util.List;
12 import java.util.stream.Collectors;
15 * DirectoryOptions class represents a command-line option for one or more directories.
17 public class DirectoryOptions extends Option<List<File>, DirectoryOptions> {
20 * This enum is used to define if resource denoted by particular option parameter shall exist or not.
22 * This allows to specify for example if directory shall exist or not.
24 private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
27 * Creates a new DirectoryOptions object with the given description.
29 * @param description The description of the directory options.
31 public DirectoryOptions(final String description) {
32 super(description, ParameterCount.ONE_OR_MORE);
36 * Returns a string describing the format of the option.
38 * @return A string describing the format of the option.
41 public String describeFormat() {
42 switch (existenceType) {
44 return "One to many existing directories.";
46 return "One to many non-existing directories.";
48 return "One to many directories.";
53 * Returns the value of the option as a list of File objects.
55 * @return The value of the option as a list of File objects.
58 public List<File> getValue() {
59 return parameters.stream().map(File::new).collect(Collectors.toList());
62 public DirectoryOptions mustExist() {
63 existenceType = ExistenceType.MUST_EXIST;
67 public DirectoryOptions mustNotExist() {
68 existenceType = ExistenceType.MUST_NOT_EXIST;
74 public boolean isValid(final String value) {
75 final File file = new File(value);
77 if (existenceType == ExistenceType.MUST_EXIST) {
78 return file.exists() && file.isDirectory();
81 if (existenceType == ExistenceType.MUST_NOT_EXIST) {
82 return !file.exists();
85 if (existenceType == ExistenceType.DOES_NOT_MATTER) {