[bs-135] Add support for closure-style options declarations

E.g.

    options {
	option "foo", "Foo set"
	option "bar", "Bar has an argument of type int"
          withOptionalArg() ofType Integer
    }

    println "Hello ${options.nonOptionArguments()}: " +
      "${options.has('foo')} ${options.valueOf('bar')}"

[#50427095] [bs-135] Plugin model for spring commands
This commit is contained in:
Dave Syer
2013-05-28 16:59:07 +01:00
parent dcdf2d00b8
commit bf30e2de90
11 changed files with 305 additions and 43 deletions

View File

@@ -15,11 +15,13 @@
*/
package org.springframework.bootstrap.cli.command;
import groovy.lang.GroovyObjectSupport;
import groovy.lang.Script;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
/**
@@ -44,6 +46,30 @@ public class ScriptCommandTests {
((String[]) ((Script) command.getMain()).getProperty("args"))[0]);
}
@Test
public void testLocateFile() throws Exception {
ScriptCommand command = new ScriptCommand(
"src/test/resources/commands/script.groovy");
command.setPaths(new String[] { "." });
command.run("World");
assertEquals("World",
((String[]) ((Script) command.getMain()).getProperty("args"))[0]);
}
@Test
public void testRunnable() throws Exception {
ScriptCommand command = new ScriptCommand("runnable");
command.run("World");
assertTrue(executed);
}
@Test
public void testClosure() throws Exception {
ScriptCommand command = new ScriptCommand("closure");
command.run("World");
assertTrue(executed);
}
@Test
public void testCommand() throws Exception {
ScriptCommand command = new ScriptCommand("command");
@@ -52,12 +78,42 @@ public class ScriptCommandTests {
assertTrue(executed);
}
@Test
public void testDuplicateClassName() throws Exception {
ScriptCommand command1 = new ScriptCommand("handler");
ScriptCommand command2 = new ScriptCommand("command");
assertNotSame(command1.getMain().getClass(), command2.getMain().getClass());
assertEquals(command1.getMain().getClass().getName(), command2.getMain()
.getClass().getName());
}
@Test
public void testOptions() throws Exception {
ScriptCommand command = new ScriptCommand("test");
command.run("World", "--foo");
ScriptCommand command = new ScriptCommand("handler");
String out = ((OptionHandler) command.getMain()).getHelp();
assertTrue("Wrong output: " + out, out.contains("--foo"));
command.run("World", "--foo");
assertTrue(executed);
}
@Test
public void testMixin() throws Exception {
ScriptCommand command = new ScriptCommand("mixin");
GroovyObjectSupport object = (GroovyObjectSupport) command.getMain();
String out = (String) object.getProperty("help");
assertTrue("Wrong output: " + out, out.contains("--foo"));
command.run("World", "--foo");
assertTrue(executed);
}
@Test
public void testMixinWithBlock() throws Exception {
ScriptCommand command = new ScriptCommand("test");
GroovyObjectSupport object = (GroovyObjectSupport) command.getMain();
String out = (String) object.getProperty("help");
System.err.println(out);
assertTrue("Wrong output: " + out, out.contains("--foo"));
command.run("World", "--foo", "--bar=2");
assertTrue(executed);
}

View File

@@ -0,0 +1,6 @@
def run = { msg ->
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${msg}"
}
run

View File

@@ -0,0 +1,18 @@
package org.test.command
class TestCommand implements Command {
String name = "foo"
String description = "My script command"
String help = "No options"
String usageHelp = "Not very useful"
void run(String... args) {
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${args[0]}"
}
}

View File

@@ -0,0 +1,19 @@
package org.test.command
@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r")
import org.eclipse.jgit.api.Git
class TestCommand extends OptionHandler {
void options() {
option "foo", "Foo set"
}
void run(OptionSet options) {
// Demonstrate use of Grape.grab to load dependencies before running
println "Clean : " + Git.open(".." as File).status().call().isClean()
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
}
}

View File

@@ -0,0 +1,6 @@
void options() {
option "foo", "Foo set"
}
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"

View File

@@ -0,0 +1,11 @@
class TestCommand implements Runnable {
def msg
TestCommand(String msg) {
this.msg = msg
}
void run() {
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${msg}"
}
}
new TestCommand(args[0])

View File

@@ -1,19 +1,7 @@
package org.test.command
@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r")
import org.eclipse.jgit.api.Git
class TestCommand extends OptionHandler {
void options() {
option "foo", "Foo set"
}
void run(OptionSet options) {
// Demonstrate use of Grape.grab to load dependencies before running
println "Clean : " + Git.open(".." as File).status().call().isClean()
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
}
options {
option "foo", "Foo set"
option "bar", "Bar has an argument of type int" withOptionalArg() ofType Integer
}
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')} ${options.valueOf('bar')}"