Search in sources :

Example 6 with RunLast

use of picocli.CommandLine.RunLast in project besu by hyperledger.

the class CommandLineUtilsTest method optionIsNotExpected.

@Test
public void optionIsNotExpected() {
    final AbstractTestCommand testCommand = new TestCommandWithDeps(mockLogger);
    testCommand.commandLine.parseWithHandlers(new RunLast(), defaultExceptionHandler(), "--option-enabled", "false", "--option2", "20", "--option4", "40");
    verifyOptionsConstraintLoggerCall(mockLogger, "--option2", "--option-enabled");
    assertThat(testCommand.optionEnabled).isFalse();
    assertThat(testCommand.option2).isEqualTo(20);
    assertThat(testCommand.option3).isEqualTo(3);
    assertThat(testCommand.option4).isEqualTo(40);
}
Also used : RunLast(picocli.CommandLine.RunLast) Test(org.junit.Test)

Example 7 with RunLast

use of picocli.CommandLine.RunLast in project besu by hyperledger.

the class CommandTestAbstract method parseCommand.

protected TestBesuCommand parseCommand(final InputStream in, final String... args) {
    // turn off ansi usage globally in picocli
    System.setProperty("picocli.ansi", "false");
    final TestBesuCommand besuCommand = new TestBesuCommand(mockLogger, () -> rlpBlockImporter, this::jsonBlockImporterFactory, (blockchain) -> rlpBlockExporter, mockRunnerBuilder, mockControllerBuilderFactory, mockBesuPluginContext, environment, storageService, securityModuleService, mockPkiBlockCreationConfigProvider, privacyPluginService);
    besuCommands.add(besuCommand);
    File defaultKeyFile = KeyPairUtil.getDefaultKeyFile(DefaultCommandValues.getDefaultBesuDataPath(besuCommand));
    try {
        Files.writeString(defaultKeyFile.toPath(), keyPair.getPrivateKey().toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    besuCommand.setBesuConfiguration(commonPluginConfiguration);
    // parse using Ansi.OFF to be able to assert on non formatted output results
    besuCommand.parse(new RunLast().useOut(outPrintStream).useAnsi(Ansi.OFF), besuCommand.exceptionHandler().useErr(errPrintStream).useAnsi(Ansi.OFF), in, args);
    return besuCommand;
}
Also used : RunLast(picocli.CommandLine.RunLast) IOException(java.io.IOException) File(java.io.File)

Example 8 with RunLast

use of picocli.CommandLine.RunLast in project ozone by apache.

the class TestOzoneDatanodeShell method executeDatanode.

private void executeDatanode(HddsDatanodeService hdds, String[] args) {
    LOG.info("Executing datanode command with args {}", Arrays.asList(args));
    CommandLine cmd = hdds.getCmd();
    IExceptionHandler2<List<Object>> exceptionHandler = new IExceptionHandler2<List<Object>>() {

        @Override
        public List<Object> handleParseException(ParameterException ex, String[] args) {
            throw ex;
        }

        @Override
        public List<Object> handleExecutionException(ExecutionException ex, ParseResult parseResult) {
            throw ex;
        }
    };
    cmd.parseWithHandlers(new RunLast(), exceptionHandler, args);
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) RunLast(picocli.CommandLine.RunLast) List(java.util.List) ParameterException(picocli.CommandLine.ParameterException) IExceptionHandler2(picocli.CommandLine.IExceptionHandler2) ExecutionException(picocli.CommandLine.ExecutionException)

Example 9 with RunLast

use of picocli.CommandLine.RunLast in project mixcr by milaboratory.

the class Main method handleParseResult.

public static void handleParseResult(ParseResult parseResult, String[] args) {
    ExceptionHandler<Object> exHandler = new ExceptionHandler<>();
    exHandler.andExit(1);
    RunLast runLast = new RunLast() {

        @Override
        protected List<Object> handle(ParseResult parseResult) throws CommandLine.ExecutionException {
            List<CommandLine> parsedCommands = parseResult.asCommandLineList();
            CommandLine commandLine = parsedCommands.get(parsedCommands.size() - 1);
            Object command = commandLine.getCommand();
            try {
                if (command instanceof CommandSpec && ((CommandSpec) command).userObject() instanceof Runnable) {
                    ((Runnable) ((CommandSpec) command).userObject()).run();
                    return new ArrayList<>();
                }
                return super.handle(parseResult);
            } catch (ParameterException ex) {
                throw ex;
            } catch (CommandLine.ExecutionException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new CommandLine.ExecutionException(commandLine, "Error while running command (" + command + "): " + ex, ex);
            }
        }
    };
    try {
        runLast.handleParseResult(parseResult);
    } catch (ParameterException ex) {
        exHandler.handleParseException(ex, args);
    } catch (CommandLine.ExecutionException ex) {
        exHandler.handleExecutionException(ex, parseResult);
    }
}
Also used : ParseResult(picocli.CommandLine.ParseResult) ArrayList(java.util.ArrayList) ParameterException(picocli.CommandLine.ParameterException) ValidationException(com.milaboratory.cli.ValidationException) CommandLine(picocli.CommandLine) CommandSpec(picocli.CommandLine.Model.CommandSpec) RunLast(picocli.CommandLine.RunLast) ParameterException(picocli.CommandLine.ParameterException)

Example 10 with RunLast

use of picocli.CommandLine.RunLast in project ozone by apache.

the class TestOzoneShellHA method execute.

private void execute(GenericCli shell, String[] args) {
    LOG.info("Executing OzoneShell command with args {}", Arrays.asList(args));
    CommandLine cmd = shell.getCmd();
    IExceptionHandler2<List<Object>> exceptionHandler = new IExceptionHandler2<List<Object>>() {

        @Override
        public List<Object> handleParseException(ParameterException ex, String[] args) {
            throw ex;
        }

        @Override
        public List<Object> handleExecutionException(ExecutionException ex, ParseResult parseRes) {
            throw ex;
        }
    };
    // Since there is no elegant way to pass Ozone config to the shell,
    // the idea is to use 'set' to place those OM HA configs.
    String[] argsWithHAConf = getHASetConfStrings(args);
    cmd.parseWithHandlers(new RunLast(), exceptionHandler, argsWithHAConf);
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) RunLast(picocli.CommandLine.RunLast) List(java.util.List) ArrayList(java.util.ArrayList) ParameterException(picocli.CommandLine.ParameterException) IExceptionHandler2(picocli.CommandLine.IExceptionHandler2) ExecutionException(picocli.CommandLine.ExecutionException)

Aggregations

RunLast (picocli.CommandLine.RunLast)13 CommandLine (picocli.CommandLine)6 Test (org.junit.Test)5 ParameterException (picocli.CommandLine.ParameterException)3 ParseResult (picocli.CommandLine.ParseResult)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutionException (picocli.CommandLine.ExecutionException)2 IExceptionHandler2 (picocli.CommandLine.IExceptionHandler2)2 ValidationException (com.milaboratory.cli.ValidationException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringPrintWriter (net.sf.jstuff.core.io.StringPrintWriter)1 RlpBlockExporter (org.hyperledger.besu.chainexport.RlpBlockExporter)1 JsonBlockImporter (org.hyperledger.besu.chainimport.JsonBlockImporter)1 RlpBlockImporter (org.hyperledger.besu.chainimport.RlpBlockImporter)1 BesuCommand (org.hyperledger.besu.cli.BesuCommand)1 BesuController (org.hyperledger.besu.controller.BesuController)1 BesuPluginContextImpl (org.hyperledger.besu.services.BesuPluginContextImpl)1 MetaborgException (org.metaborg.core.MetaborgException)1