Merge pull request #17938 from rjernst/plugin_command_help

Cli: Improve output for usage errors
This commit is contained in:
Ryan Ernst 2016-04-22 14:58:13 -07:00
commit d56d8b03c8
2 changed files with 24 additions and 0 deletions

View File

@ -56,6 +56,9 @@ public abstract class Command {
terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage()); terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
return ExitCodes.USAGE; return ExitCodes.USAGE;
} catch (UserError e) { } catch (UserError e) {
if (e.exitCode == ExitCodes.USAGE) {
printHelp(terminal);
}
terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage()); terminal.println(Terminal.Verbosity.SILENT, "ERROR: " + e.getMessage());
return e.exitCode; return e.exitCode;
} }

View File

@ -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 { static class NoopCommand extends Command {
boolean executed = false; boolean executed = false;
NoopCommand() { NoopCommand() {
@ -120,4 +130,15 @@ public class CommandTests extends ESTestCase {
assertEquals(output, ExitCodes.DATA_ERROR, status); assertEquals(output, ExitCodes.DATA_ERROR, status);
assertTrue(output, output.contains("ERROR: Bad input")); 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"));
}
} }