Add examples to the CLI

This commit updates the help command to also show some example(s) to
illustrate how the command can be used. The commit also defines useful
examples for the init command

Fixes gh-1809
pull/1815/head
Stephane Nicoll 10 years ago
parent fb0d9d2a2e
commit 0061f237b8

@ -29,6 +29,8 @@ import org.springframework.boot.cli.command.options.OptionHelp;
*/
public abstract class AbstractCommand implements Command {
protected static final String NEW_LINE = System.getProperty("line.separator");
private final String name;
private final String description;
@ -58,6 +60,11 @@ public abstract class AbstractCommand implements Command {
return null;
}
@Override
public String getExamples() {
return null;
}
@Override
public String getHelp() {
return null;

@ -26,6 +26,7 @@ import org.springframework.boot.cli.command.status.ExitStatus;
*
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
* @see #run(String...)
*/
public interface Command {
@ -47,6 +48,13 @@ public interface Command {
*/
String getUsageHelp();
/**
* Return some examples for the command. This can be a multi-lined string with
* one example per line, starting with the description and ending with the
* example.
*/
String getExamples();
/**
* Gets full help text for the command, e.g. a longer description and one line per
* option.

@ -101,6 +101,12 @@ public class HelpCommand extends AbstractCommand {
+ " " + command.getUsageHelp());
Log.info("");
}
if (command.getExamples() != null) {
Log.info("example(s):");
Log.info("");
Log.info(command.getExamples());
Log.info("");
}
if (command.getHelp() != null) {
Log.info(command.getHelp());
}

@ -53,6 +53,16 @@ public class InitCommand extends OptionParsingCommand {
return "[options] [location]";
}
@Override
public String getExamples() {
StringBuilder sb = new StringBuilder();
sb.append("Lists the capabilities of the service: spring init --list").append(NEW_LINE);
sb.append("Creates a default project: spring init").append(NEW_LINE);
sb.append("Creates a web my-app.zip: spring init -d=web my-app.zip").append(NEW_LINE);
sb.append("Creates a web/data-jpa gradle project unpacked: spring init -d=web,jpa --build=gradle my-dir/");
return sb.toString();
}
static class InitOptionHandler extends OptionHandler {
private final ServiceCapabilitiesReportGenerator serviceCapabilitiesReport;

Loading…
Cancel
Save