2013-03-14 137 views
2

我有要求用户输入一个ant脚本。我想,以确保正确的输入类型已经由用户输入,如果没有,提示他们输入的东西是正确的。如何检查输入任务中的输入是否为空?

<input message="secure-input:" addproperty="the.password" > 
</input> 

我必须检查输入是否为空然后我会提示他们重新输入。

是否有可能在蚂蚁输入任务?

回答

3

大多数条件子句的最好,让您fail脚本如果不符合某些条件。我想象你正在收集用户的一些输入,所以如果用户提供空密码失败可能不是最好的可用性体验,因为他将不得不重新输入一切......

此外,只是测试一个空字符串似乎是一个糟糕的验证,如果用户提交了一堆空格呢?

我(诚然hackish的)建议如下:

<target name="askPassword"> 
    <local name="passwordToValidate"/> 
    <input message="secure-input:" addproperty="passwordToValidate"/> 

    <script language="javascript"> 
     <![CDATA[ 
      passwordToValidate = project.getProperty("passwordToValidate"); 

      //You can add more validations here... 
      if(passwordToValidate.isEmpty()){ 
       println("The password cannot be empty."); 
       project.executeTarget('askPassword'); 
      } 
     ]]> 
    </script> 

    <property name="the.password" value="${passwordToValidate}"/>  
</target> 

所以亮点:

  1. 使用local task设置一个局部范围的 “passwordToValidate” 属性(请记住,一旦你设置一个属性上的ant脚本的大部分任务不允许你重写,所以递归是有问题)
  2. 测试你输入一个脚本(也许添加一些验证)
  3. 如果测试失败,调用目标再次
  4. 如果测试成功,分配本地范围的属性,在全球范围的一个

我的2美分。

+0

感谢Paulp ..我从来没有想过的JavaScript可以Ant构建内部使用... :) – Shashi 2013-03-15 05:46:27

1

您可以使用条件任务以检查输入是不是空的,但你也可以使用antcontrib添加的if/else和显示用户相应的消息:

<if> 
    <not> 
     <length string="${the.password}" trim="true" when="greater" length="0" /> 
    </not> 
</if> 
<then> 
    <echo>Your password is empty!</echo> 
</then>