Bug 30651 - Add ability to pass arbitrary arguments after a special -- marker as special $ properties
Summary: Add ability to pass arbitrary arguments after a special -- marker as special ...
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.6.2
Hardware: All other
: P3 enhancement with 1 vote (vote)
Target Milestone: ---
Assignee: Ant Notifications List
Keywords: PatchAvailable
Depends on:
Reported: 2004-08-13 15:27 UTC by Dominique Devienne
Modified: 2009-07-31 05:38 UTC (History)
0 users

Patch to Launcher.java for adding -- argument logic (2.63 KB, patch)
2004-08-13 15:28 UTC, Dominique Devienne
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique Devienne 2004-08-13 15:27:48 UTC
Even though Ant is not designed to be a scripting environment, it is a shame 
that developpers have to write some (usually crappy) .bat or .sh scripts to 
startup little interactive demo/example/benchmark programs, when all the 
CLASSPATH/PATH/LD_LIBRARY_PATH is already cleanly encapsulated in the build 
file. Furthermore, these scripts duplicate all the path info of the build file 
itself (which in my case is computed by custom tasks based on dependency info, 
Maven style).

In a different life, I used to startup the X server on Solaris using the 
xserver program (I think that's the name), which recognized a special -- 
argument, after which all other args were not for xserver itself, but for the 
process it forked (the separate X11 server program).

I thus added a similar behavior to the new Ant 1.6.x Launcher class. It now 
recognizes a new -- argument, and converts all arguments after it into Ant 
properties passed in to the traditional Ant main. The properties defined take 
the shell/perl like $0, $1, $2, etc... notation, with the special $# property 
for the total number of arguments after -- (including $0), and the special $* 
property that contains all the arguments (excluding $0) as a string (with 
simple logic to quote arguments that contain spaces or tabs).

Given the following command lines, the properties are defined as below using 
the following target (that uses custom tasks, but that's beside the point):

  <target name="start" xmlns:bm="antlib:com.lgc.buildmagic">
    <bm:echo unless="$0" message="Warning: No -- arguments specified" />
    <bm:echo><propertyset><propertyref prefix="$" /></propertyset></bm:echo>

C:\oss\org_apache\ant16>build start
Warning: No -- arguments specified

C:\oss\org_apache\ant16>build start --
The -- argument must be followed by one or more argument(s)

C:\oss\org_apache\ant16>build start -- toto
$0 = toto
$# = 1
$* =

C:\oss\org_apache\ant16>build start -- toto titi tutu
$1 = titi
$0 = toto
$# = 3
$* = titi tutu
$2 = tutu

C:\oss\org_apache\ant16>build start -- toto "titi tyty" tutu
$1 = titi tyty
$0 = toto
$# = 3
$* = "titi tyty" tutu
$2 = tutu

Using this new -- argument passing technique with <java> and <exec>, Ant is now 
much more flexible to start up arbitrary Java programs (by using $0 in <java>'s 
classname attribute) and either <arg line="${$*}" /> or multiple <arg 
value="${$1}" /> style arguments.

Thanks for considering this addition to Ant. --DD
Comment 1 Dominique Devienne 2004-08-13 15:28:26 UTC
Created attachment 12420 [details]
Patch to Launcher.java for adding -- argument logic