From 1ec32cc502ebc36b1850670b648d715b57257723 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 17 Oct 2023 21:59:39 +0300 Subject: [PATCH] Refactor ParameterCount enum and update option descriptions Refactored the ParameterCount enum for clearer representation - 'SINGLE' and 'MULTI' changed to 'ONE' and 'ONE_OR_MORE'. Also updated various option descriptions for better readability and consistency. The describeFormat() methods in different file/directory options now contain clear messages about the existence of the resource, improving user experience. --- .../cli_helper/parameter_parser/Option.java | 4 ++-- .../parameter_parser/ParameterCount.java | 12 +++++++----- .../parameter/DirectoryOption.java | 11 +++++++++-- .../parameter/DirectoryOptions.java | 11 +++++++++-- .../parameter_parser/parameter/ExistenceType.java | 15 +++------------ .../parameter_parser/parameter/FileOption.java | 11 +++++++++-- .../parameter_parser/parameter/FileOptions.java | 13 ++++++++++--- .../parameter_parser/parameter/IntegerOption.java | 4 ++-- .../parameter_parser/parameter/NullOption.java | 2 +- .../parameter_parser/parameter/StringOption.java | 6 +++--- .../parameter_parser/parameter/StringOptions.java | 4 ++-- 11 files changed, 57 insertions(+), 36 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java index cb36966..9bcea3f 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java @@ -85,7 +85,7 @@ public abstract class Option { // check if multiple arguments are allowed if ((!parameters.isEmpty()) - && (parameterCount.equals(ParameterCount.SINGLE))) { + && (parameterCount.equals(ParameterCount.ONE))) { System.out .println("Error! Only single parameter is allowed for argument(s): " + getAliasesAsString()); @@ -131,7 +131,7 @@ public abstract class Option { .append(describeFormat()) .append(")"); - if (parameterCount.equals(ParameterCount.MULTI)) + if (parameterCount.equals(ParameterCount.ONE_OR_MORE)) result.append("..."); } result.append("\n"); diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java index bcaceec..0d74b3f 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java @@ -5,7 +5,8 @@ package eu.svjatoslav.commons.cli_helper.parameter_parser; /** - * This enum is used to define how many parameters con be given to particular option. + * This enumeration is used to define how many parameters + * does particular commandline option accept. */ public enum ParameterCount { @@ -13,13 +14,14 @@ public enum ParameterCount { * Option has no parameters. */ NONE, + /** - * Option has exactly one parameter. + * Option has exactly one mandatory parameter. */ - SINGLE, + ONE, /** - * Option can have any amount of parameters. + * Option must have one or more parameters. */ - MULTI + ONE_OR_MORE } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java index 8f97a8b..718aa09 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java @@ -14,12 +14,19 @@ public class DirectoryOption extends Option { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; public DirectoryOption(final String description) { - super(description, ParameterCount.SINGLE); + super(description, ParameterCount.ONE); } @Override public java.lang.String describeFormat() { - return existenceType.description + " directory"; + switch (existenceType) { + case MUST_EXIST: + return "Existing directory."; + case MUST_NOT_EXIST: + return "Non-existing directory."; + default: + return "Directory."; + } } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java index 6a84919..2632174 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java @@ -16,12 +16,19 @@ public class DirectoryOptions extends Option, DirectoryOptions> { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; public DirectoryOptions(final String description) { - super(description, ParameterCount.MULTI); + super(description, ParameterCount.ONE_OR_MORE); } @Override public String describeFormat() { - return existenceType.description + " directories"; + switch (existenceType) { + case MUST_EXIST: + return "One to many existing directories."; + case MUST_NOT_EXIST: + return "One to many non-existing directories."; + default: + return "One to many directories."; + } } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java index dfedb74..e001b0d 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java @@ -14,25 +14,16 @@ public enum ExistenceType { /** * Resource shall exist. */ - MUST_EXIST("existing"), + MUST_EXIST, /** * Resource shall not exist. */ - MUST_NOT_EXIST("not existing"), + MUST_NOT_EXIST, /** * Resource existence does not matter. */ - DOES_NOT_MATTER(""); - - /** - * Human readable description of existence type. - */ - public final String description; - - ExistenceType(final String description) { - this.description = description; - } + DOES_NOT_MATTER; } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java index db941c4..935ea9f 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java @@ -14,7 +14,7 @@ public class FileOption extends Option { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; public FileOption(final String description) { - super(description, ParameterCount.SINGLE); + super(description, ParameterCount.ONE); } protected static boolean validateFile(ExistenceType existenceType, String value) { @@ -41,7 +41,14 @@ public class FileOption extends Option { @Override public java.lang.String describeFormat() { - return existenceType.description + " file"; + switch (existenceType) { + case MUST_EXIST: + return "Existing file."; + case MUST_NOT_EXIST: + return "Non-existing file."; + default: + return "File."; + } } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java index 8328bd3..6c6e51c 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java @@ -4,8 +4,8 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount; import eu.svjatoslav.commons.cli_helper.parameter_parser.Option; +import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount; import java.io.File; import java.util.List; @@ -18,12 +18,19 @@ public class FileOptions extends Option, FileOptions> { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; public FileOptions(final String description) { - super(description, ParameterCount.MULTI); + super(description, ParameterCount.ONE_OR_MORE); } @Override public String describeFormat() { - return existenceType.description + " files"; + switch (existenceType) { + case MUST_EXIST: + return "One to many existing files."; + case MUST_NOT_EXIST: + return "One to many non-existing files."; + default: + return "One to many files."; + } } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java index a5d4bf4..7a5b2b7 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java @@ -10,12 +10,12 @@ import eu.svjatoslav.commons.cli_helper.parameter_parser.Option; public class IntegerOption extends Option { public IntegerOption(final String description) { - super(description, ParameterCount.SINGLE); + super(description, ParameterCount.ONE); } @Override public java.lang.String describeFormat() { - return "integer"; + return "Integer."; } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java index a220ab3..a2a2e8e 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java @@ -15,7 +15,7 @@ public class NullOption extends Option { @Override public java.lang.String describeFormat() { - return ""; + return "None."; } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java index 7ba7c93..25448dc 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java @@ -12,18 +12,18 @@ public class StringOption extends Option { public final String defaultValue; public StringOption(final String description) { - super(description, ParameterCount.SINGLE); + super(description, ParameterCount.ONE); defaultValue = null; } public StringOption(final String description, String defaultValue) { - super(description, ParameterCount.SINGLE); + super(description, ParameterCount.ONE); this.defaultValue = defaultValue; this.setPresent(true); } @Override public java.lang.String describeFormat() { - return "string"; + return "String."; } @Override diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java index 2b888b1..ccbc39e 100644 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java @@ -12,12 +12,12 @@ import java.util.List; public class StringOptions extends Option, StringOptions> { public StringOptions(final String description) { - super(description, ParameterCount.MULTI); + super(description, ParameterCount.ONE_OR_MORE); } @Override public java.lang.String describeFormat() { - return "one to many strings"; + return "One to many string."; } @Override -- 2.20.1