From fe6dbce11913a6d067f434c8dc0d021f6f675f87 Mon Sep 17 00:00:00 2001 From: Paul Horn Date: Sun, 21 Oct 2018 21:30:59 +0200 Subject: [PATCH] Fill array values before setting them as value --- src/main/java/picocli/CommandLine.java | 2 +- src/test/java/picocli/CommandLineTest.java | 32 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 100a65e53..71464346a 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -7227,10 +7227,10 @@ private int applyValuesToArrayField(ArgSpec argSpec, } } Object array = Array.newInstance(type, newValues.size()); - argSpec.setValue(array, commandSpec.commandLine()); for (int i = 0; i < newValues.size(); i++) { Array.set(array, i, newValues.get(i)); } + argSpec.setValue(array, commandSpec.commandLine()); parseResult.add(argSpec, position); return converted.size(); // return how many args were consumed } diff --git a/src/test/java/picocli/CommandLineTest.java b/src/test/java/picocli/CommandLineTest.java index e1959b6be..7ba167467 100644 --- a/src/test/java/picocli/CommandLineTest.java +++ b/src/test/java/picocli/CommandLineTest.java @@ -195,6 +195,38 @@ class ArrayPositionalParams { assertNotSame(array, params.array); assertArrayEquals(new int[]{3, 2, 1}, params.array); } + @Test + public void testArrayPositionalParametersAreFilledWithValues() { + @Command + class ArrayPositionalParams { + String string; + @Parameters() + void setString( String[] array) { + StringBuilder sb = new StringBuilder(); + for (String s : array) { sb.append(s); } + string = sb.toString(); + } + } + ArrayPositionalParams params = new ArrayPositionalParams(); + new CommandLine(params).parse("foo", "bar", "baz"); + assertEquals("foobarbaz", params.string); + } + @Test + public void testArrayOptionsAreFilledWithValues() { + @Command + class ArrayPositionalParams { + String string; + @Option(names="-s") + void setString( String[] array) { + StringBuilder sb = new StringBuilder(); + for (String s : array) { sb.append(s); } + string = sb.toString(); + } + } + ArrayPositionalParams params = new ArrayPositionalParams(); + new CommandLine(params).parse("-s", "foo", "-s", "bar", "-s", "baz"); + assertEquals("foobarbaz", params.string); + } private class ListPositionalParams { @Parameters(type = Integer.class) List list; }