diff --git a/jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java b/jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java index d30bd5865..39ef18272 100644 --- a/jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java +++ b/jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java @@ -24,9 +24,12 @@ public class LaunchOptions { @Option(name = "-s", aliases = {"--server"}, usage = "runs HTTP server to serve out baked site, if no is supplied will default to a folder called \"output\" in the current directory") private boolean runServer; - @Option(name = "-h", aliases = {"--help"}, usage = "prints this message") + @Option(name = "-h", aliases = {"--help"}, usage = "prints this message and exits") private boolean helpNeeded; + @Option(name = "-V", aliases = {"--version"}, usage = "prints the version identifier and exits") + private boolean versionNeeded; + @Option(name = "--reset", usage = "clears the local cache, enforcing rendering from scratch") private boolean clearCache; @@ -66,6 +69,10 @@ public boolean isHelpNeeded() { return helpNeeded || !(isBake() || isRunServer() || isInit() || source != null || destination != null); } + public boolean isVersionNeeded() { + return versionNeeded; + } + public boolean isRunServer() { return runServer; } diff --git a/jbake-core/src/main/java/org/jbake/launcher/Main.java b/jbake-core/src/main/java/org/jbake/launcher/Main.java index 5f970d439..2bbf395cf 100644 --- a/jbake-core/src/main/java/org/jbake/launcher/Main.java +++ b/jbake-core/src/main/java/org/jbake/launcher/Main.java @@ -7,6 +7,8 @@ import org.jbake.app.configuration.JBakeConfigurationFactory; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.slf4j.bridge.SLF4JBridgeHandler; import java.io.File; @@ -19,6 +21,8 @@ */ public class Main { + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); + private final String USAGE_PREFIX = "Usage: jbake"; private final String ALT_USAGE_PREFIX = " or jbake"; private Baker baker; @@ -84,15 +88,19 @@ protected void run(String[] args) { } protected void run(LaunchOptions res, JBakeConfiguration config) { - System.out.println("JBake " + config.getVersion() + " (" + config.getBuildTimeStamp() + ") [http://jbake.org]"); - System.out.println(); - if (res.isHelpNeeded()) { printUsage(res); // Help was requested, so we are done here return; } + if (res.isVersionNeeded()) { + System.out.println(getVersionString(config)); + return; + } + + LOGGER.info(getVersionString(config)); + if (res.isBake()) { baker.bake(config); } @@ -124,6 +132,10 @@ protected void run(LaunchOptions res, JBakeConfiguration config) { } + private String getVersionString(JBakeConfiguration config) { + return "JBake " + config.getVersion() + " (" + config.getBuildTimeStamp() + ") [http://jbake.org]"; + } + private LaunchOptions parseArguments(String[] args) { LaunchOptions res = new LaunchOptions(); CmdLineParser parser = new CmdLineParser(res); diff --git a/jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java b/jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java index 78d8060f4..69437fbd8 100644 --- a/jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java +++ b/jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java @@ -9,6 +9,16 @@ public class LaunchOptionsTest { + @Test + public void showHelpIfNoArguments() throws Exception { + String[] args = {}; + LaunchOptions res = new LaunchOptions(); + CmdLineParser parser = new CmdLineParser(res); + parser.parseArgument(args); + + assertThat(res.isHelpNeeded()).isTrue(); + } + @Test public void showHelp() throws Exception { String[] args = {"-h"};