From f3ca64a08930aa9a9372fe42df596b5e4643084e Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 26 Jun 2022 02:44:35 +0300 Subject: [PATCH] Moved CLI helper functionality to new dedicated project: https://www3.svjatoslav.eu/projects/cli-helper/ --- doc/index.html | 576 +++++++++--------- doc/index.org | 16 - .../commons/commandline/CLIHelper.java | 102 ---- .../parameterparser/ArgumentCount.java | 13 - .../parameterparser/Parameter.java | 204 ------- .../commandline/parameterparser/Parser.java | 97 --- .../parameter/DirectoryParameter.java | 68 --- .../parameter/DirectoryParameters.java | 65 -- .../parameter/ExistenceType.java | 17 - .../parameter/FileParameter.java | 72 --- .../parameter/FileParameters.java | 49 -- .../parameter/IntegerParameter.java | 38 -- .../parameter/NullParameter.java | 31 - .../parameter/StringParameter.java | 45 -- .../parameter/StringParameters.java | 33 - .../parameterparser/ParserTest.java | 68 --- 16 files changed, 276 insertions(+), 1218 deletions(-) delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistenceType.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java delete mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java delete mode 100755 src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java diff --git a/doc/index.html b/doc/index.html index 9ec1008..92e6001 100644 --- a/doc/index.html +++ b/doc/index.html @@ -1,362 +1,353 @@ - - + + + + + + Svjatoslav Commons - Java library of commonly used functions - - - - - - - - - - - + -
-

Svjatoslav Commons - Java library of commonly used functions

+
+

Svjatoslav Commons - Java library of commonly used functions

+
+

Table of Contents

+ +
-
-

1 General

+
+

1. General

-
  • Other software projects hosted at svjatoslav.eu -
  • +
  • Other software projects hosted at svjatoslav.eu
  • -
    -

    1.1 Source code

    +
    +

    1.1. Source code

    -
    -

    2 Library contents

    +
    +

    2. Library contents

      -
    • See JavaDoc. -
    • - -
    • Commandline Interface helper -
      -eu.svjatoslav.commons.commandline.CLIHelper
      -
      -
      -
    • - -
    • Commandline arguments parser and validator. -
      -eu.svjatoslav.commons.commandline.parameterparser
      -
      -
      -
    • +
    • See JavaDoc.
    • -
    • Bit input and output streams. +
    • +Bit input and output streams. +

       eu.svjatoslav.commons.data.BitInputStream
       eu.svjatoslav.commons.data.BitOutputStream
      +
    • - - - -
    • Slightly enhanced data input and output streams. +
    • +Slightly enhanced data input and output streams. +

       eu.svjatoslav.commons.data.EnhancedDataInputStream
       eu.svjatoslav.commons.data.EnhancedDataOutputStream
      +
    • - - - -
    • Byte array to HEX string converter. +
    • +Byte array to HEX string converter. +

       eu.svjatoslav.commons.data.HexConverter
      +
    • - - - -
    • Utility that tries to quess user desktop directory. (Quite ugly and +
    • +Utility that tries to quess user desktop directory. (Quite ugly and unreliable. Better solution is needed.) +

       eu.svjatoslav.commons.file.CommonPathResolver
      +
    • - - - -
    • File path parser. +
    • +File path parser. +

       eu.svjatoslav.commons.file.FilePathParser
      +
    • - - - -
    • File IO helper. +
    • +File IO helper. +

       eu.svjatoslav.commons.file.IOHelper
      -
      -
      -
    • +
    • Graphical error dialog.
        -
      • Reusable graphical dialog to capture and show program exceptions +
      • +Reusable graphical dialog to capture and show program exceptions and associated program call stack traceback. +

         eu.svjatoslav.commons.gui.dialog.ExceptionDialog
        +
      • +
    • - - -
    - - -
  • Primitive and simple component to add navigation menus to the web +
  • +Primitive and simple component to add navigation menus to the web sites. +

     eu.svjatoslav.commons.network.navigation
    +
  • - - - -
  • String tokenizer. +
  • +String tokenizer. +

     eu.svjatoslav.commons.string.tokenizer
    +
  • - - - -
  • Improved String, optimized for dealing with prefixes and suffixes. +
  • +Improved String, optimized for dealing with prefixes and suffixes. +

     eu.svjatoslav.commons.string.String2
    +
  • - - - -
  • Wildcards matcher. (?, *) +
  • +Wildcards matcher. (?, *) +

     eu.svjatoslav.commons.string.WildCardMatcher#match
    -
    -
    -
  • +
    -
    -

    3 Usage

    +
    +

    3. Usage

    Instructions to embed svjatoslav-commons in your project as a library: @@ -367,7 +358,6 @@ Maven pom.xml file snippet:

    -
    <dependencies>
         ...
         <dependency>
    @@ -392,25 +382,11 @@ Maven pom.xml file snippet:
     
    -
    -
    -

    Author: Svjatoslav Agejenko

    -

    Created: 2020-05-01 Fri 23:39

    -

    Emacs 26.1 (Org-mode 9.1.9)

    +
    +

    Author: Svjatoslav Agejenko

    +

    Created: 2022-06-26 Sun 02:43

    +

    Validate

    -
    diff --git a/doc/index.org b/doc/index.org index 0183620..aff40fe 100644 --- a/doc/index.org +++ b/doc/index.org @@ -1,15 +1,5 @@ #+TITLE: Svjatoslav Commons - Java library of commonly used functions -* (document settings) :noexport: -** use dark style for TWBS-HTML exporter -#+HTML_HEAD: -#+HTML_HEAD: -#+HTML_HEAD: -#+HTML_HEAD: - * General - This program is free software: released under Creative Commons Zero (CC0) license @@ -34,12 +24,6 @@ * Library contents - See [[https://www3.svjatoslav.eu/projects/svjatoslav_commons/apidocs/][JavaDoc]]. -- Commandline Interface helper - : eu.svjatoslav.commons.commandline.CLIHelper - -- Commandline arguments parser and validator. - : eu.svjatoslav.commons.commandline.parameterparser - - Bit input and output streams. : eu.svjatoslav.commons.data.BitInputStream : eu.svjatoslav.commons.data.BitOutputStream diff --git a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java deleted file mode 100755 index 597bdec..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/CLIHelper.java +++ /dev/null @@ -1,102 +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.commandline; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; - -/** - * Command-line interface helper. - */ -public class CLIHelper { - - /** - * Ask boolean value from user on command-line. - * - * @param prompt to show to the user - * @return true is user answered affirmative. - */ - public static boolean askBoolean(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) - try { - System.out.print(prompt); - - String line = br.readLine(); - if (line != null) { - final String userInput = line.toLowerCase(); - - if ("y".equals(userInput) || "yes".equals(userInput) - || "true".equals(userInput)) - return true; - - if ("n".equals(userInput) || "no".equals(userInput) - || "false".equals(userInput)) - return false; - } - System.out - .println("Invalid input. You shall enter y/yes/true or n/no/false."); - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - } - - /** - * Ask numerical long value from user on command-line. - * - * @param prompt to show to the user - * @return value given by user - */ - public static long askLong(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - final String userInput = br.readLine(); - - try { - return Long.parseLong(userInput); - } catch (final NumberFormatException e) { - System.out.println("\nError: You shall enter an integer."); - } - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } - - /** - * Ask string value from user on command-line. - * - * @param prompt to show to the user - * @return value given by the user - */ - public static String askString(final String prompt) { - - final BufferedReader br = new BufferedReader(new InputStreamReader( - System.in)); - - while (true) { - System.out.print(prompt); - - try { - return br.readLine(); - } catch (final IOException ioe) { - ioe.printStackTrace(); - } - - } - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java deleted file mode 100755 index 0f30b72..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/ArgumentCount.java +++ /dev/null @@ -1,13 +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.commandline.parameterparser; - -public enum ArgumentCount { - - NONE, // parameter has no arguments - SINGLE, // parameter has one argument - MULTI // parameter can have multiple arguments - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java deleted file mode 100755 index c9023a4..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ /dev/null @@ -1,204 +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.commandline.parameterparser; - -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; - - 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/commandline/parameterparser/Parser.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java deleted file mode 100755 index e8e3638..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ /dev/null @@ -1,97 +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.commandline.parameterparser; - -import java.util.ArrayList; - -/** - *
    - * Single commandline parameter can have any amount of arguments.
    - * 
    - */ -public class Parser { - - private final ArrayList> parameters = new ArrayList<>(); - - public E add(final E parameter) { - parameters.add(parameter); - return parameter; - } - - /** - * @return true if no errors were found. false - * otherwise. - */ - private boolean checkMandatoryArgumentsPresent() { - - for (final Parameter parameter : parameters) - if (parameter.isMandatory() && (!parameter.isSpecified())) { - System.out.println("Error! Mandatory parameter (" - + parameter.getAliases() + ") is not specified."); - return false; - } - - return true; - } - - /** - * Return parameter by given alias or null if no parameter - * exists for given alias. - * - * @param alias parameter alias - * @return found parameter or null if parameter was not found. - */ - public Parameter findParameterByAlias(final String alias) { - - for (final Parameter parameter : parameters) - if (parameter.matchesAlias(alias)) - return parameter; - - return null; - } - - /** - * @param args commandline arguments - * @return true if no errors were found. false - * otherwise. - */ - public boolean parse(final String[] args) { - - Parameter currentParameter = null; - - for (final String argument : args) { - - final Parameter parameterForAlias = findParameterByAlias(argument); - if (parameterForAlias == null) { - if (currentParameter == null) { - System.out.println("Unknown commandline parameter: " - + argument); - return false; - } - - if (!currentParameter.addArgument(argument)) - return false; - - } else { - if (currentParameter != null) - if (!currentParameter.noMoreArguments()) - return false; - - parameterForAlias.setSpecified(true); - currentParameter = parameterForAlias; - } - - } - - return checkMandatoryArgumentsPresent(); - } - - public void showHelp() { - System.out.println("Available commandline arguments:"); - for (final Parameter parameter : parameters) - System.out.println(parameter.getHelp()); - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java deleted file mode 100755 index a7b872a..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java +++ /dev/null @@ -1,68 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -import java.io.File; - -public class DirectoryParameter extends Parameter { - - private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - - public DirectoryParameter(final String description) { - super(description, ArgumentCount.SINGLE); - } - - @Override - public java.lang.String describeFormat() { - return existenceType.description + " directory"; - } - - @Override - public File getValue() { - - if (arguments.size() != 1) - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - - return new File(arguments.get(0)); - } - - public DirectoryParameter mustExist() { - existenceType = ExistenceType.MUST_EXIST; - return this; - } - - public DirectoryParameter mustNotExist() { - existenceType = ExistenceType.MUST_NOT_EXIST; - return this; - } - - @Override - public boolean validate(final java.lang.String value) { - final File file = new File(value); - - if (existenceType == ExistenceType.MUST_EXIST) { - return file.exists() && file.isDirectory(); - } - - if (existenceType == ExistenceType.MUST_NOT_EXIST) { - return !file.exists(); - } - - if (existenceType == ExistenceType.DOES_NOT_MATTER) { - if (file.exists()) - if (file.isFile()) - return false; - - return true; - } - - return false; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java deleted file mode 100755 index a35976d..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameters.java +++ /dev/null @@ -1,65 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -import java.io.File; -import java.util.List; -import java.util.stream.Collectors; - -public class DirectoryParameters extends Parameter, DirectoryParameters> { - - private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - - public DirectoryParameters(final String description) { - super(description, ArgumentCount.MULTI); - } - - @Override - public String describeFormat() { - return existenceType.description + " directories"; - } - - @Override - public List getValue() { - return arguments.stream().map(File::new).collect(Collectors.toList()); - } - - public DirectoryParameters mustExist() { - existenceType = ExistenceType.MUST_EXIST; - return this; - } - - public DirectoryParameters mustNotExist() { - existenceType = ExistenceType.MUST_NOT_EXIST; - return this; - } - - @Override - public boolean validate(final String value) { - final File file = new File(value); - - if (existenceType == ExistenceType.MUST_EXIST) { - return file.exists() && file.isDirectory(); - } - - if (existenceType == ExistenceType.MUST_NOT_EXIST) { - return !file.exists(); - } - - if (existenceType == ExistenceType.DOES_NOT_MATTER) { - if (file.exists()) - if (file.isFile()) - return false; - - return true; - } - - return false; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistenceType.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistenceType.java deleted file mode 100755 index 33cdd91..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistenceType.java +++ /dev/null @@ -1,17 +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.commandline.parameterparser.parameter; - -public enum ExistenceType { - - MUST_EXIST("existing"), MUST_NOT_EXIST("not existing"), DOES_NOT_MATTER(""); - - public final String description; - - ExistenceType(final String description) { - this.description = description; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java deleted file mode 100755 index 4a63be7..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java +++ /dev/null @@ -1,72 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -import java.io.File; - -public class FileParameter extends Parameter { - - private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - - public FileParameter(final String description) { - super(description, ArgumentCount.SINGLE); - } - - protected static boolean validateFile(ExistenceType existenceType, String value) { - final File file = new File(value); - - if (existenceType == ExistenceType.MUST_EXIST) { - return file.exists() && file.isFile(); - } - - if (existenceType == ExistenceType.MUST_NOT_EXIST) { - return !file.exists(); - } - - if (existenceType == ExistenceType.DOES_NOT_MATTER) { - if (file.exists()) - if (file.isDirectory()) - return false; - - return true; - } - - return false; - } - - @Override - public java.lang.String describeFormat() { - return existenceType.description + " file"; - } - - @Override - public File getValue() { - - if (arguments.size() != 1) - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - - return new File(arguments.get(0)); - } - - public FileParameter mustExist() { - existenceType = ExistenceType.MUST_EXIST; - return this; - } - - public FileParameter mustNotExist() { - existenceType = ExistenceType.MUST_NOT_EXIST; - return this; - } - - @Override - public boolean validate(final java.lang.String value) { - return validateFile(existenceType, value); - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java deleted file mode 100755 index 5bbf749..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java +++ /dev/null @@ -1,49 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -import java.io.File; -import java.util.List; -import java.util.stream.Collectors; - -import static eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter.validateFile; - -public class FileParameters extends Parameter, FileParameters> { - - private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; - - public FileParameters(final String description) { - super(description, ArgumentCount.MULTI); - } - - @Override - public String describeFormat() { - return existenceType.description + " files"; - } - - @Override - public List getValue() { - return arguments.stream().map(File::new).collect(Collectors.toList()); - } - - public FileParameters mustExist() { - existenceType = ExistenceType.MUST_EXIST; - return this; - } - - public FileParameters mustNotExist() { - existenceType = ExistenceType.MUST_NOT_EXIST; - return this; - } - - @Override - public boolean validate(final String value) { - return validateFile(existenceType, value); - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java deleted file mode 100755 index 3aacf57..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java +++ /dev/null @@ -1,38 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -public class IntegerParameter extends Parameter { - - public IntegerParameter(final String description) { - super(description, ArgumentCount.SINGLE); - } - - @Override - public java.lang.String describeFormat() { - return "integer"; - } - - @Override - public Integer getValue() { - if (arguments.size() != 1) - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - return Integer.parseInt(arguments.get(0)); - } - - @Override - public boolean validate(final java.lang.String value) { - try { - java.lang.Integer.valueOf(value); - return true; - } catch (final NumberFormatException e) { - return false; - } - } -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java deleted file mode 100755 index e06cbc3..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java +++ /dev/null @@ -1,31 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -public class NullParameter extends Parameter { - - public NullParameter(final String description) { - super(description, ArgumentCount.NONE); - } - - @Override - public java.lang.String describeFormat() { - return ""; - } - - @Override - public Boolean getValue() { - return isSpecified(); - } - - @Override - public boolean validate(final java.lang.String value) { - return true; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java deleted file mode 100755 index 7a72cf3..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java +++ /dev/null @@ -1,45 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -public class StringParameter extends Parameter { - - public final String defaultValue; - - public StringParameter(final String description) { - super(description, ArgumentCount.SINGLE); - defaultValue = null; - } - public StringParameter(final String description, String defaultValue) { - super(description, ArgumentCount.SINGLE); - this.defaultValue = defaultValue; - this.setSpecified(true); - } - - @Override - public java.lang.String describeFormat() { - return "string"; - } - - @Override - public String getValue() { - - if (arguments.isEmpty() && (defaultValue != null)) return defaultValue; - - if (arguments.size() == 1) return arguments.get(0); - - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - } - - @Override - public boolean validate(final java.lang.String value) { - return true; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java deleted file mode 100644 index 5e6b27f..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameters.java +++ /dev/null @@ -1,33 +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.commandline.parameterparser.parameter; - -import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; - -import java.util.List; - -public class StringParameters extends Parameter, StringParameters> { - - public StringParameters(final String description) { - super(description, ArgumentCount.MULTI); - } - - @Override - public java.lang.String describeFormat() { - return "one to many strings"; - } - - @Override - public List getValue() { - return arguments; - } - - @Override - public boolean validate(final java.lang.String value) { - return true; - } - -} diff --git a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java deleted file mode 100755 index 44cd67e..0000000 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ /dev/null @@ -1,68 +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.commandline.parameterparser; - -import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter; -import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; -import org.junit.Before; -import org.junit.Test; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -import static org.junit.Assert.*; - -public class ParserTest { - - Parser parser; - - @Before - public void setUp() { - parser = new Parser(); - } - - @Test - public void testParse() throws IOException { - - // define allowed parameters - final StringParameter helpParameter = parser.add(new StringParameter("Show help screen") - .addAliases("--help", "-h").setMandatory()); - - final StringParameter compileParameter = parser.add(new StringParameter("Compile code")) - .addAliases("--compile", "-c"); - - FileParameter fileParameter = parser.add(new FileParameter("Input file") - .addAliases("-i").mustExist()); - - createTemporaryFile(); - - // check help generation - parser.showHelp(); - - // parse arguments - parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"}); - - // --help was in the arguments - assertTrue(helpParameter.isSpecified()); - - // compile was not present - assertFalse(compileParameter.isSpecified()); - - // validate that help argument was "section" - assertEquals("section", helpParameter.getValue()); - - assertTrue(fileParameter.isSpecified()); - assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath()); - - } - - private void createTemporaryFile() throws IOException { - File fileWithSpaces = new File("/tmp/file with spaces"); - FileWriter writer = new FileWriter(fileWithSpaces); - writer.write("test"); - writer.close(); - } -} -- 2.20.1