Skip to content

Commit

Permalink
switch to picocli
Browse files Browse the repository at this point in the history
  • Loading branch information
ancho committed Nov 16, 2018
1 parent 1903219 commit 24fd40e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 86 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -32,6 +32,7 @@ ext {
jade4jVersion = '1.2.7'
mockitoVersion = '2.23.0'
jsoupVersion = '1.11.3'
picocli = '3.8.0'

isTravis = (System.getenv("TRAVIS") == "true")
isTravisPullRequest = (System.getenv("TRAVIS_PULL_REQUEST")) != "false"
Expand Down
2 changes: 1 addition & 1 deletion jbake-core/build.gradle
Expand Up @@ -30,7 +30,7 @@ dependencies {

// cli specific dependencies
compile "org.eclipse.jetty:jetty-server:$jettyServerVersion", optional
compile "args4j:args4j:$args4jVersion", optional
compile "info.picocli:picocli:$picocli", optional
}

processResources {
Expand Down
27 changes: 15 additions & 12 deletions jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java
@@ -1,33 +1,36 @@
package org.jbake.launcher;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
import picocli.CommandLine;

import java.io.File;

@CommandLine.Command(
description = "JBake is a Java based, open source, static site/blog generator for developers & designers",
name = "jbake"
)
public class LaunchOptions {
@Argument(index = 0, usage = "source folder of site content (with templates and assets), if not supplied will default to current directory", metaVar = "<source>")
@CommandLine.Parameters(index = "0", description = "source folder of site content (with templates and assets), if not supplied will default to current directory", arity = "0..1")
private String source;

@Argument(index = 1, usage = "destination folder for output, if not supplied will default to a folder called \"output\" in the current directory", metaVar = "<destination>")
@CommandLine.Parameters(index = "1", description = "destination folder for output, if not supplied will default to a folder called \"output\" in the current directory", arity = "0..1")
private String destination;

@Option(name = "-b", aliases = {"--bake"}, usage = "performs a bake")
@CommandLine.Option(names = {"-b", "--bake"}, description = "performs a bake")
private boolean bake;

@Option(name = "-i", aliases = {"--init"}, usage = "initialises required folder structure with default templates (defaults to current directory if <value> is not supplied)")
@CommandLine.Option(names = {"-i", "--init"}, description = "initialises required folder structure with default templates (defaults to current directory if <value> is not supplied)", arity = "0..1")
private boolean init;

@Option(name = "-t", aliases = {"--template"}, usage = "use specified template engine for default templates (uses Freemarker if <value> is not supplied) ", depends = ("-i"))
@CommandLine.Option(names = {"-t", "--template"}, description = "use specified template engine for default templates (uses Freemarker if <value> is not supplied) ", arity = "1")
private String template;

@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")
@CommandLine.Option(names = {"-s", "--server"}, description = "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")
private boolean helpNeeded;
@CommandLine.Option(names = {"-h", "--help"}, description = "prints this message", usageHelp = true)
private boolean helpRequested;

@Option(name = "--reset", usage = "clears the local cache, enforcing rendering from scratch")
@CommandLine.Option(names = {"--reset"}, description = "clears the local cache, enforcing rendering from scratch")
private boolean clearCache;

public String getTemplate() {
Expand Down Expand Up @@ -63,7 +66,7 @@ public String getDestinationValue() {
}

public boolean isHelpNeeded() {
return helpNeeded || !(isBake() || isRunServer() || isInit() || source != null || destination != null);
return helpRequested || !(isBake() || isRunServer() || isInit() || source != null || destination != null);
}

public boolean isRunServer() {
Expand Down
26 changes: 3 additions & 23 deletions jbake-core/src/main/java/org/jbake/launcher/Main.java
Expand Up @@ -5,12 +5,10 @@
import org.jbake.app.JBakeException;
import org.jbake.app.configuration.JBakeConfiguration;
import org.jbake.app.configuration.JBakeConfigurationFactory;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.slf4j.bridge.SLF4JBridgeHandler;
import picocli.CommandLine;

import java.io.File;
import java.io.StringWriter;

/**
* Launcher for JBake.
Expand Down Expand Up @@ -125,29 +123,11 @@ protected void run(LaunchOptions res, JBakeConfiguration config) {
}

private LaunchOptions parseArguments(String[] args) {
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);

try {
parser.parseArgument(args);
} catch (final CmdLineException e) {
printUsage(res);
throw new JBakeException("Invalid commandline arguments: " + e.getMessage(), e);
}

return res;
return CommandLine.populateCommand(new LaunchOptions(), args);
}

private void printUsage(Object options) {
CmdLineParser parser = new CmdLineParser(options);
StringWriter sw = new StringWriter();
sw.append(USAGE_PREFIX + "\n");
sw.append(ALT_USAGE_PREFIX + " <source> <destination>\n");
sw.append(ALT_USAGE_PREFIX + " [OPTION]... [<value>...]\n\n");
sw.append("Options:");
System.out.println(sw.toString());
parser.getProperties().withUsageWidth(100);
parser.printUsage(System.out);
CommandLine.usage(options, System.out);
}

private void runServer(File path, int port) {
Expand Down
67 changes: 25 additions & 42 deletions jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java
@@ -1,7 +1,7 @@
package org.jbake.launcher;

import org.junit.Test;
import org.kohsuke.args4j.CmdLineParser;
import picocli.CommandLine;

import java.io.File;

Expand All @@ -10,97 +10,78 @@
public class LaunchOptionsTest {

@Test
public void showHelp() throws Exception {
public void showHelp() {
String[] args = {"-h"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);

LaunchOptions res = parseArgs(args);
assertThat(res.isHelpNeeded()).isTrue();
}

@Test
public void runServer() throws Exception {
public void runServer() {
String[] args = {"-s"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isRunServer()).isTrue();
}

@Test
public void runServerWithFolder() throws Exception {
public void runServerWithFolder() {
String[] args = {"-s", "/tmp"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isRunServer()).isTrue();
assertThat(res.getSource()).isEqualTo(new File("/tmp"));
}

@Test
public void init() throws Exception {
public void init() {
String[] args = {"-i"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isInit()).isTrue();
assertThat(res.getTemplate()).isEqualTo("freemarker");
}

@Test
public void initWithTemplate() throws Exception {
public void initWithTemplate() {
String[] args = {"-i", "-t", "foo"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isInit()).isTrue();
assertThat(res.getTemplate()).isEqualTo("foo");
}

@Test
public void initWithSourceDirectory() throws Exception {
public void initWithSourceDirectory() {
String[] args = {"-i", "/tmp"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isInit()).isTrue();
assertThat(res.getSourceValue()).isEqualTo("/tmp");
}

@Test
public void initWithTemplateAndSourceDirectory() throws Exception {
public void initWithTemplateAndSourceDirectory() {
String[] args = {"-i", "-t", "foo", "/tmp"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isInit()).isTrue();
assertThat(res.getTemplate()).isEqualTo("foo");
assertThat(res.getSourceValue()).isEqualTo("/tmp");
}

@Test
public void bake() throws Exception {
public void bake() {
String[] args = {"-b"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isBake()).isTrue();
}

@Test
public void bakeNoArgs() throws Exception {
public void bakeNoArgs() {
String[] args = {};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isHelpNeeded()).isTrue();
assertThat(res.isRunServer()).isFalse();
Expand All @@ -111,11 +92,9 @@ public void bakeNoArgs() throws Exception {
}

@Test
public void bakeWithArgs() throws Exception {
public void bakeWithArgs() {
String[] args = {"/tmp/source", "/tmp/destination"};
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
LaunchOptions res = parseArgs(args);

assertThat(res.isHelpNeeded()).isFalse();
assertThat(res.isRunServer()).isFalse();
Expand All @@ -124,4 +103,8 @@ public void bakeWithArgs() throws Exception {
assertThat(res.getSource()).isEqualTo(new File("/tmp/source"));
assertThat(res.getDestination()).isEqualTo(new File("/tmp/destination"));
}

private LaunchOptions parseArgs(String[] args) {
return CommandLine.populateCommand(new LaunchOptions(), args);
}
}
12 changes: 4 additions & 8 deletions jbake-core/src/test/java/org/jbake/launcher/MainTest.java
Expand Up @@ -10,10 +10,9 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import picocli.CommandLine;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -137,19 +136,16 @@ public void launchJettyWithCmdlineOverridingProperties() throws Exception {
final File expectedOutput = folder.newFolder("build","jbake");
final File configTarget = folder.newFolder("target","jbake");

String[] args = {sourceFolder.getPath(), expectedOutput.getPath(), "-s"};
String[] args = {"-s", sourceFolder.getPath(), expectedOutput.getPath()};
DefaultJBakeConfiguration configuration = stubConfig();
configuration.setDestinationFolder(configTarget);
main.run(stubOptions(args), configuration);

verify(mockJetty).run(expectedOutput.getPath(),"8820");
}

private LaunchOptions stubOptions(String[] args) throws CmdLineException {
LaunchOptions res = new LaunchOptions();
CmdLineParser parser = new CmdLineParser(res);
parser.parseArgument(args);
return res;
private LaunchOptions stubOptions(String[] args) {
return CommandLine.populateCommand(new LaunchOptions(), args);
}

private DefaultJBakeConfiguration stubConfig() throws ConfigurationException {
Expand Down

0 comments on commit 24fd40e

Please sign in to comment.