Search in sources :

Example 1 with StringPrintWriter

use of net.sf.jstuff.core.io.StringPrintWriter in project copycat by vegardit.

the class CopyCatMain method main.

public static void main(final String[] args) throws Exception {
    Thread.currentThread().setName("main");
    // this is a small hack, but we need to evaluate the logging options before
    // any other component starts throwing exceptions, see https://github.com/remkop/picocli/issues/1295
    final var fileHandler = configureLogging(args);
    // enable ANSI coloring
    AnsiConsole.systemInstall();
    final var handler = new CommandLine(new CopyCatMain());
    handler.setCaseInsensitiveEnumValuesAllowed(true);
    handler.setExecutionStrategy(new RunLast());
    /*
       * custom exception handlers that use a logger instead of directly writing to stdout/stderr
       */
    handler.setParameterExceptionHandler((ex, args2) -> {
        if (args2.length == 0) {
            CommandLine.usage(handler, System.err);
            System.err.println();
            LOG.error(ex.getMessage());
        } else {
            LOG.error(ex.getMessage());
            try (var sw = new StringPrintWriter()) {
                UnmatchedArgumentException.printSuggestions(ex, sw);
                final var suggestions = sw.toString();
                if (Strings.isNotBlank(suggestions)) {
                    LOG.info(Strings.trim(suggestions));
                }
            }
            LOG.info("Execute 'copycat --help' for usage help.");
        }
        return 1;
    });
    handler.setExecutionExceptionHandler((ex, commandLine, parseResult) -> {
        if (LOG.isDebugEnabled()) {
            // log with stacktrace
            LOG.error(ex);
        } else {
            LOG.error(ex.getMessage());
        }
        return 1;
    });
    final var exitCode = handler.execute(args);
    if (fileHandler != null) {
        fileHandler.close();
    }
    System.exit(exitCode);
}
Also used : StringPrintWriter(net.sf.jstuff.core.io.StringPrintWriter) CommandLine(picocli.CommandLine) RunLast(picocli.CommandLine.RunLast)

Aggregations

StringPrintWriter (net.sf.jstuff.core.io.StringPrintWriter)1 CommandLine (picocli.CommandLine)1 RunLast (picocli.CommandLine.RunLast)1