private SingleCmdTool(String name, Terminal terminal, NamedCommand command) { super( CliToolConfig.config(name, SingleCmdTool.class) .cmds(cmd(command.name, command.getClass())) .build(), terminal); this.command = command; }
private MultiCmdTool(String name, Terminal terminal, NamedCommand... commands) { super(CliToolConfig.config(name, MultiCmdTool.class).cmds(cmds(commands)).build(), terminal); Map<String, Command> commandByName = new HashMap<>(); for (int i = 0; i < commands.length; i++) { commandByName.put(commands[i].name, commands[i]); } this.commands = unmodifiableMap(commandByName); }
public void testStopAtNonOptionParsing() throws Exception { final CliToolConfig.Cmd lenientCommand = cmd("lenient", CliTool.Command.Exit.class).stopAtNonOption(true).build(); final CliToolConfig.Cmd strictCommand = cmd("strict", CliTool.Command.Exit.class).stopAtNonOption(false).build(); final CliToolConfig config = CliToolConfig.config("elasticsearch", CliTool.class) .cmds(lenientCommand, strictCommand) .build(); final CaptureOutputTerminal terminal = new CaptureOutputTerminal(); final CliTool cliTool = new CliTool(config, terminal) { @Override protected Command parse(String cmdName, CommandLine cli) throws Exception { return new NamedCommand(cmdName, terminal) { @Override public ExitStatus execute(Settings settings, Environment env) throws Exception { return OK; } }; } }; // known parameters, no error assertStatus(cliTool.execute(args("lenient --verbose")), OK); assertStatus(cliTool.execute(args("lenient -v")), OK); // unknown parameters, no error assertStatus(cliTool.execute(args("lenient --unknown")), OK); assertStatus(cliTool.execute(args("lenient -u")), OK); // unknown parameters, error assertStatus(cliTool.execute(args("strict --unknown")), USAGE); assertThat( terminal.getTerminalOutput(), hasItem(containsString("Unrecognized option: --unknown"))); terminal.getTerminalOutput().clear(); assertStatus(cliTool.execute(args("strict -u")), USAGE); assertThat(terminal.getTerminalOutput(), hasItem(containsString("Unrecognized option: -u"))); }