Search in sources :

Example 1 with CLIExceptionCapturer

use of com.quorum.tessera.cli.CLIExceptionCapturer in project tessera by ConsenSys.

the class PicoCliDelegate method execute.

public CliResult execute(String... args) throws Exception {
    LOGGER.debug("Execute with args [{}]", String.join(",", args));
    final CommandLine commandLine = new CommandLine(TesseraCommand.class);
    final CLIExceptionCapturer exceptionCapturer = new CLIExceptionCapturer();
    commandLine.addSubcommand(new CommandLine(CommandLine.HelpCommand.class));
    commandLine.addSubcommand(new CommandLine(VersionCommand.class));
    commandLine.addSubcommand(new CommandLine(KeyGenCommand.class, new KeyGenCommandFactory()));
    commandLine.addSubcommand(new CommandLine(KeyUpdateCommand.class, new KeyUpdateCommandFactory()));
    commandLine.registerConverter(Config.class, new ConfigConverter()).registerConverter(ArgonOptions.class, new ArgonOptionsConverter()).setSeparator(" ").setCaseInsensitiveEnumValuesAllowed(true).setExecutionExceptionHandler(exceptionCapturer).setParameterExceptionHandler(exceptionCapturer).setStopAtUnmatched(false);
    try {
        // parse the args so that we can print usage help if no cmd args were provided
        final CommandLine.ParseResult parseResult = commandLine.parseArgs(args);
        final List<CommandLine> l = parseResult.asCommandLineList();
        final CommandLine lastCmd = l.get(l.size() - 1);
        // print help if no args were provided
        if (lastCmd.getParseResult().matchedArgs().size() == 0 && !"help".equals(lastCmd.getCommandName()) && !"version".equals(lastCmd.getCommandName())) {
            lastCmd.usage(lastCmd.getOut());
        } else {
            commandLine.execute(args);
        }
    } catch (CommandLine.ParameterException ex) {
        exceptionCapturer.handleParseException(ex, args);
    }
    // if an exception occurred, throw it to to the upper levels where it gets handled
    if (exceptionCapturer.getThrown() != null) {
        throw exceptionCapturer.getThrown();
    }
    return (CliResult) Optional.ofNullable(commandLine.getExecutionResult()).orElse(new CliResult(0, true, null));
}
Also used : ConfigConverter(com.quorum.tessera.cli.parsers.ConfigConverter) ArgonOptions(com.quorum.tessera.config.ArgonOptions) CommandLine(picocli.CommandLine) CLIExceptionCapturer(com.quorum.tessera.cli.CLIExceptionCapturer) CliResult(com.quorum.tessera.cli.CliResult)

Aggregations

CLIExceptionCapturer (com.quorum.tessera.cli.CLIExceptionCapturer)1 CliResult (com.quorum.tessera.cli.CliResult)1 ConfigConverter (com.quorum.tessera.cli.parsers.ConfigConverter)1 ArgonOptions (com.quorum.tessera.config.ArgonOptions)1 CommandLine (picocli.CommandLine)1