首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用<macrodef>和exec的可变参数数

使用<macrodef>和exec的可变参数数
EN

Stack Overflow用户
提问于 2013-12-17 10:06:04
回答 1查看 3.8K关注 0票数 4

我正在使用exec从ant build.xml多次调用命令行程序。这个命令行程序为不同的情况接受可变数量的参数。

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

代码语言:javascript
复制
<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>

因此,我计划使用宏file重构这个build.xml文件。

我的问题是如何将变量数的参数传递给宏。如上面所示,我需要根据场景将不同的参数传递给exec-ed程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-17 11:24:14

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

这用于指定新任务的嵌套元素。任务实例的嵌套元素的内容放在标记名称的模板化任务中。

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

代码语言:javascript
复制
<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>

就这样叫它:

代码语言:javascript
复制
<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>
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20631191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档