From 87ee24a606f4c115462106d4faa5ae6b2bb4952e Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Sat, 19 Oct 2024 20:48:18 +0900 Subject: [PATCH] [#2342] Add example --- src/test/java/picocli/Issue2342.java | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/picocli/Issue2342.java diff --git a/src/test/java/picocli/Issue2342.java b/src/test/java/picocli/Issue2342.java new file mode 100644 index 000000000..dde4fd785 --- /dev/null +++ b/src/test/java/picocli/Issue2342.java @@ -0,0 +1,74 @@ +package picocli; + +import org.junit.Test; +import picocli.CommandLine.Option; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class Issue2342 { + static class CompileOptions { + @Option(names = { "--compiler-arguments" }, split = " ", arity = "*", + description = "Compiler arguments to use to compile generated sources.") + private List compilerArguments; + } + + @Test + public void testArgsWithSpaces() { + CompileOptions co = new CompileOptions(); + String[] args = new String[] { + "--compiler-arguments", + "\"--a-param with space\"", + "--parameters", + "\"--release 21\"", + "--nowarn" + }; + new CommandLine(co) + .setAllowOptionsAsOptionParameters(true) + .parseArgs(args); + List expected = new ArrayList(Arrays.asList(args)); + expected.remove(0); + assertEquals(expected, co.compilerArguments); + } + + @Test + public void testArgsWithSpacesQuotesTrimmed() { + CompileOptions co = new CompileOptions(); + String[] args = new String[] { + "--compiler-arguments", + "\"--a-param with space\"", + "--parameters", + "\"--release 21\"", + "--nowarn" + }; + new CommandLine(co) + .setAllowOptionsAsOptionParameters(true) + .setTrimQuotes(true) + .parseArgs(args); + List expected = Arrays.asList( + "--a-param", "with", "space", "--parameters", "--release", "21", "--nowarn"); + assertEquals(expected, co.compilerArguments); + } + + @Test + public void testArgsSeparateReleaseFrom21() { + CompileOptions co = new CompileOptions(); + String[] args = new String[] { + "--compiler-arguments", + "\"--a-param with space\"", + "--parameters", + "--release", + "21", + "--nowarn" + }; + new CommandLine(co) + .setAllowOptionsAsOptionParameters(true) + .parseArgs(args); + List expected = new ArrayList(Arrays.asList(args)); + expected.remove(0); + assertEquals(expected, co.compilerArguments); + } +}