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

Feature/option improvements #615

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public class LaunchOptions {
@Option(name = "-s", aliases = {"--server"}, usage = "runs HTTP server to serve out baked site, if no <value> 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;

Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 15 additions & 3 deletions jbake-core/src/main/java/org/jbake/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Expand Down