Sunday, December 11, 2005

NAnt exec: arg || commandline

NAnt contains an exec task that is used for executing a system command. The exec task is commonly used to execute osql, ruby scripts, simian, etc. Generally, when using exec you will need to pass arguments to the program you are executing. Looking at the documentation for NAnt it appears you have two options: arg or commandline.

Commandline is documented as: The command-line arguments for the program. The syntax is:
<exec program="ruby" commandline="script.rb arg1 arg2 arg3"/>
Using exec and commandline should be the same as executing the following statement at a command line.
$ ruby script.rb arg1 arg2 arg3

Arg is documented as: The command-line arguments for the external program. The syntax is:
<exec program="ruby">
<arg value="script.rb"/>
<arg value="arg1"/>
<arg value="arg2"/>
<arg value="arg3"/>
</exec>
Using exec and arg should be the same as executing the following statement at a command line.
$ ruby "script.rb" "arg1" "arg2" "arg3"

Obviously, the difference is that arg adds quotes around the arguments. This may seem like a small issue, but I've seen time wasted on this minor difference. The most common problematic scenario occurs when someone tries to combine multiple args into one arg tag.
<exec program="ruby">
<arg value="script.rb arg1 arg2 arg3"/>
</exec>
The problem with the above example is that it is the same as executing the following statement at a command line.
$ ruby "script.rb arg1 arg2 arg3"

This won't work in a build and at the command line it would produce:
ruby: No such file or directory -- script.rb arg1 arg2 arg3 (LoadError)

Using commandline or arg is likely a matter of personal preference as long as you are educated on the differences.

4 comments:

  1. Anonymous12:53 PM

    I want mix an argument with quotes and arguments without quotes.
    Something like this:

    exec program="devenv"
    arg file="G:/Mi Sourcesafe/latest/GENESYSENGINE/Src/System/Chamaleon/Chamaleon.vcproj"
    arg value="/build"
    arg value="Release"


    It can't works. Help, please.

    ReplyDelete
  2. Anonymous9:58 PM

    If you use a single quote as the string delimiter for the commandline string, you can use double quotes within the commandline string.

    For example:
    <exec program="devenv" commandline='"c:/my work/project 1/foo.sln" /build Debug'/>

    ReplyDelete
  3. Anonymous11:07 AM

    Sorry abou being late to this party but,

    While this does not work:

    <exec program="ruby">
    <arg value="script.rb arg1 arg2 arg3"/>
    </exec>

    Now this should work:
    <exec program="ruby">
    <arg line="script.rb arg1 arg2 arg3"/>
    </exec>

    ReplyDelete
  4. I have used all the different ways to pass command line args, however nant always enclose the whole arguments with 2 brackets, this is what I have:
    <target name="setup">
    <exec program="${regexp.exe}" verbose="true">
    <arg line="/L (OutputBaseFilename=)(.*) \1 < deploy.iss > temp.iss" />
    </exec>
    </target>
    and this is what I get:
    [exec] Starting 'D:\utils\regexp.exe (/L (OutputBaseFilename=)(.*) \1 < deploy.iss > temp.iss)' in 'D:\'
    [exec] Too many command line parameters.

    Could you help please?

    ReplyDelete

Note: Only a member of this blog may post a comment.