From 19ddf64071f85d05bb477b2d8506a587311655e5 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Jan 2021 19:09:20 -0500 Subject: [PATCH] Fixed an issue with clibuilder not honoring the type flag GROOVY-9886 --- .../groovy/groovy/cli/commons/CliBuilder.groovy | 3 ++- .../groovy/cli/commons/CliBuilderTest.groovy | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/subprojects/groovy-cli-commons/src/main/groovy/groovy/cli/commons/CliBuilder.groovy b/subprojects/groovy-cli-commons/src/main/groovy/groovy/cli/commons/CliBuilder.groovy index 15d663290b7..ec3e399e985 100644 --- a/subprojects/groovy-cli-commons/src/main/groovy/groovy/cli/commons/CliBuilder.groovy +++ b/subprojects/groovy-cli-commons/src/main/groovy/groovy/cli/commons/CliBuilder.groovy @@ -329,13 +329,14 @@ class CliBuilder { } if (args.size() == 2 && args[0] instanceof Map) { def convert = args[0].remove('convert') - def type = args[0].remove('type') + def type = args[0]['type'] def defaultValue = args[0].remove('defaultValue') if (type && !(type instanceof Class)) { throw new CliBuilderException("'type' must be a Class") } if ((convert || type) && !args[0].containsKey('args') && type?.simpleName?.toLowerCase() != 'boolean') { + args[0].remove('type') args[0].args = 1 } def option = option(name, args[0], args[1]) diff --git a/subprojects/groovy-cli-commons/src/test/groovy/groovy/cli/commons/CliBuilderTest.groovy b/subprojects/groovy-cli-commons/src/test/groovy/groovy/cli/commons/CliBuilderTest.groovy index d23b64a1775..a582eaf1b9c 100644 --- a/subprojects/groovy-cli-commons/src/test/groovy/groovy/cli/commons/CliBuilderTest.groovy +++ b/subprojects/groovy-cli-commons/src/test/groovy/groovy/cli/commons/CliBuilderTest.groovy @@ -674,6 +674,20 @@ usage: groovy assert hello.nums() == [12, 34] } + // GROOVY-9886 + void testHonorsTypeParameter() { + def cli = new CliBuilder(writer : new PrintWriter(System.out)) + def opt = 'a' + def key = 'abc' + def type = double.class + def desc = 'abc' + cli."$opt"(args: 1, longOpt: key, type: type, desc) + def options = cli.parse(['--abc', '0.00']) + options.getOptions().each { + assert(it.getType().toString() == 'double') + } + } + void testParseFromInstanceFlagEdgeCases() { def cli = new CliBuilder() def options = cli.parseFromSpec(FlagEdgeCasesI, '-abc -efg true --ijk foo --lmn bar baz'.split())