You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2018/05/18 11:13:43 UTC
[1/2] groovy git commit: GROOVY-8520 bugfix: fix issues raised after
previous PR (closes #701)
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_6_X 71915d6c6 -> fea8d87d3
GROOVY-8520 bugfix: fix issues raised after previous PR (closes #701)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/270b5d25
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/270b5d25
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/270b5d25
Branch: refs/heads/GROOVY_2_6_X
Commit: 270b5d25b48c2a8da92c395a8426caf738344781
Parents: 71915d6
Author: Remko Popma <re...@yahoo.com>
Authored: Mon May 7 00:07:35 2018 +0200
Committer: Paul King <pa...@asert.com.au>
Committed: Fri May 18 21:13:05 2018 +1000
----------------------------------------------------------------------
.../groovy/groovy/cli/picocli/CliBuilder.groovy | 6 ++--
.../src/spec/test/builder/CliBuilderTest.groovy | 22 +++++++++++--
.../groovy/cli/picocli/CliBuilderTest.groovy | 33 +++++++++++++++++++-
3 files changed, 55 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/270b5d25/subprojects/groovy-cli-picocli/src/main/groovy/groovy/cli/picocli/CliBuilder.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/cli/picocli/CliBuilder.groovy b/subprojects/groovy-cli-picocli/src/main/groovy/groovy/cli/picocli/CliBuilder.groovy
index aacd832..e0377b3 100644
--- a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/cli/picocli/CliBuilder.groovy
+++ b/subprojects/groovy-cli-picocli/src/main/groovy/groovy/cli/picocli/CliBuilder.groovy
@@ -378,6 +378,7 @@ class CliBuilder {
* <code>true</code> if the parser should recognize long options with both
* a single hyphen and a double hyphen prefix. The default is <code>false</code>,
* so only long options with a double hypen prefix (<code>--option</code>) are recognized.
+ * @since 2.5
*/
boolean acceptLongOptionsWithSingleHyphen = false
@@ -512,9 +513,9 @@ class CliBuilder {
* used by the underlying library.
* @param posix whether to allow clustered short options
*/
- void setPosix(boolean posix) {
+ void setPosix(Boolean posix) {
this.posix = posix
- parser.posixClusteredShortOptionsAllowed(posix)
+ parser.posixClusteredShortOptionsAllowed(posix ?: false)
}
/**
@@ -804,6 +805,7 @@ class CliBuilder {
if (attr.type) { builder.type(attr.type) } // cannot set type to null
if (attr.auxiliaryTypes) { builder.auxiliaryTypes(attr.auxiliaryTypes) } // cannot set aux types to null
builder.arity(arity)
+ builder.description(unparsed.description())
builder.paramLabel("<$attr.label>")
builder.getter(attr.getter)
builder.setter(attr.setter)
http://git-wip-us.apache.org/repos/asf/groovy/blob/270b5d25/subprojects/groovy-cli-picocli/src/spec/test/builder/CliBuilderTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-picocli/src/spec/test/builder/CliBuilderTest.groovy b/subprojects/groovy-cli-picocli/src/spec/test/builder/CliBuilderTest.groovy
index 302bef6..195ac0d 100644
--- a/subprojects/groovy-cli-picocli/src/spec/test/builder/CliBuilderTest.groovy
+++ b/subprojects/groovy-cli-picocli/src/spec/test/builder/CliBuilderTest.groovy
@@ -40,7 +40,7 @@ class CliBuilderTest extends GroovyTestCase {
interface GreeterI {
@Option(shortName='h', description='display usage') Boolean help() // <1>
@Option(shortName='a', description='greeting audience') String audience() // <2>
- @Unparsed List remaining() // <3>
+ @Unparsed(description = "positional parameters") List remaining() // <3>
}
// end::annotationInterfaceSpec[]
@@ -56,14 +56,14 @@ class CliBuilderTest extends GroovyTestCase {
}
String getAudience() { audience }
- @Unparsed
+ @Unparsed(description = "positional parameters")
List remaining // <3>
}
// end::annotationClassSpec[]
void testAnnotationsInterface() {
// tag::annotationInterface[]
- def cli = new CliBuilder(usage: 'groovy Greeter [option]') // <1>
+ def cli = new CliBuilder(name: 'groovy Greeter') // <1>
def argz = '--audience Groovologist'.split()
def options = cli.parseFromSpec(GreeterI, argz) // <2>
assert options.audience() == 'Groovologist' // <3>
@@ -73,6 +73,22 @@ class CliBuilderTest extends GroovyTestCase {
assert options.help()
assert options.remaining() == ['Some', 'Other', 'Args'] // <5>
// end::annotationInterface[]
+
+ options = cli.parseFromSpec(GreeterI, ['-h', 'Some', 'Other', 'Args'] as String[])
+ assert options.help()
+ assert options.remaining() == ['Some', 'Other', 'Args']
+ StringWriter sw = new StringWriter()
+ cli.writer = new PrintWriter(sw)
+ cli.usage()
+
+ String expected = '''\
+Usage: groovy Greeter [-h] [-a=<audience>] [<remaining>...]
+ [<remaining>...] positional parameters
+ -a, --audience=<audience>
+ greeting audience
+ -h, --help display usage
+'''
+ assert expected.normalize() == sw.toString().normalize()
}
void testAnnotationsClass() {
http://git-wip-us.apache.org/repos/asf/groovy/blob/270b5d25/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy b/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy
index e6a0e7a..be8b5f9 100644
--- a/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy
+++ b/subprojects/groovy-cli-picocli/src/test/groovy/groovy/cli/picocli/CliBuilderTest.groovy
@@ -20,7 +20,6 @@ package groovy.cli.picocli
import groovy.cli.Option
import groovy.cli.Unparsed
-import groovy.cli.picocli.CliBuilder
import groovy.transform.ToString
import groovy.transform.TypeChecked
import picocli.CommandLine.DuplicateOptionAnnotationsException
@@ -123,6 +122,38 @@ class CliBuilderTest extends GroovyTestCase {
groovy.util.GroovyTestCase.assertEquals(['1', '2'], options.args)
}
+ void testPosixNullValueHandledCorrectly_inConstructor() {
+ def cli = new CliBuilder()
+ assert cli.posix == true
+ assert cli.parser.posixClusteredShortOptionsAllowed()
+
+ cli = new CliBuilder(posix: false)
+ assert cli.posix == false
+ assert !cli.parser.posixClusteredShortOptionsAllowed()
+
+ cli = new CliBuilder(posix: null)
+ assert cli.posix == null
+ assert !cli.parser.posixClusteredShortOptionsAllowed()
+ }
+
+ void testPosixNullValueHandledCorrectly_inSetter() {
+ def cli = new CliBuilder()
+ assert cli.posix == true
+ assert cli.parser.posixClusteredShortOptionsAllowed()
+
+ cli.posix = false
+ assert cli.posix == false
+ assert !cli.parser.posixClusteredShortOptionsAllowed()
+
+ cli = new CliBuilder()
+ assert cli.posix == true
+ assert cli.parser.posixClusteredShortOptionsAllowed()
+
+ cli.posix = null
+ assert cli.posix == null
+ assert !cli.parser.posixClusteredShortOptionsAllowed()
+ }
+
void testFailedParsePrintsUsage() {
def cli = new CliBuilder(writer: printWriter)
cli.x(required: true, 'message')
[2/2] groovy git commit: GROOVY-8520 replace commons-cli with picocli
in CliBuilder (let groovy.util version delegate to groovy-cli-commons
version)
Posted by pa...@apache.org.
GROOVY-8520 replace commons-cli with picocli in CliBuilder (let groovy.util version delegate to groovy-cli-commons version)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fea8d87d
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fea8d87d
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fea8d87d
Branch: refs/heads/GROOVY_2_6_X
Commit: fea8d87d36382d8d947f8fa4b0e9075978994bef
Parents: 270b5d2
Author: Paul King <pa...@asert.com.au>
Authored: Fri May 18 19:31:43 2018 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Fri May 18 21:13:06 2018 +1000
----------------------------------------------------------------------
.../doc/core-domain-specific-languages.adoc | 46 +--
src/spec/test/builder/CliBuilderTest.groovy | 368 -------------------
.../main/groovy/groovy/util/CliBuilder.groovy | 81 ++++
.../groovy/groovy/util/OptionAccessor.groovy | 38 ++
.../src/spec/test/builder/CliBuilderTest.groovy | 368 +++++++++++++++++++
.../main/groovy/groovy/util/CliBuilder.groovy | 37 --
.../groovy/groovy/util/OptionAccessor.groovy | 38 --
subprojects/groovy-console/build.gradle | 2 +-
subprojects/groovy-groovydoc/build.gradle | 2 +-
.../codehaus/groovy/tools/groovydoc/Main.groovy | 2 +-
10 files changed, 513 insertions(+), 469 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/src/spec/doc/core-domain-specific-languages.adoc
----------------------------------------------------------------------
diff --git a/src/spec/doc/core-domain-specific-languages.adoc b/src/spec/doc/core-domain-specific-languages.adoc
index 3418c6c..6db531f 100644
--- a/src/spec/doc/core-domain-specific-languages.adoc
+++ b/src/spec/doc/core-domain-specific-languages.adoc
@@ -1206,7 +1206,7 @@ Here is how such a specification can be defined:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationInterfaceSpec,indent=0]
----
<1> Specify a Boolean option set using `-h` or `--help`
<2> Specify a String option set using `-a` or `--audience`
@@ -1221,7 +1221,7 @@ Here is how you could use the interface specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationInterface,indent=0]
----
<1> Create a `CliBuilder` instance as before with optional properties
<2> Parse parameters using the interface specification
@@ -1243,7 +1243,7 @@ Here is how such a specification can be defined:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationClassSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationClassSpec,indent=0]
----
<1> Indicate that a Boolean property is an option
<2> Indicate that a String property (with explicit setter) is an option
@@ -1253,7 +1253,7 @@ And here is how you could use the specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationClass,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationClass,indent=0]
----
<1> Create a `CliBuilder` instance as before with optional parameters
<2> Create an instance for `CliBuilder` to populate
@@ -1277,7 +1277,7 @@ with the same arguments as shown for the instance example earlier:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationScript,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=annotationScript,indent=0]
----
===== Options with arguments
@@ -1289,7 +1289,7 @@ Here is an example involving those cases:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgument,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgument,indent=0]
----
<1> An option that is simply a flag - the default; setting args to 0 is allowed but not needed.
<2> An option with exactly one argument
@@ -1306,14 +1306,14 @@ illustrating such a definition:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgumentInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgumentInterfaceSpec,indent=0]
----
And here is how it is used:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgumentInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withArgumentInterface,indent=0]
----
This example makes use of an array-typed option specification. We cover this in more detail shortly when we discuss
@@ -1334,7 +1334,7 @@ Here is an example using types with the dynamic api argument definition style:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withType,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withType,indent=0]
----
Primitives, numeric types, files, enums and arrays thereof, are supported (they are converted using
@@ -1347,7 +1347,7 @@ for you. Here is a sample using the dynamic api style:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvert,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvert,indent=0]
----
Alternatively, you can use the annotation style by supplying the conversion closure as an annotation parameter.
@@ -1355,14 +1355,14 @@ Here is an example specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvertInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvertInterfaceSpec,indent=0]
----
And an example using that specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvertInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withConvertInterface,indent=0]
----
===== Options with multiple arguments
@@ -1388,7 +1388,7 @@ Here is an excerpt highlighting the use of multiple arguments:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgs,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgs,indent=0]
----
<1> Args value supplied as a String and comma value separator specified
<2> One or more arguments are allowed
@@ -1408,14 +1408,14 @@ array type for the annotated class member (method or property) as this example s
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgsInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgsInterfaceSpec,indent=0]
----
And used as follows:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgsInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=multipleArgsInterface,indent=0]
----
===== Types and multiple arguments
@@ -1424,7 +1424,7 @@ Here is an example using types and multiple arguments with the dynamic api argum
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeMultiple,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeMultiple,indent=0]
----
<1> For an array type, the trailing 's' can be used but isn't needed
@@ -1439,7 +1439,7 @@ Here is how you could use it using the dynamic api style:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValue,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValue,indent=0]
----
Similarly, you might want such a specification using the annotation style. Here is an example using an interface
@@ -1447,14 +1447,14 @@ specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValueInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValueInterfaceSpec,indent=0]
----
Which would be used like this:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValueInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withDefaultValueInterface,indent=0]
----
You can also use the `defaultValue` annotation attribute when using annotations with an instance,
@@ -1468,14 +1468,14 @@ annotation style, for example, here is an interface option specification:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeCheckedInterfaceSpec,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeCheckedInterfaceSpec,indent=0]
----
And it can be used in combination with `@TypeChecked` as shown here:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeCheckedInterface,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeCheckedInterface,indent=0]
----
Secondly, there is a feature of the dynamic api style which offers some support. The definition statements
@@ -1502,13 +1502,13 @@ assert a == 21
Finally, there is one additional convenience method offered by `CliBuilder` to even allow the
definition part to be type checked. It is a slightly more verbose method call. Instead of using
-the short name (the _opt_ name) in the method call, you use a fixed named of `option` and
+the short name (the _opt_ name) in the method call, you use a fixed name of `option` and
supply the `opt` value as a property. You must also specify the type directly as shown in
the following example:
[source,groovy]
----
-include::{projectdir}/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeChecked,indent=0]
+include::{projectdir}/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy[tags=withTypeChecked,indent=0]
----
===== Advanced CLI Usage
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/src/spec/test/builder/CliBuilderTest.groovy
----------------------------------------------------------------------
diff --git a/src/spec/test/builder/CliBuilderTest.groovy b/src/spec/test/builder/CliBuilderTest.groovy
deleted file mode 100644
index a84b3a4..0000000
--- a/src/spec/test/builder/CliBuilderTest.groovy
+++ /dev/null
@@ -1,368 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package builder
-
-import groovy.cli.Option
-import groovy.cli.TypedOption
-import groovy.cli.Unparsed
-import groovy.transform.TypeChecked
-
-import java.math.RoundingMode
-
-class CliBuilderTest extends GroovyTestCase {
-// void tearDown() {
-// }
-
- // tag::annotationInterfaceSpec[]
- interface GreeterI {
- @Option(shortName='h', description='display usage') Boolean help() // <1>
- @Option(shortName='a', description='greeting audience') String audience() // <2>
- @Unparsed List remaining() // <3>
- }
- // end::annotationInterfaceSpec[]
-
- // tag::annotationClassSpec[]
- class GreeterC {
- @Option(shortName='h', description='display usage')
- Boolean help // <1>
-
- private String audience
- @Option(shortName='a', description='greeting audience')
- void setAudience(String audience) { // <2>
- this.audience = audience
- }
- String getAudience() { audience }
-
- @Unparsed
- List remaining // <3>
- }
- // end::annotationClassSpec[]
-
- void testAnnotationsInterface() {
- // tag::annotationInterface[]
- def cli = new CliBuilder(usage: 'groovy Greeter [option]') // <1>
- def argz = '--audience Groovologist'.split()
- def options = cli.parseFromSpec(GreeterI, argz) // <2>
- assert options.audience() == 'Groovologist' // <3>
-
- argz = '-h Some Other Args'.split()
- options = cli.parseFromSpec(GreeterI, argz) // <4>
- assert options.help()
- assert options.remaining() == ['Some', 'Other', 'Args'] // <5>
- // end::annotationInterface[]
- }
-
- void testAnnotationsClass() {
- // tag::annotationClass[]
- def cli = new CliBuilder(usage: 'groovy Greeter [option]') // <1>
- def options = new GreeterC() // <2>
- def argz = '--audience Groovologist foo'.split()
- cli.parseFromInstance(options, argz) // <3>
- assert options.audience == 'Groovologist' // <4>
- assert options.remaining == ['foo'] // <5>
- // end::annotationClass[]
- }
-
- void testParseScript() {
- def argz = '--audience Groovologist foo'.split()
- new GroovyShell().run('''
- // tag::annotationScript[]
- import groovy.cli.OptionField
- import groovy.cli.UnparsedField
-
- @OptionField String audience
- @OptionField Boolean help
- @UnparsedField List remaining
- new CliBuilder().parseFromInstance(this, args)
- assert audience == 'Groovologist'
- assert remaining == ['foo']
- // end::annotationScript[]
- ''', 'TestScript.groovy', argz)
- }
-
- void testWithArgument() {
- // tag::withArgument[]
- def cli = new CliBuilder()
- cli.a(args: 0, 'a arg') // <1>
- cli.b(args: 1, 'b arg') // <2>
- cli.c(args: 1, optionalArg: true, 'c arg') // <3>
- def options = cli.parse('-a -b foo -c bar baz'.split()) // <4>
-
- assert options.a == true
- assert options.b == 'foo'
- assert options.c == 'bar'
- assert options.arguments() == ['baz']
-
- options = cli.parse('-a -c -b foo bar baz'.split()) // <5>
-
- assert options.a == true
- assert options.c == true
- assert options.b == 'foo'
- assert options.arguments() == ['bar', 'baz']
- // end::withArgument[]
- }
-
- // tag::withArgumentInterfaceSpec[]
- interface WithArgsI {
- @Option boolean a()
- @Option String b()
- @Option(optionalArg=true) String[] c()
- @Unparsed List remaining()
- }
- // end::withArgumentInterfaceSpec[]
-
- void testWithArgumentInterface() {
- // tag::withArgumentInterface[]
- def cli = new CliBuilder()
- def options = cli.parseFromSpec(WithArgsI, '-a -b foo -c bar baz'.split())
- assert options.a()
- assert options.b() == 'foo'
- assert options.c() == ['bar']
- assert options.remaining() == ['baz']
-
- options = cli.parseFromSpec(WithArgsI, '-a -c -b foo bar baz'.split())
- assert options.a()
- assert options.c() == []
- assert options.b() == 'foo'
- assert options.remaining() == ['bar', 'baz']
- // end::withArgumentInterface[]
- }
-
- void testMultipleArgsAndOptionalValueSeparator() {
- // tag::multipleArgs[]
- def cli = new CliBuilder()
- cli.a(args: 2, 'a-arg')
- cli.b(args: '2', valueSeparator: ',', 'b-arg') // <1>
- cli.c(args: '+', valueSeparator: ',', 'c-arg') // <2>
-
- def options = cli.parse('-a 1 2 3 4'.split()) // <3>
- assert options.a == '1' // <4>
- assert options.as == ['1', '2'] // <5>
- assert options.arguments() == ['3', '4']
-
- options = cli.parse('-a1 -a2 3'.split()) // <6>
- assert options.as == ['1', '2']
- assert options.arguments() == ['3']
-
- options = cli.parse(['-b1,2']) // <7>
- assert options.bs == ['1', '2']
-
- options = cli.parse(['-c', '1'])
- assert options.cs == ['1']
-
- options = cli.parse(['-c1'])
- assert options.cs == ['1']
-
- options = cli.parse(['-c1,2,3'])
- assert options.cs == ['1', '2', '3']
- // end::multipleArgs[]
- }
-
- // tag::multipleArgsInterfaceSpec[]
- interface ValSepI {
- @Option(numberOfArguments=2) String[] a()
- @Option(numberOfArgumentsString='2', valueSeparator=',') String[] b()
- @Option(numberOfArgumentsString='+', valueSeparator=',') String[] c()
- @Unparsed remaining()
- }
- // end::multipleArgsInterfaceSpec[]
-
- void testMultipleArgsAndOptionalValueSeparatorInterface() {
- // tag::multipleArgsInterface[]
- def cli = new CliBuilder()
-
- def options = cli.parseFromSpec(ValSepI, '-a 1 2 3 4'.split())
- assert options.a() == ['1', '2']
- assert options.remaining() == ['3', '4']
-
- options = cli.parseFromSpec(ValSepI, '-a1 -a2 3'.split())
- assert options.a() == ['1', '2']
- assert options.remaining() == ['3']
-
- options = cli.parseFromSpec(ValSepI, ['-b1,2'] as String[])
- assert options.b() == ['1', '2']
-
- options = cli.parseFromSpec(ValSepI, ['-c', '1'] as String[])
- assert options.c() == ['1']
-
- options = cli.parseFromSpec(ValSepI, ['-c1'] as String[])
- assert options.c() == ['1']
-
- options = cli.parseFromSpec(ValSepI, ['-c1,2,3'] as String[])
- assert options.c() == ['1', '2', '3']
- // end::multipleArgsInterface[]
- }
-
- void testType() {
- // tag::withType[]
- def argz = '''-a John -b -d 21 -e 1980 -f 3.5 -g 3.14159
- -h cv.txt -i DOWN and some more'''.split()
- def cli = new CliBuilder()
- cli.a(type: String, 'a-arg')
- cli.b(type: boolean, 'b-arg')
- cli.c(type: Boolean, 'c-arg')
- cli.d(type: int, 'd-arg')
- cli.e(type: Long, 'e-arg')
- cli.f(type: Float, 'f-arg')
- cli.g(type: BigDecimal, 'g-arg')
- cli.h(type: File, 'h-arg')
- cli.i(type: RoundingMode, 'i-arg')
- def options = cli.parse(argz)
- assert options.a == 'John'
- assert options.b
- assert !options.c
- assert options.d == 21
- assert options.e == 1980L
- assert options.f == 3.5f
- assert options.g == 3.14159
- assert options.h == new File('cv.txt')
- assert options.i == RoundingMode.DOWN
- assert options.arguments() == ['and', 'some', 'more']
- // end::withType[]
- }
-
- void testTypeMultiple() {
- // tag::withTypeMultiple[]
- def argz = '''-j 3 4 5 -k1.5,2.5,3.5 and some more'''.split()
- def cli = new CliBuilder()
- cli.j(args: 3, type: int[], 'j-arg')
- cli.k(args: '+', valueSeparator: ',', type: BigDecimal[], 'k-arg')
- def options = cli.parse(argz)
- assert options.js == [3, 4, 5] // <1>
- assert options.j == [3, 4, 5] // <1>
- assert options.k == [1.5, 2.5, 3.5]
- assert options.arguments() == ['and', 'some', 'more']
- // end::withTypeMultiple[]
- }
-
- void testConvert() {
- // tag::withConvert[]
- def argz = '''-a John -b Mary -d 2016-01-01 and some more'''.split()
- def cli = new CliBuilder()
- def lower = { it.toLowerCase() }
- cli.a(convert: lower, 'a-arg')
- cli.b(convert: { it.toUpperCase() }, 'b-arg')
- cli.d(convert: { Date.parse('yyyy-MM-dd', it) }, 'd-arg')
- def options = cli.parse(argz)
- assert options.a == 'john'
- assert options.b == 'MARY'
- assert options.d.format('yyyy-MM-dd') == '2016-01-01'
- assert options.arguments() == ['and', 'some', 'more']
- // end::withConvert[]
- }
-
- // tag::withConvertInterfaceSpec[]
- interface WithConvertI {
- @Option(convert={ it.toLowerCase() }) String a()
- @Option(convert={ it.toUpperCase() }) String b()
- @Option(convert={ Date.parse("yyyy-MM-dd", it) }) Date d()
- @Unparsed List remaining()
- }
- // end::withConvertInterfaceSpec[]
-
- void testConvertInterface() {
- // tag::withConvertInterface[]
- Date newYears = Date.parse("yyyy-MM-dd", "2016-01-01")
- def argz = '''-a John -b Mary -d 2016-01-01 and some more'''.split()
- def cli = new CliBuilder()
- def options = cli.parseFromSpec(WithConvertI, argz)
- assert options.a() == 'john'
- assert options.b() == 'MARY'
- assert options.d() == newYears
- assert options.remaining() == ['and', 'some', 'more']
- // end::withConvertInterface[]
- }
-
- void testDefaultValue() {
- // tag::withDefaultValue[]
- def cli = new CliBuilder()
- cli.f longOpt: 'from', type: String, args: 1, defaultValue: 'one', 'f option'
- cli.t longOpt: 'to', type: int, defaultValue: '35', 't option'
-
- def options = cli.parse('-f two'.split())
- assert options.hasOption('f')
- assert options.f == 'two'
- assert !options.hasOption('t')
- assert options.t == 35
-
- options = cli.parse('-t 45'.split())
- assert !options.hasOption('from')
- assert options.from == 'one'
- assert options.hasOption('to')
- assert options.to == 45
- // end::withDefaultValue[]
- }
-
- // tag::withDefaultValueInterfaceSpec[]
- interface WithDefaultValueI {
- @Option(shortName='f', defaultValue='one') String from()
- @Option(shortName='t', defaultValue='35') int to()
- }
- // end::withDefaultValueInterfaceSpec[]
-
- void testDefaultValueInterface() {
- // tag::withDefaultValueInterface[]
- def cli = new CliBuilder()
-
- def options = cli.parseFromSpec(WithDefaultValueI, '-f two'.split())
- assert options.from() == 'two'
- assert options.to() == 35
-
- options = cli.parseFromSpec(WithDefaultValueI, '-t 45'.split())
- assert options.from() == 'one'
- assert options.to() == 45
- // end::withDefaultValueInterface[]
- }
-
- // tag::withTypeCheckedInterfaceSpec[]
- interface TypeCheckedI{
- @Option String name()
- @Option int age()
- @Unparsed List remaining()
- }
- // end::withTypeCheckedInterfaceSpec[]
-
- // tag::withTypeCheckedInterface[]
- @TypeChecked
- void testTypeCheckedInterface() {
- def argz = "--name John --age 21 and some more".split()
- def cli = new CliBuilder()
- def options = cli.parseFromSpec(TypeCheckedI, argz)
- String n = options.name()
- int a = options.age()
- assert n == 'John' && a == 21
- assert options.remaining() == ['and', 'some', 'more']
- }
- // end::withTypeCheckedInterface[]
-
- // tag::withTypeChecked[]
- @TypeChecked
- void testTypeChecked() {
- def cli = new CliBuilder(acceptLongOptionsWithSingleHyphen: true)
- TypedOption<String> name = cli.option(String, opt: 'n', longOpt: 'name', 'name option')
- TypedOption<Integer> age = cli.option(Integer, longOpt: 'age', 'age option')
- def argz = "--name John -age 21 and some more".split()
- def options = cli.parse(argz)
- String n = options[name]
- int a = options[age]
- assert n == 'John' && a == 21
- assert options.arguments() == ['and', 'some', 'more']
- }
- // end::withTypeChecked[]
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/CliBuilder.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/CliBuilder.groovy b/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/CliBuilder.groovy
new file mode 100644
index 0000000..a7097db
--- /dev/null
+++ b/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/CliBuilder.groovy
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.util
+
+import org.apache.commons.cli.CommandLineParser
+import org.apache.commons.cli.HelpFormatter
+
+/**
+ * @deprecated use {@code groovy.cli.picocli.CliBuilder} or {@link groovy.cli.commons.CliBuilder} instead.
+ */
+@Deprecated
+class CliBuilder {
+ private @Delegate
+ groovy.cli.commons.CliBuilder delegate = new groovy.cli.commons.CliBuilder()
+
+ // explicit delegate to convert return type to expected legacy package
+ OptionAccessor parse(args) {
+ return new OptionAccessor(delegate: delegate.parse(args))
+ }
+
+ // explicit delegate since groovyObject methods ignored by @Delegate
+ def invokeMethod(String name, Object args) {
+ delegate.invokeMethod(name, args)
+ }
+
+ // delegated versions of the methods below are available but we want
+ // IDE warnings to encourage people not to use these methods in particular
+ // over and above the warning they should have at the class level
+
+ /**
+ * @deprecated This may not be available in future groovy.util.CliBuilder versions.
+ * Use groovy.cli.commons.CliBuilder if you need this feature.
+ */
+ @Deprecated
+ void setParser(CommandLineParser parser) {
+ delegate.setParser(parser)
+ }
+
+ /**
+ * @deprecated This may not be available in future groovy.util.CliBuilder versions.
+ * Use groovy.cli.commons.CliBuilder if you need this feature.
+ */
+ @Deprecated
+ CommandLineParser setParser() {
+ delegate.getParser()
+ }
+
+ /**
+ * @deprecated This may not be available in future groovy.util.CliBuilder versions.
+ * Use groovy.cli.commons.CliBuilder if you need this feature.
+ */
+ @Deprecated
+ void setFormatter(HelpFormatter formatter) {
+ delegate.setFormatter(formatter)
+ }
+
+ /**
+ * @deprecated This may not be available in future groovy.util.CliBuilder versions.
+ * Use groovy.cli.commons.CliBuilder if you need this feature.
+ */
+ @Deprecated
+ HelpFormatter getFormatter() {
+ delegate.getFormatter()
+ }
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy b/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
new file mode 100644
index 0000000..935cf10
--- /dev/null
+++ b/subprojects/groovy-cli-commons/src/main/groovy/groovy/util/OptionAccessor.groovy
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.util
+
+/**
+ * @deprecated use {@code groovy.cli.picocli.OptionAccessor} or {@link groovy.cli.commons.OptionAccessor}
+ */
+@Deprecated
+class OptionAccessor {
+ @Delegate
+ groovy.cli.commons.OptionAccessor delegate
+
+ // explicit delegate since groovyObject methods ignored by @Delegate
+ def getProperty(String name) {
+ delegate.getProperty(name)
+ }
+
+ // explicit delegate since groovyObject methods ignored by @Delegate
+ def invokeMethod(String name, Object args) {
+ delegate.invokeMethod(name, args)
+ }
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy b/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy
new file mode 100644
index 0000000..313a793
--- /dev/null
+++ b/subprojects/groovy-cli-commons/src/spec/test/builder/CliBuilderTest.groovy
@@ -0,0 +1,368 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package builder
+
+import groovy.cli.Option
+import groovy.cli.TypedOption
+import groovy.cli.Unparsed
+import groovy.transform.TypeChecked
+
+import java.math.RoundingMode
+
+class CliBuilderTest extends GroovyTestCase {
+// void tearDown() {
+// }
+
+ // tag::annotationInterfaceSpec[]
+ interface GreeterI {
+ @Option(shortName='h', description='display usage') Boolean help() // <1>
+ @Option(shortName='a', description='greeting audience') String audience() // <2>
+ @Unparsed List remaining() // <3>
+ }
+ // end::annotationInterfaceSpec[]
+
+ // tag::annotationClassSpec[]
+ class GreeterC {
+ @Option(shortName='h', description='display usage')
+ Boolean help // <1>
+
+ private String audience
+ @Option(shortName='a', description='greeting audience')
+ void setAudience(String audience) { // <2>
+ this.audience = audience
+ }
+ String getAudience() { audience }
+
+ @Unparsed
+ List remaining // <3>
+ }
+ // end::annotationClassSpec[]
+
+ void testAnnotationsInterface() {
+ // tag::annotationInterface[]
+ def cli = new CliBuilder(usage: 'groovy Greeter [option]') // <1>
+ def argz = '--audience Groovologist'.split()
+ def options = cli.parseFromSpec(GreeterI, argz) // <2>
+ assert options.audience() == 'Groovologist' // <3>
+
+ argz = '-h Some Other Args'.split()
+ options = cli.parseFromSpec(GreeterI, argz) // <4>
+ assert options.help()
+ assert options.remaining() == ['Some', 'Other', 'Args'] // <5>
+ // end::annotationInterface[]
+ }
+
+ void testAnnotationsClass() {
+ // tag::annotationClass[]
+ def cli = new CliBuilder(usage: 'groovy Greeter [option]') // <1>
+ def options = new GreeterC() // <2>
+ def argz = '--audience Groovologist foo'.split()
+ cli.parseFromInstance(options, argz) // <3>
+ assert options.audience == 'Groovologist' // <4>
+ assert options.remaining == ['foo'] // <5>
+ // end::annotationClass[]
+ }
+
+ void testParseScript() {
+ def argz = '--audience Groovologist foo'.split()
+ new GroovyShell().run('''
+ // tag::annotationScript[]
+ import groovy.cli.OptionField
+ import groovy.cli.UnparsedField
+
+ @OptionField String audience
+ @OptionField Boolean help
+ @UnparsedField List remaining
+ new CliBuilder().parseFromInstance(this, args)
+ assert audience == 'Groovologist'
+ assert remaining == ['foo']
+ // end::annotationScript[]
+ ''', 'TestScript.groovy', argz)
+ }
+
+ void testWithArgument() {
+ // tag::withArgument[]
+ def cli = new CliBuilder()
+ cli.a(args: 0, 'a arg') // <1>
+ cli.b(args: 1, 'b arg') // <2>
+ cli.c(args: 1, optionalArg: true, 'c arg') // <3>
+ def options = cli.parse('-a -b foo -c bar baz'.split()) // <4>
+
+ assert options.a == true
+ assert options.b == 'foo'
+ assert options.c == 'bar'
+ assert options.arguments() == ['baz']
+
+ options = cli.parse('-a -c -b foo bar baz'.split()) // <5>
+
+ assert options.a == true
+ assert options.c == true
+ assert options.b == 'foo'
+ assert options.arguments() == ['bar', 'baz']
+ // end::withArgument[]
+ }
+
+ // tag::withArgumentInterfaceSpec[]
+ interface WithArgsI {
+ @Option boolean a()
+ @Option String b()
+ @Option(optionalArg=true) String[] c()
+ @Unparsed List remaining()
+ }
+ // end::withArgumentInterfaceSpec[]
+
+ void testWithArgumentInterface() {
+ // tag::withArgumentInterface[]
+ def cli = new CliBuilder()
+ def options = cli.parseFromSpec(WithArgsI, '-a -b foo -c bar baz'.split())
+ assert options.a()
+ assert options.b() == 'foo'
+ assert options.c() == ['bar']
+ assert options.remaining() == ['baz']
+
+ options = cli.parseFromSpec(WithArgsI, '-a -c -b foo bar baz'.split())
+ assert options.a()
+ assert options.c() == []
+ assert options.b() == 'foo'
+ assert options.remaining() == ['bar', 'baz']
+ // end::withArgumentInterface[]
+ }
+
+ void testMultipleArgsAndOptionalValueSeparator() {
+ // tag::multipleArgs[]
+ def cli = new CliBuilder()
+ cli.a(args: 2, 'a-arg')
+ cli.b(args: '2', valueSeparator: ',', 'b-arg') // <1>
+ cli.c(args: '+', valueSeparator: ',', 'c-arg') // <2>
+
+ def options = cli.parse('-a 1 2 3 4'.split()) // <3>
+ assert options.a == '1' // <4>
+ assert options.as == ['1', '2'] // <5>
+ assert options.arguments() == ['3', '4']
+
+ options = cli.parse('-a1 -a2 3'.split()) // <6>
+ assert options.as == ['1', '2']
+ assert options.arguments() == ['3']
+
+ options = cli.parse(['-b1,2']) // <7>
+ assert options.bs == ['1', '2']
+
+ options = cli.parse(['-c', '1'])
+ assert options.cs == ['1']
+
+ options = cli.parse(['-c1'])
+ assert options.cs == ['1']
+
+ options = cli.parse(['-c1,2,3'])
+ assert options.cs == ['1', '2', '3']
+ // end::multipleArgs[]
+ }
+
+ // tag::multipleArgsInterfaceSpec[]
+ interface ValSepI {
+ @Option(numberOfArguments=2) String[] a()
+ @Option(numberOfArgumentsString='2', valueSeparator=',') String[] b()
+ @Option(numberOfArgumentsString='+', valueSeparator=',') String[] c()
+ @Unparsed remaining()
+ }
+ // end::multipleArgsInterfaceSpec[]
+
+ void testMultipleArgsAndOptionalValueSeparatorInterface() {
+ // tag::multipleArgsInterface[]
+ def cli = new CliBuilder()
+
+ def options = cli.parseFromSpec(ValSepI, '-a 1 2 3 4'.split())
+ assert options.a() == ['1', '2']
+ assert options.remaining() == ['3', '4']
+
+ options = cli.parseFromSpec(ValSepI, '-a1 -a2 3'.split())
+ assert options.a() == ['1', '2']
+ assert options.remaining() == ['3']
+
+ options = cli.parseFromSpec(ValSepI, ['-b1,2'] as String[])
+ assert options.b() == ['1', '2']
+
+ options = cli.parseFromSpec(ValSepI, ['-c', '1'] as String[])
+ assert options.c() == ['1']
+
+ options = cli.parseFromSpec(ValSepI, ['-c1'] as String[])
+ assert options.c() == ['1']
+
+ options = cli.parseFromSpec(ValSepI, ['-c1,2,3'] as String[])
+ assert options.c() == ['1', '2', '3']
+ // end::multipleArgsInterface[]
+ }
+
+ void testType() {
+ // tag::withType[]
+ def argz = '''-a John -b -d 21 -e 1980 -f 3.5 -g 3.14159
+ -h cv.txt -i DOWN and some more'''.split()
+ def cli = new CliBuilder()
+ cli.a(type: String, 'a-arg')
+ cli.b(type: boolean, 'b-arg')
+ cli.c(type: Boolean, 'c-arg')
+ cli.d(type: int, 'd-arg')
+ cli.e(type: Long, 'e-arg')
+ cli.f(type: Float, 'f-arg')
+ cli.g(type: BigDecimal, 'g-arg')
+ cli.h(type: File, 'h-arg')
+ cli.i(type: RoundingMode, 'i-arg')
+ def options = cli.parse(argz)
+ assert options.a == 'John'
+ assert options.b
+ assert !options.c
+ assert options.d == 21
+ assert options.e == 1980L
+ assert options.f == 3.5f
+ assert options.g == 3.14159
+ assert options.h == new File('cv.txt')
+ assert options.i == RoundingMode.DOWN
+ assert options.arguments() == ['and', 'some', 'more']
+ // end::withType[]
+ }
+
+ void testTypeMultiple() {
+ // tag::withTypeMultiple[]
+ def argz = '''-j 3 4 5 -k1.5,2.5,3.5 and some more'''.split()
+ def cli = new CliBuilder()
+ cli.j(args: 3, type: int[], 'j-arg')
+ cli.k(args: '+', valueSeparator: ',', type: BigDecimal[], 'k-arg')
+ def options = cli.parse(argz)
+ assert options.js == [3, 4, 5] // <1>
+ assert options.j == [3, 4, 5] // <1>
+ assert options.k == [1.5, 2.5, 3.5]
+ assert options.arguments() == ['and', 'some', 'more']
+ // end::withTypeMultiple[]
+ }
+
+ void testConvert() {
+ // tag::withConvert[]
+ def argz = '''-a John -b Mary -d 2016-01-01 and some more'''.split()
+ def cli = new CliBuilder()
+ def lower = { it.toLowerCase() }
+ cli.a(convert: lower, 'a-arg')
+ cli.b(convert: { it.toUpperCase() }, 'b-arg')
+ cli.d(convert: { Date.parse('yyyy-MM-dd', it) }, 'd-arg')
+ def options = cli.parse(argz)
+ assert options.a == 'john'
+ assert options.b == 'MARY'
+ assert options.d.format('yyyy-MM-dd') == '2016-01-01'
+ assert options.arguments() == ['and', 'some', 'more']
+ // end::withConvert[]
+ }
+
+ // tag::withConvertInterfaceSpec[]
+ interface WithConvertI {
+ @Option(convert={ it.toLowerCase() }) String a()
+ @Option(convert={ it.toUpperCase() }) String b()
+ @Option(convert={ Date.parse("yyyy-MM-dd", it) }) Date d()
+ @Unparsed List remaining()
+ }
+ // end::withConvertInterfaceSpec[]
+
+ void testConvertInterface() {
+ // tag::withConvertInterface[]
+ Date newYears = Date.parse("yyyy-MM-dd", "2016-01-01")
+ def argz = '''-a John -b Mary -d 2016-01-01 and some more'''.split()
+ def cli = new CliBuilder()
+ def options = cli.parseFromSpec(WithConvertI, argz)
+ assert options.a() == 'john'
+ assert options.b() == 'MARY'
+ assert options.d() == newYears
+ assert options.remaining() == ['and', 'some', 'more']
+ // end::withConvertInterface[]
+ }
+
+ void testDefaultValue() {
+ // tag::withDefaultValue[]
+ def cli = new CliBuilder()
+ cli.f longOpt: 'from', type: String, args: 1, defaultValue: 'one', 'f option'
+ cli.t longOpt: 'to', type: int, defaultValue: '35', 't option'
+
+ def options = cli.parse('-f two'.split())
+ assert options.hasOption('f')
+ assert options.f == 'two'
+ assert !options.hasOption('t')
+ assert options.t == 35
+
+ options = cli.parse('-t 45'.split())
+ assert !options.hasOption('from')
+ assert options.from == 'one'
+ assert options.hasOption('to')
+ assert options.to == 45
+ // end::withDefaultValue[]
+ }
+
+ // tag::withDefaultValueInterfaceSpec[]
+ interface WithDefaultValueI {
+ @Option(shortName='f', defaultValue='one') String from()
+ @Option(shortName='t', defaultValue='35') int to()
+ }
+ // end::withDefaultValueInterfaceSpec[]
+
+ void testDefaultValueInterface() {
+ // tag::withDefaultValueInterface[]
+ def cli = new CliBuilder()
+
+ def options = cli.parseFromSpec(WithDefaultValueI, '-f two'.split())
+ assert options.from() == 'two'
+ assert options.to() == 35
+
+ options = cli.parseFromSpec(WithDefaultValueI, '-t 45'.split())
+ assert options.from() == 'one'
+ assert options.to() == 45
+ // end::withDefaultValueInterface[]
+ }
+
+ // tag::withTypeCheckedInterfaceSpec[]
+ interface TypeCheckedI{
+ @Option String name()
+ @Option int age()
+ @Unparsed List remaining()
+ }
+ // end::withTypeCheckedInterfaceSpec[]
+
+ // tag::withTypeCheckedInterface[]
+ @TypeChecked
+ void testTypeCheckedInterface() {
+ def argz = "--name John --age 21 and some more".split()
+ def cli = new CliBuilder()
+ def options = cli.parseFromSpec(TypeCheckedI, argz)
+ String n = options.name()
+ int a = options.age()
+ assert n == 'John' && a == 21
+ assert options.remaining() == ['and', 'some', 'more']
+ }
+ // end::withTypeCheckedInterface[]
+
+ // tag::withTypeChecked[]
+ @TypeChecked
+ void testTypeChecked() {
+ def cli = new CliBuilder()
+ TypedOption<String> name = cli.option(String, opt: 'n', longOpt: 'name', 'name option')
+ TypedOption<Integer> age = cli.option(Integer, longOpt: 'age', 'age option')
+ def argz = "--name John --age 21 and some more".split()
+ def options = cli.parse(argz)
+ String n = options[name]
+ int a = options[age]
+ assert n == 'John' && a == 21
+ assert options.arguments() == ['and', 'some', 'more']
+ }
+ // end::withTypeChecked[]
+}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/CliBuilder.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/CliBuilder.groovy b/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/CliBuilder.groovy
deleted file mode 100644
index bce9ebb..0000000
--- a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/CliBuilder.groovy
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package groovy.util
-
-/**
- * @deprecated use {@link groovy.cli.picocli.CliBuilder}
- */
-@Deprecated
-class CliBuilder {
- private @Delegate groovy.cli.picocli.CliBuilder delegate = new groovy.cli.picocli.CliBuilder()
-
- // explicit delegate to convert return type to expected legacy package
- OptionAccessor parse(args) {
- return new OptionAccessor(delegate: delegate.parse(args))
- }
-
- // explicit delegate since groovyObject methods ignored by @Delegate
- def invokeMethod(String name, Object args) {
- delegate.invokeMethod(name, args)
- }
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/OptionAccessor.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/OptionAccessor.groovy b/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/OptionAccessor.groovy
deleted file mode 100644
index fe77e74..0000000
--- a/subprojects/groovy-cli-picocli/src/main/groovy/groovy/util/OptionAccessor.groovy
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package groovy.util
-
-/**
- * @deprecated use {@link groovy.cli.picocli.CliBuilder}
- */
-@Deprecated
-class OptionAccessor {
- @Delegate
- groovy.cli.picocli.OptionAccessor delegate
-
- // explicit delegate since groovyObject methods ignored by @Delegate
- def getProperty(String name) {
- delegate.getProperty(name)
- }
-
- // explicit delegate since groovyObject methods ignored by @Delegate
- def invokeMethod(String name, Object args) {
- delegate.invokeMethod(name, args)
- }
-}
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-console/build.gradle
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/build.gradle b/subprojects/groovy-console/build.gradle
index fd8f929..6362468 100644
--- a/subprojects/groovy-console/build.gradle
+++ b/subprojects/groovy-console/build.gradle
@@ -20,7 +20,7 @@ evaluationDependsOn(':groovy-swing')
dependencies {
compile rootProject
- compile project(':groovy-cli-picocli')
+ compile project(':groovy-cli-commons')
compile project(':groovy-swing')
compile project(':groovy-templates')
testCompile project(':groovy-test')
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-groovydoc/build.gradle
----------------------------------------------------------------------
diff --git a/subprojects/groovy-groovydoc/build.gradle b/subprojects/groovy-groovydoc/build.gradle
index 886b3f6..3e86d0b 100644
--- a/subprojects/groovy-groovydoc/build.gradle
+++ b/subprojects/groovy-groovydoc/build.gradle
@@ -19,7 +19,7 @@
dependencies {
compile rootProject
testCompile rootProject.sourceSets.test.runtimeClasspath
- compile project(':groovy-cli-picocli')
+ compile project(':groovy-cli-commons')
compile project(':groovy-templates')
runtime project(':groovy-dateutil')
testCompile project(':groovy-test')
http://git-wip-us.apache.org/repos/asf/groovy/blob/fea8d87d/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy b/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
index 0579cda..b304178 100644
--- a/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
+++ b/subprojects/groovy-groovydoc/src/main/groovy/org/codehaus/groovy/tools/groovydoc/Main.groovy
@@ -18,7 +18,7 @@
*/
package org.codehaus.groovy.tools.groovydoc
-import groovy.cli.picocli.CliBuilder
+import groovy.cli.commons.CliBuilder
import groovy.io.FileType
import org.codehaus.groovy.tools.groovydoc.gstringTemplates.GroovyDocTemplateInfo
import org.codehaus.groovy.tools.shell.IO