From 037c1087c64e149b3ecf73121b8cec95facba2e6 Mon Sep 17 00:00:00 2001 From: codistmonk Date: Tue, 11 Sep 2012 11:22:51 +0000 Subject: [PATCH] [Aprog] Updated CommandLineArgumentParser to handle integer lists. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@195 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- .../aprog/tools/CommandLineArgumentsParser.java | 34 +++++++++++++--------- .../tools/CommandLineArgumentsParserTest.java | 5 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Aprog/src/net/sourceforge/aprog/tools/CommandLineArgumentsParser.java b/Aprog/src/net/sourceforge/aprog/tools/CommandLineArgumentsParser.java index 8520a8c..36cce92 100644 --- a/Aprog/src/net/sourceforge/aprog/tools/CommandLineArgumentsParser.java +++ b/Aprog/src/net/sourceforge/aprog/tools/CommandLineArgumentsParser.java @@ -53,23 +53,29 @@ public final class CommandLineArgumentsParser { return defaultValue; } - final String[] rangeParameters = valueRepresentation.split(":"); - final int first = Integer.parseInt(rangeParameters[0]); - final int step; - final int last; + final List values = new ArrayList(); - if (rangeParameters.length == 2) { - step = 1; - last = Integer.parseInt(rangeParameters[1]); - } else if (rangeParameters.length == 3) { - step = Integer.parseInt(rangeParameters[1]); - last = Integer.parseInt(rangeParameters[2]); - } else { - step = 1; - last = first; + for (final String rangeRepresentation : valueRepresentation.split(",")) { + final String[] rangeParameters = rangeRepresentation.split(":"); + final int first = Integer.parseInt(rangeParameters[0]); + final int step; + final int last; + + if (rangeParameters.length == 2) { + step = 1; + last = Integer.parseInt(rangeParameters[1]); + } else if (rangeParameters.length == 3) { + step = Integer.parseInt(rangeParameters[1]); + last = Integer.parseInt(rangeParameters[2]); + } else { + step = 1; + last = first; + } + + values.addAll(newRange(first, step, last)); } - return toIntArray(newRange(first, step, last)); + return toIntArray(values); } // public final long get(final String key, final long defaultValue) { diff --git a/Aprog/test/net/sourceforge/aprog/tools/CommandLineArgumentsParserTest.java b/Aprog/test/net/sourceforge/aprog/tools/CommandLineArgumentsParserTest.java index 7a73974..b213312 100644 --- a/Aprog/test/net/sourceforge/aprog/tools/CommandLineArgumentsParserTest.java +++ b/Aprog/test/net/sourceforge/aprog/tools/CommandLineArgumentsParserTest.java @@ -37,11 +37,12 @@ public final class CommandLineArgumentsParserTest { @Test public final void test1() { - final CommandLineArgumentsParser arguments = new CommandLineArgumentsParser("a", "42", "b", "1:2:3"); + final CommandLineArgumentsParser arguments = new CommandLineArgumentsParser("a", "42", "b", "1:2:5", "c", "0,-2,8:10"); assertArrayEquals(new int[] { 42 }, arguments.get("a", 41)); assertArrayEquals(new int[] { 42 }, arguments.get("d", 42)); - assertArrayEquals(new int[] { 1, 3 }, arguments.get("b")); + assertArrayEquals(new int[] { 1, 3, 5 }, arguments.get("b")); + assertArrayEquals(new int[] { 0, -2, 8, 9, 10 }, arguments.get("c")); } } -- 2.11.4.GIT