From 24fd40e32371fcc415e9bc47cc3725450f21d564 Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Sun, 19 Aug 2018 10:25:22 +0200 Subject: [PATCH] switch to picocli --- build.gradle | 1 + jbake-core/build.gradle | 2 +- .../org/jbake/launcher/LaunchOptions.java | 27 ++++---- .../main/java/org/jbake/launcher/Main.java | 26 +------ .../org/jbake/launcher/LaunchOptionsTest.java | 67 +++++++------------ .../java/org/jbake/launcher/MainTest.java | 12 ++-- 6 files changed, 49 insertions(+), 86 deletions(-) diff --git a/build.gradle b/build.gradle index ee200cb7e..e03b7ad16 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/jbake-core/build.gradle b/jbake-core/build.gradle index f9fd5a7d5..aaf014b81 100644 --- a/jbake-core/build.gradle +++ b/jbake-core/build.gradle @@ -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 { 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..2fdccb4ed 100644 --- a/jbake-core/src/main/java/org/jbake/launcher/LaunchOptions.java +++ b/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 = "") + @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 = "") + @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 is not supplied)") + @CommandLine.Option(names = {"-i", "--init"}, description = "initialises required folder structure with default templates (defaults to current directory if 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 is not supplied) ", depends = ("-i")) + @CommandLine.Option(names = {"-t", "--template"}, description = "use specified template engine for default templates (uses Freemarker if is not supplied) ", arity = "1") private String template; - @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") + @CommandLine.Option(names = {"-s", "--server"}, description = "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") - 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() { @@ -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() { 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..3bb25ee16 100644 --- a/jbake-core/src/main/java/org/jbake/launcher/Main.java +++ b/jbake-core/src/main/java/org/jbake/launcher/Main.java @@ -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. @@ -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 + " \n"); - sw.append(ALT_USAGE_PREFIX + " [OPTION]... [...]\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) { 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..92856594d 100644 --- a/jbake-core/src/test/java/org/jbake/launcher/LaunchOptionsTest.java +++ b/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; @@ -10,75 +10,60 @@ 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"); @@ -86,21 +71,17 @@ public void initWithTemplateAndSourceDirectory() throws Exception { } @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(); @@ -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(); @@ -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); + } } diff --git a/jbake-core/src/test/java/org/jbake/launcher/MainTest.java b/jbake-core/src/test/java/org/jbake/launcher/MainTest.java index 12e320f2a..5303adb35 100644 --- a/jbake-core/src/test/java/org/jbake/launcher/MainTest.java +++ b/jbake-core/src/test/java/org/jbake/launcher/MainTest.java @@ -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; @@ -137,7 +136,7 @@ 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); @@ -145,11 +144,8 @@ public void launchJettyWithCmdlineOverridingProperties() throws Exception { 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 {