NAnt: how to get the target name specified on the command line

From my NAnt script assembly, I am trying to figure out the name of this target, which was specified on the command line (or the default target if none is specified).

I was looking at the documentation at http://nant.sourceforge.net/release/0.85-rc1/help/functions/index.html#NAnt and didn't seem to find anything. The only loosely coupled function I can find is target :: get-current-target , which returns the name of the target I'm currently in, and not the target specified on the command line.

Does anyone know if there is a way to access this information? I also did not find anything in NAntContrib. It looks like it should be out there somewhere.

Thanks.

+6
nant target
source share
3 answers

Here is a simple function to see if a target was specified on the command line. Just call myFunctions :: isTargetOnCommandLine ('foo') , replacing the name of your target.

<script language="C#" prefix="myFunctions" > <code> <![CDATA[ [Function("isTargetOnCommandLine")] public static bool isTargetOnCommandLine(string target) { return (Array.IndexOf(Environment.GetCommandLineArgs(), target) != -1); } ]]> </code> </script> 
+2
source share

One thing you can do is define a property that will support the name for you. For each goal you set, check if this property is set and if it is set for the current target name, if it is empty.

+1
source share

Just faced a similar problem. I decided it this way, hope it helps a bit.

 <script language="C#"><code><![CDATA[ public static void ScriptMain(Project project) { project.Properties["command-line-targets"] = string.Empty; StringBuilder sb = new StringBuilder(); string[] args = Environment.GetCommandLineArgs(); for (int i = 1; i < args.Length; ++i) { string arg = args[i]; if (! arg.StartsWith("-")) { project.Log(Level.Info, " Command line target: " + arg); sb.Append(" ").Append(arg); } } if (sb.Length >= 1) { project.Properties["command-line-targets"] = sb.ToString(1, sb.Length - 1); } } ]]></code></script> <echo message="Command line targets: ${command-line-targets}" /> 

This code will not show you the default targets.

+1
source share

All Articles