From 324ea20c0c65f671c0d35e94ed90142912a56b4c Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Thu, 12 Oct 2023 23:09:35 +0300 Subject: [PATCH] Update naming in commandline parser --- doc/CLI arguments processing.html | 358 ++++++++++++++++++ doc/CLI arguments processing.org | 39 +- doc/index.html | 95 +++-- .../cli_helper/parameter_parser/Option.java | 208 ++++++++++ .../parameter_parser/Parameter.java | 207 ---------- ...ArgumentCount.java => ParameterCount.java} | 11 +- .../cli_helper/parameter_parser/Parser.java | 42 +- ...oryParameter.java => DirectoryOption.java} | 20 +- ...yParameters.java => DirectoryOptions.java} | 18 +- .../parameter/ExistenceType.java | 23 +- .../{FileParameter.java => FileOption.java} | 20 +- .../{FileParameters.java => FileOptions.java} | 20 +- ...tegerParameter.java => IntegerOption.java} | 16 +- .../{NullParameter.java => NullOption.java} | 14 +- ...StringParameter.java => StringOption.java} | 22 +- ...ringParameters.java => StringOptions.java} | 14 +- .../parameter_parser/ParserTest.java | 20 +- 17 files changed, 759 insertions(+), 388 deletions(-) create mode 100644 doc/CLI arguments processing.html create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java delete mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/{ArgumentCount.java => ParameterCount.java} (56%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{DirectoryParameter.java => DirectoryOption.java} (73%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{DirectoryParameters.java => DirectoryOptions.java} (72%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{FileParameter.java => FileOption.java} (76%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{FileParameters.java => FileOptions.java} (64%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{IntegerParameter.java => IntegerOption.java} (65%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{NullParameter.java => NullOption.java} (59%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{StringParameter.java => StringOption.java} (54%) rename src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/{StringParameters.java => StringOptions.java} (61%) diff --git a/doc/CLI arguments processing.html b/doc/CLI arguments processing.html new file mode 100644 index 0000000..30814c6 --- /dev/null +++ b/doc/CLI arguments processing.html @@ -0,0 +1,358 @@ + + + + + + + +Commandline interface arguments processing + + + + + + +
+

Commandline interface arguments processing

+ + + +
+

1. Terminology

+
+
+
+

1.1. Command and argument

+
+

+Every command-line application has a way of receiving input from +users, usually in the form of command-line arguments. A command-line +argument is a piece of information provided to the command-line +application when it's invoked. These arguments are provided as an +array of strings. The first element of the array (argument 0) is +typically the name of the command itself. +

+ +

+In the example below, 'my-video-coder' is our command, and the rest +are arguments: +

+ +
+
my-video-coder encode --input vid1.mp4 vid2.mp4 vid3.mp4 --quality 5
+
+
+ +

+To better understand how these concepts work together, let's break +down our example command: +

+ + + + +++ ++ ++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
argument #value(s)type
0my-video-codercommand
1encodesubcommand
2–inputoption1
3, 4, 5vid1.mp4 vid2.mp4 vid3.mp4parameters for –input option
6–qualityoption2
75parameter for –quaily option
+
+
+ +
+

1.2. Subcommand

+
+

+Subcommands are arguments that invoke more specific action that a +command can perform. They are often used with commands that have +multiple functions. In our example, encode is a subcommand of +my-video-coder. +

+
+
+ +
+

1.3. Option

+
+

+Options are arguments that change the behavior of a command or +subcommand. They usually start with a dash (-) or double dash +(–). For instance, –input and –quality are options in our +example command. +

+
+
+ +
+

1.4. Parameter

+
+

+Parameter provides additional information to a command, subcommand or +option. +

+ +

+For instance, in our example: +

+
    +
  • 'vid1.mp4 vid2.mp4 vid3.mp4' are parameters for the –input option.
  • +
  • '5' is a parameter for the –quality option.
  • +
+
+
+
+
+
+

Author: Svjatoslav Agejenko

+

Created: 2023-10-12 Thu 23:05

+

Validate

+
+ + diff --git a/doc/CLI arguments processing.org b/doc/CLI arguments processing.org index 2a38bee..c385fcb 100644 --- a/doc/CLI arguments processing.org +++ b/doc/CLI arguments processing.org @@ -7,8 +7,8 @@ - [[id:bb4f96cd-458c-495b-a605-313b2e3e28d2][Back to CLI Helper - library main page]] -* Theory -** Commands and Arguments +* Terminology +** Command and argument Every command-line application has a way of receiving input from users, usually in the form of command-line arguments. A command-line @@ -34,16 +34,17 @@ down our example command: | 2 | --input | [[id:ffedf388-4d23-41eb-98d0-83fd3940b24d][option1]] | | 3, 4, 5 | vid1.mp4 vid2.mp4 vid3.mp4 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][parameters for --input option]] | | 6 | --quality | [[id:ffedf388-4d23-41eb-98d0-83fd3940b24d][option2]] | -| 7 | 5 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][option for --quaily option]] | +| 7 | 5 | [[id:8a39d20c-421f-4bc7-94e4-8e561e58bea0][parameter for --quaily option]] | ** Subcommand :PROPERTIES: :ID: 94242e8a-c59b-42fd-8cc7-ba3df1938119 :END: -Subcommands are more specific actions that a command can perform. They -are often used with commands that have multiple functions. In our -example, *encode* is a subcommand of *my-video-coder*. +Subcommands are arguments that invoke more specific action that a +command can perform. They are often used with commands that have +multiple functions. In our example, *encode* is a subcommand of +*my-video-coder*. ** Option :PROPERTIES: @@ -66,29 +67,3 @@ option. For instance, in our example: - 'vid1.mp4 vid2.mp4 vid3.mp4' are parameters for the *--input* option. - '5' is a parameter for the *--quality* option. - -* Implementation :noexport: - -Parsing Command-line Arguments: -- `Parameter` class is used for defining parameters with their - descriptions, types, and aliases. It also keeps track of whether the - specific option was present in the command line or not. This - information is used later in the processing. - - - `DirectoryParameter`, `FileParameter`, `IntegerParameter`, - `StringParameter`, `NullParameter`, and `StringParameters` are - examples of Parameter classes, each one having unique - characteristics for handling specific types of parameters - (directories, files, integers, strings, etc.). - -- `ArgumentCount` class determines if a option can have any amount - of arguments (MULTI), exactly one argument (SINGLE), or no arguments - (NONE). - -- `Parser` class takes all these parameters and checks whether all - required arguments are provided and if they match the expected - format. - -** Usage example - -TODO: diff --git a/doc/index.html b/doc/index.html index cf028f3..4648538 100644 --- a/doc/index.html +++ b/doc/index.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + CLI Helper - library to help implementing commandline interfaces @@ -202,19 +202,23 @@

Table of Contents

-
-

1. General

+
+

1. General

  • This program is free software: released under Creative Commons Zero @@ -231,8 +235,8 @@
-
-

1.1. Source code

+
+

1.1. Source code

-
-

2. Library contents

+
+

2. Library contents

-
  • -Commandline Interface helper +

    +This library is a collection of command-line interface (CLI) helper +functions that simplifies the process of building and maintaining CLI +applications. The library provides several different functionalities, +such as:

    -
    -eu.svjatoslav.commons.cli_helper.CLIHelper
    -
  • -
  • -Commandline arguments parser and validator. -

    -
    -eu.svjatoslav.commons.cli_helper.parameter_parser
    -
  • +
    + +
    +

    2.1. Ask for user input

    +
    +
    +
    askBoolean()
    Asks the user to enter a boolean value (yes/no).
    +
    askLong()
    Asks the user to enter an integer.
    +
    askString()
    Asks the user to enter a string.
    +
    +
    +
    -
    -

    3. Usage

    +
    +

    3. Getting the library

    -Instructions to embed svjatoslav-commons in your project as a library: +Instructions to embed svjatoslav-commons library in your project:

    @@ -287,26 +300,26 @@ Maven pom.xml file snippet:

    -
    <dependencies>
    +
    <dependencies>
         ...
    -    <dependency>
    -        <groupId>eu.svjatoslav</groupId>
    -        <artifactId>cli-helper</artifactId>
    -        <version>1.0</version>
    -    </dependency>
    +    <dependency>
    +        <groupId>eu.svjatoslav</groupId>
    +        <artifactId>cli-helper</artifactId>
    +        <version>1.0</version>
    +    </dependency>
         ...
    -</dependencies>
    +</dependencies>
     
     
    -<repositories>
    +<repositories>
         ...
    -    <repository>
    -        <id>svjatoslav.eu</id>
    -        <name>Svjatoslav repository</name>
    -        <url>http://www3.svjatoslav.eu/maven/</url>
    -    </repository>
    +    <repository>
    +        <id>svjatoslav.eu</id>
    +        <name>Svjatoslav repository</name>
    +        <url>http://www3.svjatoslav.eu/maven/</url>
    +    </repository>
         ...
    -</repositories>
    +</repositories>
     
    @@ -314,7 +327,7 @@ Maven pom.xml file snippet:

    Author: Svjatoslav Agejenko

    -

    Created: 2022-07-13 Wed 04:24

    +

    Created: 2023-10-12 Thu 22:18

    Validate

    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 new file mode 100755 index 0000000..cb36966 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Option.java @@ -0,0 +1,208 @@ +/* + * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. + */ +package eu.svjatoslav.commons.cli_helper.parameter_parser; + +import java.util.ArrayList; +import java.util.List; + +import static eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount.NONE; +import static java.lang.String.join; +import static java.util.Collections.addAll; +import static java.util.stream.Collectors.joining; + +public abstract class Option { + + /** + * Purpose of this option, like: input image path, compression level, etc... + * + * Note: for describing option type there is {@link #describeFormat()}. + */ + public final String description; + + /** + * List of arguments for this parameter. + */ + public final List parameters = new ArrayList<>(); + final ParameterCount parameterCount; + + /** + * List of aliases for this option. + */ + private final List aliases = new ArrayList<>(); + + /** + * Indicates that at least one parameter is mandatory for this option. + */ + protected boolean mandatory; + + /** + * If this parameter was present in the commandline, then this boolean will + * be set to true. + */ + private boolean isPresent; + + public Option(final boolean mandatory, + final ParameterCount parameterCount, final String description, + final String... aliases2) { + + this.mandatory = mandatory; + this.description = description; + this.parameterCount = parameterCount; + + // save aliases + addAll(aliases, aliases2); + } + + public Option(final String description, final ParameterCount parameterCount) { + this.description = description; + this.parameterCount = parameterCount; + } + + @SuppressWarnings("unchecked") + public I addAliases(final String... aliasArray) { + + // save aliases + addAll(aliases, aliasArray); + + return (I) this; + } + + /** + * @param parameterString parameter to add + * @return true if no errors were found. false + * otherwise. + */ + public boolean addParameter(final String parameterString) { + // check if arguments are allowed for this parameter + if (parameterCount.equals(NONE)) { + System.out + .println("Error! No parameters are allowed for option(s): " + + getAliasesAsString()); + return false; + } + + // check if multiple arguments are allowed + if ((!parameters.isEmpty()) + && (parameterCount.equals(ParameterCount.SINGLE))) { + System.out + .println("Error! Only single parameter is allowed for argument(s): " + + getAliasesAsString()); + return false; + } + + if (isValid(parameterString)) { + parameters.add(parameterString); + return true; + } else { + System.out.println("Error! Invalid parameter type for \"" + parameterString + + "\". It shall be " + describeFormat() + "."); + return false; + } + } + + /** + * @return Single line parameter type description. For example: "file", "date", "integer", "regular expression", etc.. + *

    + * Note: for argument purpose description there is {@link #description} + */ + public abstract String describeFormat(); + + /** + * @return aliases for this parameter as string. + */ + public String getAliasesAsString() { + return join(", ", aliases); + } + + /** + * @return help for this option. + */ + public String getHelp() { + final StringBuilder result = new StringBuilder(); + + // first line + result.append(getAliasesAsString()); + if (!NONE.equals(parameterCount)) { + result + .append(" (") + .append(isMandatory() ? "mandatory, " : "") + .append(describeFormat()) + .append(")"); + + if (parameterCount.equals(ParameterCount.MULTI)) + result.append("..."); + } + result.append("\n"); + + // second line + result.append(" " + description + "\n"); + + return result.toString(); + } + + public abstract T getValue(); + + public boolean isMandatory() { + return mandatory; + } + + /** + * @return the parameterSpecified + */ + public boolean isPresent() { + return isPresent; + } + + /** + * @param present the parameterSpecified to set + */ + protected void setPresent(final boolean present) { + this.isPresent = present; + } + + /** + * @param alias alias to check against + * @return true if given alias is registered for this + * parameter. + */ + public boolean matchesAlias(final String alias) { + return aliases.contains(alias); + + } + + /** + * Notifies this option that no more parameters will follow. + * This gives option chance to verify if this is ok. + * + * @return true if no errors were found. false + * otherwise. + */ + public boolean noMoreArguments() { + + if ((!parameterCount.equals(NONE)) + && (parameters.isEmpty())) { + + System.out.println("Error! " + getAliasesAsString() + + " require at least one parameter."); + + return false; + } + return true; + } + + @SuppressWarnings("unchecked") + public I setMandatory() { + mandatory = true; + return (I) this; + } + + /** + * @param value value to validate + * @return true if value is correct, false + * otherwise. + */ + public abstract boolean isValid(String value); + +} diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java deleted file mode 100755 index f360ddc..0000000 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko. - * This project is released under Creative Commons Zero (CC0) license. - */ -package eu.svjatoslav.commons.cli_helper.parameter_parser; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public abstract class Parameter { - - /** - * Purpose of this argument, like: input image path, compression level, etc... - * - * Note: for describing argument type (file, integer, ...) there is {@link #describeFormat()}. - */ - public final String description; - - /** - * List of arguments for this parameter. - */ - public final List arguments = new ArrayList<>(); - final ArgumentCount argumentCount; - private final List aliases = new ArrayList<>(); - /** - * Indicates that at least one argument is mandatory for this parameter. - */ - protected boolean mandatory; - /** - * If this parameter was present in the commandline, then this boolean will - * be set to true. - */ - private boolean specified; - - public Parameter(final boolean mandatory, - final ArgumentCount argumentCount, final String description, - final String... aliases2) { - - this.mandatory = mandatory; - this.description = description; - this.argumentCount = argumentCount; - - // save aliases - Collections.addAll(aliases, aliases2); - - } - - public Parameter(final String description, final ArgumentCount argumentCount) { - this.description = description; - this.argumentCount = argumentCount; - } - - @SuppressWarnings("unchecked") - public I addAliases(final String... aliasArray) { - - // save aliases - Collections.addAll(aliases, aliasArray); - - return (I) this; - } - - /** - * @param argumentString argument to add - * @return true if no errors were found. false - * otherwise. - */ - public boolean addArgument(final String argumentString) { - // check if arguments are allowed for this parameter - if (argumentCount.equals(ArgumentCount.NONE)) { - System.out - .println("Error! No arguments are allowed for parameters: " - + getAliases()); - return false; - } - - // check if multiple arguments are allowed - if ((arguments.size() > 0) - && (argumentCount.equals(ArgumentCount.SINGLE))) { - System.out - .println("Error! Only single argument is allowed for parameters: " - + getAliases()); - return false; - } - - if (!validate(argumentString)) { - - System.out.println("Error! Invalid argument \"" + argumentString - + "\". It shall be " + describeFormat() + "."); - return false; - - } - - arguments.add(argumentString); - - return true; - } - - /** - * @return Single line argument type description. If argument type should be file, - * date, integer, regular expression, etc.. - * - * Note: for argument purpose description there is {@link #description} - */ - public abstract String describeFormat(); - - public String getAliases() { - final StringBuilder buffer = new StringBuilder(); - - for (final String alias : aliases) { - - if (buffer.length() > 0) - buffer.append(", "); - - buffer.append(alias); - } - - return buffer.toString(); - } - - public String getHelp() { - final StringBuilder buffer = new StringBuilder(); - - // first line - buffer.append(getAliases()); - if (!argumentCount.equals(ArgumentCount.NONE)) { - buffer - .append(" (") - .append(isMandatory() ? "mandatory, " : "") - .append(describeFormat()) - .append(")"); - - if (argumentCount.equals(ArgumentCount.MULTI)) - buffer.append("..."); - } - buffer.append("\n"); - - // second line - buffer.append(" " + description + "\n"); - - return buffer.toString(); - } - - public abstract T getValue(); - - public boolean isMandatory() { - return mandatory; - } - - /** - * @return the parameterSpecified - */ - public boolean isSpecified() { - return specified; - } - - /** - * @param specified the parameterSpecified to set - */ - protected void setSpecified(final boolean specified) { - this.specified = specified; - } - - /** - * @param alias alias to check against - * @return true if given alias is registered for this - * parameter. - */ - public boolean matchesAlias(final String alias) { - return aliases.contains(alias); - - } - - /** - * Notifies this parameter that no more arguments will follow. This gives - * parameter chance to verify if this is ok. - * - * @return true if no errors were found. false - * otherwise. - */ - public boolean noMoreArguments() { - - if ((!argumentCount.equals(ArgumentCount.NONE)) - && (arguments.isEmpty())) { - - System.out.println("Error! " + getAliases() - + " require at least one following argument."); - - return false; - } - return true; - } - - @SuppressWarnings("unchecked") - public I setMandatory() { - mandatory = true; - return (I) this; - } - - /** - * @param value value to validate - * @return true if value is correct, false - * otherwise. - */ - public abstract boolean validate(String value); - -} diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java similarity index 56% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java index 3d8ed7f..bcaceec 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParameterCount.java @@ -4,19 +4,22 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser; -public enum ArgumentCount { +/** + * This enum is used to define how many parameters con be given to particular option. + */ +public enum ParameterCount { /** - * Parameter has no arguments. + * Option has no parameters. */ NONE, /** - * Parameter has exactly one argument. + * Option has exactly one parameter. */ SINGLE, /** - * Parameter can have any amount of arguments. + * Option can have any amount of parameters. */ MULTI } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java index 639be0f..d307ea3 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java @@ -11,10 +11,10 @@ import java.util.ArrayList; */ public class Parser { - private final ArrayList> parameters = new ArrayList<>(); + private final ArrayList> options = new ArrayList<>(); - public E add(final E parameter) { - parameters.add(parameter); + public E add(final E parameter) { + options.add(parameter); return parameter; } @@ -24,10 +24,10 @@ public class Parser { */ private boolean checkMandatoryArgumentsPresent() { - for (final Parameter parameter : parameters) - if (parameter.isMandatory() && (!parameter.isSpecified())) { + for (final Option option : options) + if (option.isMandatory() && (!option.isPresent())) { System.out.println("Error! Mandatory parameter (" - + parameter.getAliases() + ") is not specified."); + + option.getAliasesAsString() + ") is not specified."); return false; } @@ -41,11 +41,11 @@ public class Parser { * @param alias parameter alias * @return found parameter or null if parameter was not found. */ - public Parameter findParameterByAlias(final String alias) { + public Option findParameterByAlias(final String alias) { - for (final Parameter parameter : parameters) - if (parameter.matchesAlias(alias)) - return parameter; + for (final Option option : options) + if (option.matchesAlias(alias)) + return option; return null; } @@ -57,28 +57,28 @@ public class Parser { */ public boolean parse(final String[] args) { - Parameter currentParameter = null; + Option currentOption = null; for (final String argument : args) { - final Parameter parameterForAlias = findParameterByAlias(argument); - if (parameterForAlias == null) { - if (currentParameter == null) { + final Option optionForAlias = findParameterByAlias(argument); + if (optionForAlias == null) { + if (currentOption == null) { System.out.println("Unknown commandline parameter: " + argument); return false; } - if (!currentParameter.addArgument(argument)) + if (!currentOption.addParameter(argument)) return false; } else { - if (currentParameter != null) - if (!currentParameter.noMoreArguments()) + if (currentOption != null) + if (!currentOption.noMoreArguments()) return false; - parameterForAlias.setSpecified(true); - currentParameter = parameterForAlias; + optionForAlias.setPresent(true); + currentOption = optionForAlias; } } @@ -88,8 +88,8 @@ public class Parser { public void showHelp() { System.out.println("Available commandline arguments:"); - for (final Parameter parameter : parameters) - System.out.println(parameter.getHelp()); + for (final Option option : options) + System.out.println(option.getHelp()); } } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java similarity index 73% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java index c76b146..8f97a8b 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOption.java @@ -4,17 +4,17 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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 java.io.File; -public class DirectoryParameter extends Parameter { +public class DirectoryOption extends Option { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - public DirectoryParameter(final String description) { - super(description, ArgumentCount.SINGLE); + public DirectoryOption(final String description) { + super(description, ParameterCount.SINGLE); } @Override @@ -25,25 +25,25 @@ public class DirectoryParameter extends Parameter { @Override public File getValue() { - if (arguments.size() != 1) + if (parameters.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - return new File(arguments.get(0)); + return new File(parameters.get(0)); } - public DirectoryParameter mustExist() { + public DirectoryOption mustExist() { existenceType = ExistenceType.MUST_EXIST; return this; } - public DirectoryParameter mustNotExist() { + public DirectoryOption mustNotExist() { existenceType = ExistenceType.MUST_NOT_EXIST; return this; } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { final File file = new File(value); if (existenceType == ExistenceType.MUST_EXIST) { diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java similarity index 72% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java index 0927aaf..6a84919 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryOptions.java @@ -4,19 +4,19 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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 java.io.File; import java.util.List; import java.util.stream.Collectors; -public class DirectoryParameters extends Parameter, DirectoryParameters> { +public class DirectoryOptions extends Option, DirectoryOptions> { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - public DirectoryParameters(final String description) { - super(description, ArgumentCount.MULTI); + public DirectoryOptions(final String description) { + super(description, ParameterCount.MULTI); } @Override @@ -26,21 +26,21 @@ public class DirectoryParameters extends Parameter, DirectoryParamete @Override public List getValue() { - return arguments.stream().map(File::new).collect(Collectors.toList()); + return parameters.stream().map(File::new).collect(Collectors.toList()); } - public DirectoryParameters mustExist() { + public DirectoryOptions mustExist() { existenceType = ExistenceType.MUST_EXIST; return this; } - public DirectoryParameters mustNotExist() { + public DirectoryOptions mustNotExist() { existenceType = ExistenceType.MUST_NOT_EXIST; return this; } @Override - public boolean validate(final String value) { + public boolean isValid(final String value) { final File file = new File(value); if (existenceType == ExistenceType.MUST_EXIST) { 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 0b6b4bc..dfedb74 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 @@ -4,10 +4,31 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; +/** + * This enum is used to define if resource denoted by particular option parameter shall exist or not. + *

    + * This allows to specify for example if directory shall exist or not. + */ public enum ExistenceType { - MUST_EXIST("existing"), MUST_NOT_EXIST("not existing"), DOES_NOT_MATTER(""); + /** + * Resource shall exist. + */ + MUST_EXIST("existing"), + + /** + * Resource shall not exist. + */ + MUST_NOT_EXIST("not existing"), + + /** + * Resource existence does not matter. + */ + DOES_NOT_MATTER(""); + /** + * Human readable description of existence type. + */ public final String description; ExistenceType(final String description) { diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java similarity index 76% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java index 5435bc3..db941c4 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOption.java @@ -4,17 +4,17 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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 java.io.File; -public class FileParameter extends Parameter { +public class FileOption extends Option { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - public FileParameter(final String description) { - super(description, ArgumentCount.SINGLE); + public FileOption(final String description) { + super(description, ParameterCount.SINGLE); } protected static boolean validateFile(ExistenceType existenceType, String value) { @@ -47,25 +47,25 @@ public class FileParameter extends Parameter { @Override public File getValue() { - if (arguments.size() != 1) + if (parameters.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - return new File(arguments.get(0)); + return new File(parameters.get(0)); } - public FileParameter mustExist() { + public FileOption mustExist() { existenceType = ExistenceType.MUST_EXIST; return this; } - public FileParameter mustNotExist() { + public FileOption mustNotExist() { existenceType = ExistenceType.MUST_NOT_EXIST; return this; } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { return validateFile(existenceType, value); } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java similarity index 64% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java index 850114a..8328bd3 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileOptions.java @@ -4,21 +4,21 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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 java.io.File; import java.util.List; import java.util.stream.Collectors; -import static eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileParameter.validateFile; +import static eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileOption.validateFile; -public class FileParameters extends Parameter, FileParameters> { +public class FileOptions extends Option, FileOptions> { private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - public FileParameters(final String description) { - super(description, ArgumentCount.MULTI); + public FileOptions(final String description) { + super(description, ParameterCount.MULTI); } @Override @@ -28,21 +28,21 @@ public class FileParameters extends Parameter, FileParameters> { @Override public List getValue() { - return arguments.stream().map(File::new).collect(Collectors.toList()); + return parameters.stream().map(File::new).collect(Collectors.toList()); } - public FileParameters mustExist() { + public FileOptions mustExist() { existenceType = ExistenceType.MUST_EXIST; return this; } - public FileParameters mustNotExist() { + public FileOptions mustNotExist() { existenceType = ExistenceType.MUST_NOT_EXIST; return this; } @Override - public boolean validate(final String value) { + public boolean isValid(final String value) { return validateFile(existenceType, value); } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java similarity index 65% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java index 130d360..a5d4bf4 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerOption.java @@ -4,13 +4,13 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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; -public class IntegerParameter extends Parameter { +public class IntegerOption extends Option { - public IntegerParameter(final String description) { - super(description, ArgumentCount.SINGLE); + public IntegerOption(final String description) { + super(description, ParameterCount.SINGLE); } @Override @@ -20,14 +20,14 @@ public class IntegerParameter extends Parameter { @Override public Integer getValue() { - if (arguments.size() != 1) + if (parameters.size() != 1) throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); - return Integer.parseInt(arguments.get(0)); + return Integer.parseInt(parameters.get(0)); } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { try { java.lang.Integer.valueOf(value); return true; diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java similarity index 59% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java index ca234af..a220ab3 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullOption.java @@ -4,13 +4,13 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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; -public class NullParameter extends Parameter { +public class NullOption extends Option { - public NullParameter(final String description) { - super(description, ArgumentCount.NONE); + public NullOption(final String description) { + super(description, ParameterCount.NONE); } @Override @@ -20,11 +20,11 @@ public class NullParameter extends Parameter { @Override public Boolean getValue() { - return isSpecified(); + return isPresent(); } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { return true; } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java similarity index 54% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java index c5ed383..7ba7c93 100755 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOption.java @@ -4,21 +4,21 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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; -public class StringParameter extends Parameter { +public class StringOption extends Option { public final String defaultValue; - public StringParameter(final String description) { - super(description, ArgumentCount.SINGLE); + public StringOption(final String description) { + super(description, ParameterCount.SINGLE); defaultValue = null; } - public StringParameter(final String description, String defaultValue) { - super(description, ArgumentCount.SINGLE); + public StringOption(final String description, String defaultValue) { + super(description, ParameterCount.SINGLE); this.defaultValue = defaultValue; - this.setSpecified(true); + this.setPresent(true); } @Override @@ -29,16 +29,16 @@ public class StringParameter extends Parameter { @Override public String getValue() { - if (arguments.isEmpty() && (defaultValue != null)) return defaultValue; + if (parameters.isEmpty() && (defaultValue != null)) return defaultValue; - if (arguments.size() == 1) return arguments.get(0); + if (parameters.size() == 1) return parameters.get(0); throw new RuntimeException("Parameter " + description + " shall have exactly 1 argument."); } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { return true; } diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java similarity index 61% rename from src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java rename to src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java index 1a3e410..2b888b1 100644 --- a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringOptions.java @@ -4,15 +4,15 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; -import 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 java.util.List; -public class StringParameters extends Parameter, StringParameters> { +public class StringOptions extends Option, StringOptions> { - public StringParameters(final String description) { - super(description, ArgumentCount.MULTI); + public StringOptions(final String description) { + super(description, ParameterCount.MULTI); } @Override @@ -22,11 +22,11 @@ public class StringParameters extends Parameter, StringParameters> @Override public List getValue() { - return arguments; + return parameters; } @Override - public boolean validate(final java.lang.String value) { + public boolean isValid(final java.lang.String value) { return true; } diff --git a/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java index ab87cae..949e255 100755 --- a/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java @@ -4,8 +4,8 @@ */ package eu.svjatoslav.commons.cli_helper.parameter_parser; -import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileParameter; -import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.StringParameter; +import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileOption; +import eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.StringOption; import org.junit.Before; import org.junit.Test; @@ -28,13 +28,13 @@ public class ParserTest { public void testParse() throws IOException { // define allowed parameters - final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") + final StringOption helpOption = parser.add(new StringOption("Show help screen") .addAliases("--help", "-h").setMandatory()); - final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) + final StringOption compileOption = parser.add(new StringOption("Compile code")) .addAliases("--compile", "-c"); - FileParameter fileParameter = parser.add(new FileParameter("Input file") + FileOption fileOption = parser.add(new FileOption("Input file") .addAliases("-i").mustExist()); createTemporaryFile(); @@ -46,16 +46,16 @@ public class ParserTest { parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); // --help was in the arguments - assertTrue(helpParameter.isSpecified()); + assertTrue(helpOption.isPresent()); // compile was not present - assertFalse(compileParameter.isSpecified()); + assertFalse(compileOption.isPresent()); // validate that help argument was "section" - assertEquals("section", helpParameter.getValue()); + assertEquals("section", helpOption.getValue()); - assertTrue(fileParameter.isSpecified()); - assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); + assertTrue(fileOption.isPresent()); + assertEquals("/tmp/file with spaces", fileOption.getValue().getAbsolutePath()); } -- 2.20.1