/command-line-util

CommandLineUtil to get easy access to command line unix/windows

Primary LanguageJavaApache License 2.0Apache-2.0

command-line-util

Build Maintainable Coverage Issues Commit Dependencies License Central Tag Javadoc Size Label

Description

Simple, small, native library for:

Usage

<dependency>
    <groupId>berlin.yuna</groupId>
    <artifactId>command-line-util</artifactId>
    <version>1.0.0</version>
</dependency>

[Example] Argument Parser

public class ArgumentParserTest {
    
    public void parseArgsTest() {
        //false == without environment variables
        final ArgumentReader args = parseArgs(false, "mvn clean install --Dencoding=\"UTF-8\" --javaVersion 8 -v=true --args=1,2,3", "--args=4,5");
        args.size(); // = 4
        args.getCommands(); // = 3
        args.hasCommand("install"); // = true

        //ARGUMENTS & TYPES
        args.getString("Dencoding"); // = "UTF-8"
        args.getBoolean("v"); // = true
        args.getLong("javaVersion"); // = 8
        args.getDouble("javaVersion"); // = 8.0

        //ARGUMENTS & SPLITTING & INDEXING
        args.getLong("-", 1, "Dencoding"); // = 8

        //DUPLICATES & LISTING & INDEXING
        args.getString("args"); // = "1,2,3"
        args.getString(1, "args"); // = "4,5"
        args.getStrings(',', "args"); // = ["1","2","3","4","5"]
        args.getLongs(',', "args"); // = [1,2,3,4,5]
        validateReadmeExample(args);
    }
}

[Example] Terminal execute command

new Terminal()
    .consumerInfo(System.out::println) //optional listener [Consumer<String>]
    .consumerError(System.err::println) //optional listener [Consumer<String>]
    .consoleInfo() //optional returns all info output in a String
    .consoleError() //optional returns all error output in a String
    .clearConsole() //optional clears previous consoleInfo/consoleError console
    .dir("myWorinkDirectory") //optional sets default working directory
    .timeoutMs(512) //optional to limit command or for workaround when commands are too fast to return exit status
    .breakOnError(false) //optional - only with timeoutMs possible
    .execute("echo Howdy") //executes the command
    .process //optional returns java Process;

[Example] Operating system tools

//Enum [ARM, LINUX, MAC, WINDOWS, SOLARIS, UNKNOWN]
OperatingSystem os = SystemUtil.getOsType();

//Kill process
killProcessByName("tomcat");

//Translates and saves [PosixFilePermission]s to generic
SystemUtil.setFilePermissions(file, OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);

//Read file (tries every charset)
String content SystemUtil.readFile(path);
List<String> contentLines SystemUtil.readFileLines(path);

//Delete dir recursively
boolean removed = SystemUtil.deleteDirectory

TODO

  • Timeout test
  • waitFor - some commands are too fast to get the exit status - workaround is to run terminal with .timeout().execute

command-line-util