Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help command improvements #1541

Merged
merged 11 commits into from
Oct 2, 2015
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package org.bladerunnerjs.plugin.commands.core;

import org.bladerunnerjs.api.BRJS;
import org.bladerunnerjs.api.model.exception.command.ArgumentParsingException;
import org.bladerunnerjs.api.model.exception.command.CommandArgumentsException;
import org.bladerunnerjs.api.model.exception.command.CommandOperationException;
import org.bladerunnerjs.api.plugin.CommandPlugin;
import org.bladerunnerjs.api.plugin.JSAPArgsParsingCommandPlugin;
import org.bladerunnerjs.api.spec.engine.SpecTest;
import org.bladerunnerjs.plugin.commands.core.HelpCommand;
import org.bladerunnerjs.testing.utility.MockCommandPlugin;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.UnflaggedOption;


public class HelpCommandTest extends SpecTest
{
HelpCommand helpCommand;
MockCommandPlugin command1;
MockCommandPlugin command2;
CommandPlugin command1;
CommandPlugin command2;

@Before
public void initTestObjects() throws Exception
Expand All @@ -40,11 +48,11 @@ public void helpCommandListsAllPossibleCommands() throws Exception
when(brjs).runCommand("help");
then(logging).containsConsoleText(
"Possible commands:",
" command1 : Command #1 description. ",
" command2 : Command #2 description. ",
" command1 : Command #1 description. ",
" command2 : Command #2 description. ",
" -----",
" help : Prints this list of commands ",
" version : Displays the BladeRunnerJS version ",
" help : Prints this list of commands. ",
" version : Displays the BladeRunnerJS version. ",
"",
"Supported flags:",
" --info",
Expand Down Expand Up @@ -79,12 +87,106 @@ public void helpForSpecificCommandShowsUsage() throws Exception
" Command #1 help.");
}

@Ignore
@Test
public void multilineHelpCommandIsCorrectlyFormatted() throws Exception
{
given(brjs).hasCommandPlugins( new MockCommandPlugin("command3", "Command #3 description.", "command-usage", "Command #3 help.\ncommand #3 line 2") )
.and(brjs).hasBeenCreated();
when(brjs).runCommand("help", "command3");
then(logging).containsConsoleText(
"Description:",
" Command #3 description.",
"",
"Usage:",
" brjs command3 command-usage",
"",
"Help:",
" Command #3 help.",
" command #3 line 2");
}

@Test
public void multilineHelpCommandIsCorrectlyFormattedWhenItContainsSomeWhitespace() throws Exception
{
given(brjs).hasCommandPlugins( new MockCommandPlugin("command4", "Command #4 description.", "command-usage", "Command #4 help.\n extra whitespace line\n \tand a tabbed line") )
.and(brjs).hasBeenCreated();
when(brjs).runCommand("help", "command4");
then(logging).containsConsoleText(
"Description:",
" Command #4 description.",
"",
"Usage:",
" brjs command4 command-usage",
"",
"Help:",
" Command #4 help.",
" extra whitespace line",
" \tand a tabbed line");
}

@Test
public void multilineHelpCommandIsCorrectlyFormattedWhenItContainsIncrementallyIndentedLines() throws Exception
{
given(brjs).hasCommandPlugins( new MockCommandPlugin("command4", "Command #4 description.", "command-usage", "line1\n line2\n line3") )
.and(brjs).hasBeenCreated();
when(brjs).runCommand("help", "command4");
then(logging).containsConsoleText(
"Description:",
" Command #4 description.",
"",
"Usage:",
" brjs command4 command-usage",
"",
"Help:",
" line1",
" line2",
" line3");
}

@Test
public void jsapArgsParsingCommandHelpMessageIsCorrectlyFormatted() throws Exception
{
given(brjs).hasCommandPlugins(new MockJSAPArgsParsingCommandPlugin())
.and(brjs).hasBeenCreated();
when(brjs).runCommand("help", "jsapCommand");
then(logging).containsConsoleText(
"Description:",
" mock jsap command",
"",
"Usage:",
" brjs jsapCommand [<arg1>] [<arg2>]",
"",
"Help:",
" [<arg1>]",
" the 1st argument",
" [<arg2>]",
" the 2nd argument");
}

@Test
public void commandIsAutomaticallyLoaded() throws Exception
{
given(brjs).hasBeenAuthenticallyCreated();
when(brjs).runCommand("help");
then(exceptions).verifyNoOutstandingExceptions();
}


private class MockJSAPArgsParsingCommandPlugin extends JSAPArgsParsingCommandPlugin {
public void setBRJS(BRJS brjs) {}
public String getCommandName() {
return "jsapCommand";
}
public String getCommandDescription() {
return "mock jsap command";
}
protected int doCommand(JSAPResult parsedArgs) throws CommandArgumentsException, CommandOperationException {
return 0;
}
protected void configureArgsParser(JSAP argsParser) throws JSAPException {
argsParser.registerParameter(new UnflaggedOption("arg1").setHelp("the 1st argument"));
argsParser.registerParameter(new UnflaggedOption("arg2").setHelp("the 2nd argument"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bladerunnerjs.api.model.exception.command.ArgumentParsingException;
import org.bladerunnerjs.api.model.exception.command.CommandArgumentsException;
import org.bladerunnerjs.api.model.exception.command.CommandOperationException;
Expand Down Expand Up @@ -70,7 +71,14 @@ public final String getCommandUsage() {

@Override
public String getCommandHelp() {
return argsParser.getHelp();
StringBuilder help = new StringBuilder();
for (String line : argsParser.getHelp().split("\n")) {
if (line.length() >= 2 && Character.isWhitespace(line.charAt(0)) && Character.isWhitespace(line.charAt(1))) {
line = line.substring(2);
}
help.append(line+"\n");
}
return help.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bladerunnerjs.api.BRJS;
import org.bladerunnerjs.api.logging.Logger;
import org.bladerunnerjs.api.model.exception.command.CommandArgumentsException;
Expand Down Expand Up @@ -42,7 +43,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Prints this list of commands";
return "Prints this list of commands.";
}

@Override
Expand Down Expand Up @@ -83,6 +84,10 @@ private void getHelpCommandResponse()
logger.println(" --show-pkg (show which class each log line comes from)");
logger.println(" --no-stats (permenantly disable anonymous tracking)");
logger.println(" --stats (permenantly enable anonymous tracking)");
logger.println("");

logger.println("You can get detailed help for any command by typing:");
logger.println(" brjs help <command-name>");
}

private void getHelpForSpecificCommand(String commandName) throws CommandArgumentsException
Expand All @@ -100,9 +105,19 @@ private void getHelpForSpecificCommand(String commandName) throws CommandArgumen
logger.println("");

logger.println("Help:");
logger.println(" " + command.getCommandHelp());
logger.println( getFormattedHelpMessage(command) );
}

private String getFormattedHelpMessage(CommandPlugin command)
{
String commandHelp = command.getCommandHelp();
StringBuilder formattedHelp = new StringBuilder();
for (String line : StringUtils.split(commandHelp, "\n")) {
formattedHelp.append( StringUtils.repeat(' ', 2) + line + "\n" );
}
return formattedHelp.toString();
}

public String getHelpMessageFormatString()
{
int commandNameSize = getLongestCommandName() + 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Displays the BladeRunnerJS version";
return "Displays the BladeRunnerJS version.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Duplicate an existing CSS theme";
return "Duplicate an existing CSS theme.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Copies WEB-INF files into an application ready for adding servlets and other J2EE features";
return "Copies WEB-INF files into an application ready for adding servlets and other J2EE features.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getCommandName() {

@Override
public String getCommandDescription() {
return "Generate JsDocs for a given application";
return "Generate JsDocs for a given application.";
}

@Override
Expand Down