2013-03-14 140 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>