Coder Social home page Coder Social logo

dgroup / term4j Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 0.0 186 KB

Simplify manipulations with CLI terminal(s) for Java-based applications

License: MIT License

Java 100.00%
java cli terminal console oop-principles oop elegantobjects lightweight simple hamcrest

term4j's Introduction

Maven Javadocs License: MIT Commit activity Hits-of-Code

Build Status 0pdd Dependency Status Known Vulnerabilities

DevOps By Rultor.com EO badge We recommend IntelliJ IDEA

Qulice SQ maintainability Codebeat Codacy Badge Codecov

What it is

term4j is an object-oriented primitives to simplify the manipulations with CLI terminal(s) for Java-based applications.

Principles

Design principles behind term4j.

How to use

Get the latest version here:

<dependency>
    <groupId>io.github.dgroup</groupId>
    <artifactId>term4j</artifactId>
    <version>${version}</version>
</dependency>

Java version required: 1.8+.

Interface Purpose Implementations / Related
Arg<T> Allows to fetch the application arguments StringOf, NumberOf, PathOf, FileOf, EnvOf, PropOf, Alt, Unchecked, etc
Input Wrap the raw manipulation with std in StdOf, Inmem, etc
Output Wrap the raw manipulation with std out StdOf, Inmem, etc
Highlighted The colored extension of Text for std out Green, Red, Yellow, etc
Runtime Wrap the raw manipulation with JVM runtime RuntimeOf, FakeRuntime, AppException, Stacktrace, etc

All examples below are using the following frameworks/libs:

  • Hamcrest - Library of matchers, which can be combined in to create flexible expressions of intent in tests.
  • cactoos - Object-Oriented Java primitives, as an alternative to Google Guava and Apache Commons.
  • cactoos-matchers - Object-Oriented Hamcrest matchers

Fetch the string/Text argument:

$ java -jar app.jar --key vOIkv7mzQV2UkV1
public static void main(String[] cargs) {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The argument '--key' has value 'vOIkv7mzQV2UkV1'",
        new StringOf("--key", args),
        new ArgHas<>("vOIkv7mzQV2UkV1")
    );
    // or
    final Arg<String> key = new StringOf("--key", args);
}

Fetch the numeric argument:

$ java -jar app.jar -t 10
public static void main(String[] cargs) throws ArgNotFoundException {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The argument '-t' has value '10'",
        new NumberOf("-t", args).toInt(),
        new IsEqual<>(10)
    );
    // or
    final int threads = new NumberOf("-t", args).toInt();
}

Fetch the argument as a java.nio.file.Path or java.io.File:

$ java -jar app.jar -f ./readme.md
public static void main(String[] cargs) throws ArgNotFoundException {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The argument '-f' has path './readme.md'",
        new PathOf("-f", args),
        new ArgHas<>(Paths.get(".", "readme.md"))
    );
    // or
    final Arg<Path> src = new PathOf("-f", args);
}

Fetch the environment variable:

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
public static void main(String[] cargs) throws ArgNotFoundException {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The environment variable 'JAVA_HOME' has 1.8.0_181",
        new EnvOf("JAVA_HOME").value(),
        new StringContains("1.8.0_181")
    );
    // or
    final Arg<String> jhome = new EnvOf("JAVA_HOME");
}

Fetch the application property:

$ java -Dlevel=debug -jar app.jar
public static void main(String[] cargs) throws ArgNotFoundException {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The application property 'level' is 'debug'",
        new PropOf("level"),
        new ArgHas<>("debug")
    );
    // or
    final Arg<String> verbose = new PropOf("level");
}

The alternative value in case if the argument wasn't specified:

$ java -jar app.jar
public static void main(String[] cargs) {
    final List<String> args = new ListOf<>(cargs);
    MatcherAssert.assertThat(
        "The argument '--key' is using default value 'vOIkv7mzQV2UkV1'",
        new Alt(
            new StringOf("--key", args),
            "vOIkv7mzQV2UkV1"
        ),
        new ArgHas<>("vOIkv7mzQV2UkV1")
    );
    // or
    final Arg<String> key = new Alt(
        new StringOf("--key", args), "vOIkv7mzQV2UkV1"
    );
}

Wrap the std out, for example for unit testing purposes:

    /**
     * Simulate the STD input procedure.
     */
    @Test
    public void readFromStdin() {
        /**
         * The standard system input (stdin) which keeps the expected input lines in-memory
         *  instead of direct manipulations with {@link System#in} or {@link Console}.
         */
        final Input stdin = new Stdin(
            new InputOf(String.format("line1%nline2"))
        );
        MatcherAssert.assertThat(
            "The 1st line was read from console",
            stdin.value(),
            new IsEqual("line1")
        );
        MatcherAssert.assertThat(
            "The 2nd line was read from console",
            stdin.value(),
            new IsEqual("line2")
        );
    }

Wrap the std out, for example for unit testing purposes:

    /**
     * Simulate the STD print procedure using {@link StringWriter}.
     */
    @Test
    public void printToWriter() {
        // Write 4 lines delimited by `\n` or `\r\n` to the StringWriter
        final StringWriter swter = new StringWriter();
        final Output std = new Stdout(swter);
        std.print("line1", "line2");
        std.print("line3", "line4");
        // Check that the result string has 4 lines
        MatcherAssert.assertThat(
            "4 lines of text were printed to the output",
            swter.toString(),
            new HasLines("line1", "line2", "line3", "line4")
        );
    }

Print colored text to the std out:

public static void main(String[] cargs) {
    System.out.printf(
        "%n Status: [%s|%s] %n", new Green("Passed"), new Red("Failed")
    );
}

Colored message

See more.

RuntimeOf

Exit from application using particular exit code:

public static void main(String[] cargs) {
    try {
        // application exception happens
    } catch (final AppException cause) {
        new RuntimeOf().shutdownWith(
            cause.exitCode()
        );
    }
}

term4j's People

Contributors

dependabot[bot] avatar dgroup avatar rultor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

term4j's Issues

AppException.java:90-91: Find a way how to avoid several...

The puzzle DEV-5d86c8b6 from #DEV has to be resolved:

* @todo #/DEV Find a way how to avoid several constructors which do the initialisation.
* Remote the PMD OnlyOneConstructorShouldDoInitialization violation.

The puzzle was created by @dgroup on 13-May-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Highlighted.java:33-39: Use jansi lib in order to print...

The puzzle DEV-0dc5d931 from #DEV has to be resolved:

* @todo #/DEV Use jansi lib in order to print colored text:
* - <em>Green</em>;
* - <em>Red</em>;
* - <em>Yellow</em>;
* - <em>White</em>;
* - <em>Black</em>;
* and <em>Bold</em> to make the text more expressively.

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

pom.xml:132: Activate de.thetaphi:forbiddenapis in order...

The puzzle DEV-08faa53a from #DEV has to be resolved:

term4j/pom.xml

Line 132 in 50156ab

<!-- @todo #/DEV Activate de.thetaphi:forbiddenapis in order to forbid the usage of forbidden API like static matchers, etc -->

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Arg.java:43-46: Multiple labels support: Replace 'String...

The puzzle DEV-f4ddbf1f from #DEV has to be resolved:

* @todo #/DEV Multiple labels support: Replace 'String label()' by object.
* Sometimes the arguments have several labels with short and long names
* like "-t" and "--threads". Now the arguments are supports only 1 label
* per argument.

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

package-info.java:34-35: Help: simple mechanism to print...

The puzzle DEV-7920a2ed from #DEV has to be resolved:

* @todo #/DEV Help: simple mechanism to print the help info from the classpath
* resource.

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Std.java:33-34: Fill 'Get started' section in readme.md:...

The puzzle DEV-64f09d93 from #DEV has to be resolved:

* @todo #/DEV Fill 'Get started' section in readme.md:
* "How to manipulate with cli std output within the unit tests".

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

pom.xml:110: Upgrade the hamcrest-all library to the...

The puzzle DEV-65a70c0d from #DEV has to be resolved:

term4j/pom.xml

Line 110 in 50156ab

<!-- @todo #/DEV Upgrade the hamcrest-all library to the latest release of hamcrest-java -->

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Arg.java:33-34: Fill 'Get started' section in readme.md:...

The puzzle DEV-78d7cf6b from #DEV has to be resolved:

* @todo #/DEV Fill 'Get started' section in readme.md:
* "How to manipulate with cli arguments".

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Envs.java:36: Rename Envelopes considering their types.

The puzzle DEV-2f3418a3 from #DEV has to be resolved:

* @todo #/DEV Rename Envelopes considering their types.

The puzzle was created by Yurii Dubinka on 03-Feb-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Arg.java:33: Arg should extends {@link org.cactoos.Scalar}

The puzzle DEV-eb957d7b from #DEV has to be resolved:

* @todo #/DEV Arg should extends {@link org.cactoos.Scalar}

The puzzle was created by @dgroup on 01-Mar-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

package-info.java:29-34: FirstIn argument (similar to...

The puzzle DEV-553def65 from #DEV has to be resolved:

* @todo #/DEV FirstIn argument (similar to switch statement) which allows to
* instantiate the first value from the list of the arguments. For example,
* the same parameter might be defined:
* - command-line argument <em>-prop</em>,
* - java property <em>-Dprop</em>
* - env property <em>${PROP}</em>

The puzzle was created by @dgroup on 20-Jan-19.

If you have any technical questions, don't ask me, submit new tickets instead. The task will be "done" when the problem is fixed and the text of the puzzle is removed from the source code. Here is more about PDD and about me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.