Merge pull request #17938 from rjernst/plugin_command_help
Cli: Improve output for usage errors
This commit is contained in:
commit
d56d8b03c8
|
@ -56,6 +56,9 @@ public abstract class Command {
|
|||
terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
|
||||
return ExitCodes.USAGE;
|
||||
} catch (UserError e) {
|
||||
if (e.exitCode == ExitCodes.USAGE) {
|
||||
printHelp(terminal);
|
||||
}
|
||||
terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
|
||||
return e.exitCode;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,16 @@ public class CommandTests extends ESTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
static class UsageErrorCommand extends Command {
|
||||
UsageErrorCommand() {
|
||||
super("Throws a usage error");
|
||||
}
|
||||
@Override
|
||||
protected void execute(Terminal terminal, OptionSet options) throws Exception {
|
||||
throw new UserError(ExitCodes.USAGE, "something was no good");
|
||||
}
|
||||
}
|
||||
|
||||
static class NoopCommand extends Command {
|
||||
boolean executed = false;
|
||||
NoopCommand() {
|
||||
|
@ -120,4 +130,15 @@ public class CommandTests extends ESTestCase {
|
|||
assertEquals(output, ExitCodes.DATA_ERROR, status);
|
||||
assertTrue(output, output.contains("ERROR: Bad input"));
|
||||
}
|
||||
|
||||
public void testUsageError() throws Exception {
|
||||
MockTerminal terminal = new MockTerminal();
|
||||
UsageErrorCommand command = new UsageErrorCommand();
|
||||
String[] args = {};
|
||||
int status = command.main(args, terminal);
|
||||
String output = terminal.getOutput();
|
||||
assertEquals(output, ExitCodes.USAGE, status);
|
||||
assertTrue(output, output.contains("Throws a usage error"));
|
||||
assertTrue(output, output.contains("ERROR: something was no good"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue