From c62e477a0b74de1c4f16643fce6851f44bf2e90e Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 26 Jun 2009 12:41:50 +0000 Subject: [PATCH] prefix and suffix attributes for command line arguments, PR 47365, submitted by Martin von Gagern git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@788677 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 +++ docs/manual/CoreTasks/javac.html | 11 ++++++ docs/manual/CoreTasks/rmic.html | 11 ++++++ docs/manual/OptionalTasks/javah.html | 11 ++++++ docs/manual/using.html | 14 ++++++++ .../apache/tools/ant/types/Commandline.java | 35 ++++++++++++++++++- .../tools/ant/types/CommandlineTest.java | 22 +++++++++++- 7 files changed, 106 insertions(+), 2 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index da9f06c8c..f00f94c1c 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -737,6 +737,10 @@ Other changes: * now uses previously undocumented SecureInputHandler shipped with Ant 1.7.1. + * Command line arguments for and similar tasks can now have + optional prefix and suffix attributes. + Bugzilla Report 47365 + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTasks/javac.html b/docs/manual/CoreTasks/javac.html index e3856ac14..4b5bc8ab1 100644 --- a/docs/manual/CoreTasks/javac.html +++ b/docs/manual/CoreTasks/javac.html @@ -484,6 +484,17 @@ used.

path + + prefix + See + Command-line Arguments. + Since Ant 1.8. + No + + + suffix + No + compiler Only pass the specified argument if the chosen diff --git a/docs/manual/CoreTasks/rmic.html b/docs/manual/CoreTasks/rmic.html index 788683b96..d5901c5be 100644 --- a/docs/manual/CoreTasks/rmic.html +++ b/docs/manual/CoreTasks/rmic.html @@ -244,6 +244,17 @@ used.

path + + prefix + See + Command-line Arguments. + Since Ant 1.8. + No + + + suffix + No + compiler Only pass the specified argument if the chosen diff --git a/docs/manual/OptionalTasks/javah.html b/docs/manual/OptionalTasks/javah.html index 36ebb8ad6..3e7735ee9 100644 --- a/docs/manual/OptionalTasks/javah.html +++ b/docs/manual/OptionalTasks/javah.html @@ -143,6 +143,17 @@ only if a given compiler implementation will be used.

path + + prefix + See + Command-line Arguments. + Since Ant 1.8. + No + + + suffix + No + implementation Only pass the specified argument if the chosen compiler diff --git a/docs/manual/using.html b/docs/manual/using.html index 081f9ef37..df2c444f8 100644 --- a/docs/manual/using.html +++ b/docs/manual/using.html @@ -580,6 +580,20 @@ that contain space characters, nested arg elements can be used.

line a space-delimited list of command-line arguments. + + prefix + A fixed string to be placed in front of the + argument. In the case of a line broken into parts, it will be + placed in front of every part. Since Ant 1.8. + No + + + suffix + A fixed string to be placed immediately after the + argument. In the case of a line broken into parts, it will be + placed after every part. Since Ant 1.8. + No +

It is highly recommended to avoid the line version diff --git a/src/main/org/apache/tools/ant/types/Commandline.java b/src/main/org/apache/tools/ant/types/Commandline.java index a12811763..c53979093 100644 --- a/src/main/org/apache/tools/ant/types/Commandline.java +++ b/src/main/org/apache/tools/ant/types/Commandline.java @@ -103,6 +103,9 @@ public class Commandline implements Cloneable { private String[] parts; + private String prefix = ""; + private String suffix = ""; + /** * Set a single commandline argument. * @@ -158,12 +161,42 @@ public class Commandline implements Cloneable { parts = new String[] {value.getAbsolutePath()}; } + /** + * Set the prefix to be placed in front of every part of the + * argument. + * + * @param prefix fixed prefix string. + * @since Ant 1.8.0 + */ + public void setPrefix(String prefix) { + this.prefix = prefix != null ? prefix : ""; + } + + /** + * Set the suffix to be placed at the end of every part of the + * argument. + * + * @param suffix fixed suffix string. + * @since Ant 1.8.0 + */ + public void setSuffix(String suffix) { + this.suffix = suffix != null ? suffix : ""; + } + /** * Return the constituent parts of this Argument. * @return an array of strings. */ public String[] getParts() { - return parts; + if (parts == null || parts.length == 0 + || (prefix.length() == 0 && suffix.length() == 0)) { + return parts; + } + String[] fullParts = new String[parts.length]; + for (int i = 0; i < fullParts.length; ++i) { + fullParts[i] = prefix + parts[i] + suffix; + } + return fullParts; } } diff --git a/src/tests/junit/org/apache/tools/ant/types/CommandlineTest.java b/src/tests/junit/org/apache/tools/ant/types/CommandlineTest.java index 559c5f76d..2fac757c5 100644 --- a/src/tests/junit/org/apache/tools/ant/types/CommandlineTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/CommandlineTest.java @@ -130,7 +130,7 @@ public class CommandlineTest extends TestCase { assertEquals("1 \'2\"3\'", Commandline.toString(new String[] {"1", "2\"3"})); } - public void testAwkCommand(){ + public void testAwkCommand() { Commandline c = new Commandline(); c.setExecutable("awk"); c.createArgument().setValue("'NR == 2 { print $NF }'"); @@ -140,4 +140,24 @@ public class CommandlineTest extends TestCase { assertEquals("awk", s[0]); assertEquals("'NR == 2 { print $NF }'", s[1]); } + + public void testPrefix() { + Commandline c = new Commandline(); + Commandline.Argument a = c.createArgument(); + a.setValue("foo"); + a.setPrefix("-f="); + String[] s = c.getCommandline(); + assertEquals(1, s.length); + assertEquals("-f=foo", s[0]); + } + + public void testSuffix() { + Commandline c = new Commandline(); + Commandline.Argument a = c.createArgument(); + a.setValue("foo"); + a.setSuffix(",1"); + String[] s = c.getCommandline(); + assertEquals(1, s.length); + assertEquals("foo,1", s[0]); + } }