All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.instaclustr.picocli.CLIApplication Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.instaclustr.picocli;

import javax.validation.ValidationException;
import java.io.PrintWriter;
import java.util.concurrent.Callable;

import picocli.CommandLine;

public abstract class CLIApplication extends JarManifestVersionProvider {

    public static int execute(final Runnable runnable, String... args) {
        return execute(new CommandLine(runnable), args);
    }

    public static int execute(final Callable callable, String... args) {
        return execute(new CommandLine(callable), args);
    }

    public static int execute(CommandLine commandLine, String... args) {
        return commandLine
                .setErr(new PrintWriter(System.err))
                .setOut(new PrintWriter(System.err))
                .setColorScheme(new CommandLine.Help.ColorScheme.Builder().ansi(CommandLine.Help.Ansi.ON).build())
                .setExecutionExceptionHandler((ex, cmdLine, parseResult) -> {

                    if (ex instanceof ValidationException) {
                        return 1;
                    }

                    ex.printStackTrace();

                    return 1;
                })
                .execute(args);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy