From 5aba0db9f94d0f68511511ac5bbe0dade0e0fd63 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 26 Jun 2022 02:31:25 +0300 Subject: [PATCH 1/1] Initial commit --- .gitignore | 7 + COPYING | 121 +++++++ doc/index.html | 321 ++++++++++++++++++ doc/index.org | 59 ++++ pom.xml | 125 +++++++ .../commons/cli_helper/CLIHelper.java | 102 ++++++ .../parameter_parser/ArgumentCount.java | 13 + .../parameter_parser/Parameter.java | 204 +++++++++++ .../cli_helper/parameter_parser/Parser.java | 95 ++++++ .../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 ++ .../parameter_parser/ParserTest.java | 68 ++++ tools/commit and push | 11 + tools/open with IntelliJ IDEA | 18 + tools/update web site | 17 + 22 files changed, 1579 insertions(+) create mode 100755 .gitignore create mode 100644 COPYING create mode 100644 doc/index.html create mode 100644 doc/index.org create mode 100644 pom.xml create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java create mode 100755 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java create mode 100644 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java create mode 100755 src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java create mode 100755 tools/commit and push create mode 100755 tools/open with IntelliJ IDEA create mode 100755 tools/update web site diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..aea84ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/.settings/ +/.project +/.classpath +/target/ +/doc/apidocs/ +/.idea/ +/*.iml diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/COPYING @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/doc/index.html b/doc/index.html new file mode 100644 index 0000000..669f211 --- /dev/null +++ b/doc/index.html @@ -0,0 +1,321 @@ + + + + + + + +CLI Helper - library to help implementing commandline interfaces + + + + + + +
+

CLI Helper - library to help implementing commandline interfaces

+
+

Table of Contents

+ +
+ +
+

1. General

+
+ +
+ +
+

1.1. Source code

+
+ +
+
+
+ +
+

2. Library contents

+
+
    +
  • See JavaDoc.
  • + +
  • +Commandline Interface helper +

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

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

3. Usage

+
+

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

+ +

+Maven pom.xml file snippet: +

+ +
+
<dependencies>
+    ...
+    <dependency>
+        <groupId>eu.svjatoslav</groupId>
+        <artifactId>cli-helper</artifactId>
+        <version>1.0</version>
+    </dependency>
+    ...
+</dependencies>
+
+
+<repositories>
+    ...
+    <repository>
+        <id>svjatoslav.eu</id>
+        <name>Svjatoslav repository</name>
+        <url>http://www2.svjatoslav.eu/maven/</url>
+    </repository>
+    ...
+</repositories>
+
+
+
+
+
+
+

Author: Svjatoslav Agejenko

+

Created: 2022-06-26 Sun 02:29

+

Validate

+
+ + diff --git a/doc/index.org b/doc/index.org new file mode 100644 index 0000000..64f7146 --- /dev/null +++ b/doc/index.org @@ -0,0 +1,59 @@ +#+TITLE: CLI Helper - library to help implementing commandline interfaces + +* General +- This program is free software: released under Creative Commons Zero + (CC0) license + +- Program author: + - Svjatoslav Agejenko + - Homepage: https://svjatoslav.eu + - Email: mailto://svjatoslav@svjatoslav.eu + +- [[https://www.svjatoslav.eu/projects/][Other software projects hosted at svjatoslav.eu]] + +** Source code +- [[https://www2.svjatoslav.eu/gitweb/?p=cli-helper.git;a=snapshot;h=HEAD;sf=tgz][Download latest snapshot in TAR GZ format]] + +- [[https://www2.svjatoslav.eu/gitweb/?p=cli-helper.git;a=summary][Browse Git repository online]] + +- Clone Git repository using command: + : git clone https://www2.svjatoslav.eu/git/cli-helper.git + +- See [[https://www3.svjatoslav.eu/projects/cli-helper/apidocs/][JavaDoc]] + +* Library contents +- See [[https://www3.svjatoslav.eu/projects/svjatoslav_commons/apidocs/][JavaDoc]]. + +- Commandline Interface helper + : eu.svjatoslav.commons.cli_helper.CLIHelper + +- Commandline arguments parser and validator. + : eu.svjatoslav.commons.cli_helper.parameter_parser + +* Usage +Instructions to embed svjatoslav-commons in your project as a library: + +Maven pom.xml file snippet: + +#+BEGIN_SRC xml + + ... + + eu.svjatoslav + cli-helper + 1.0 + + ... + + + + + ... + + svjatoslav.eu + Svjatoslav repository + http://www2.svjatoslav.eu/maven/ + + ... + +#+END_SRC diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..60d9cd4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,125 @@ + + 4.0.0 + eu.svjatoslav + cli-helper + 1.0-SNAPSHOT + jar + CLI helper + Helper library for implementing commandline interface + http://www2.svjatoslav.eu/gitbrowse/cli-helper/doc/index.html + + + svjatoslav.eu + http://svjatoslav.eu + + + + UTF-8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + UTF-8 + + + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + + attach-javadocs + + jar + + + + + + + + + + + + + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.5.2 + + + org.apache.maven.scm + maven-scm-provider-gitexe + 1.9.4 + + + + + + + + + org.apache.maven.wagon + wagon-ssh-external + 2.6 + + + + + + + junit + junit + 4.13.2 + test + + + + + + svjatoslav.eu + svjatoslav.eu + scpexe://svjatoslav.eu:10006/srv/maven + + + svjatoslav.eu + svjatoslav.eu + scpexe://svjatoslav.eu:10006/srv/maven + + + + + scm:git:ssh://n0@svjatoslav.eu:10006/home/n0/git/cli-helper.git + scm:git:ssh://n0@svjatoslav.eu:10006/home/n0/git/cli-helper.git + HEAD + + + diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java b/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java new file mode 100755 index 0000000..184585b --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/CLIHelper.java @@ -0,0 +1,102 @@ +/* + * 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; + +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/cli_helper/parameter_parser/ArgumentCount.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java new file mode 100755 index 0000000..1d830a3 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ArgumentCount.java @@ -0,0 +1,13 @@ +/* + * 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; + +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/cli_helper/parameter_parser/Parameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java new file mode 100755 index 0000000..92c5c81 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parameter.java @@ -0,0 +1,204 @@ +/* + * 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; + + 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/Parser.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java new file mode 100755 index 0000000..639be0f --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/Parser.java @@ -0,0 +1,95 @@ +/* + * 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; + +/** + * 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/cli_helper/parameter_parser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java new file mode 100755 index 0000000..c76b146 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameter.java @@ -0,0 +1,68 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/DirectoryParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java new file mode 100755 index 0000000..0927aaf --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/DirectoryParameters.java @@ -0,0 +1,65 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/ExistenceType.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java new file mode 100755 index 0000000..0b6b4bc --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/ExistenceType.java @@ -0,0 +1,17 @@ +/* + * 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.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/cli_helper/parameter_parser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java new file mode 100755 index 0000000..5435bc3 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameter.java @@ -0,0 +1,72 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/FileParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java new file mode 100755 index 0000000..850114a --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FileParameters.java @@ -0,0 +1,49 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.Parameter; + +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; + +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/cli_helper/parameter_parser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java new file mode 100755 index 0000000..130d360 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/IntegerParameter.java @@ -0,0 +1,38 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java new file mode 100755 index 0000000..ca234af --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/NullParameter.java @@ -0,0 +1,31 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java new file mode 100755 index 0000000..c5ed383 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameter.java @@ -0,0 +1,45 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/parameter/StringParameters.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java new file mode 100644 index 0000000..1a3e410 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/StringParameters.java @@ -0,0 +1,33 @@ +/* + * 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.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.ArgumentCount; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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/cli_helper/parameter_parser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java new file mode 100755 index 0000000..ab87cae --- /dev/null +++ b/src/test/java/eu/svjatoslav/commons/cli_helper/parameter_parser/ParserTest.java @@ -0,0 +1,68 @@ +/* + * 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 eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileParameter; +import eu.svjatoslav.commons.cli_helper.parameter_parser.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(); + } +} diff --git a/tools/commit and push b/tools/commit and push new file mode 100755 index 0000000..057b511 --- /dev/null +++ b/tools/commit and push @@ -0,0 +1,11 @@ +#!/bin/bash +cd "${0%/*}"; if [ "$1" != "T" ]; then gnome-terminal -e "'$0' T"; exit; fi; + +cd .. + +cola +git push + +echo "" +echo "Press ENTER to close this window." +read diff --git a/tools/open with IntelliJ IDEA b/tools/open with IntelliJ IDEA new file mode 100755 index 0000000..de9bae5 --- /dev/null +++ b/tools/open with IntelliJ IDEA @@ -0,0 +1,18 @@ +#!/bin/bash + +# +# This is a helper bash script that starts IntelliJ with the current project. +# Script is written is such a way that you can simply click on it in file +# navigator to run it. +# +# +# Script assumes: +# +# + GNU operating system +# + IntelliJ is installed and commandline launcher "idea" is enabled. +# + +cd "${0%/*}" +cd .. + +setsid idea . &>/dev/null diff --git a/tools/update web site b/tools/update web site new file mode 100755 index 0000000..aebafec --- /dev/null +++ b/tools/update web site @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ "$1" != "T" ]; then gnome-terminal -e "'$0' T"; exit; fi + +cd "${0%/*}" +cd .. + +mvn clean package + +rm -rf doc/apidocs/ +cp -r target/apidocs/ doc/ + +rsync -avz --delete -e 'ssh -p 10006' doc/ n0@www3.svjatoslav.eu:/mnt/big/projects/cli-helper/ + +echo +echo "Press ENTER to close this terminal" +read -- 2.20.1