gpt4 book ai didi

ant - 使用 和 exec 的参数数量可变

转载 作者:行者123 更新时间:2023-12-02 01:16:01 25 4
gpt4 key购买 nike

我正在使用 exec 从 ant build.xml 多次调用命令行程序。该命令行程序针对不同情况采用不同数量的参数。

目前我正在使用 exec 多次调用这个外部程序,代码看起来很困惑。例如:

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-h aaa"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
</exec>

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-h ddd"/>
<arg line="-u eee"/>
<arg line="-p fff"/>
<arg value="this is second test"/>
</exec>

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg line="tests.py"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
<arg value="this is another test"/>
</exec>

所以我计划使用 Macrodef 重构这个 build.xml 文件。

我的问题是如何将可变数量的参数传递给macrodef。如上所示,我需要根据场景将不同的参数传递给执行程序。

最佳答案

您可以使用 macrodef element支持这一点:

This is used to specify nested elements of the new task. The contents of the nested elements of the task instance are placed in the templated task at the tag name.

例如,您可以这样定义宏:

<macrodef name="call-exec">
<attribute name="dir"/>
<attribute name="executable"/>
<attribute name="failonerror"/>
<element name="arg-elements"/>
<sequential>
<exec dir="@{dir}" executable="@{executable}"
failonerror="@{failonerrer}" >
<arg-elements />
</exec>
</sequential>
</macrodef>

并这样调用它:

<call-exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
<arg-elements>
<arg line="tests.py"/>
<arg line="-u bbb"/>
<arg line="-p ccc"/>
<arg value="this is another test"/>
</arg-elements>
</call-exec>

关于ant - 使用 <macrodef> 和 exec 的参数数量可变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com