ARGV循环跳过Ruby中的if语句

时间:2017-07-06 13:39:03

标签: ruby argv

在我的脚本中,我使用ARGV流来获取命令行参数。有一段代码:

        <div align="center"
            style="border: 2px solid LightGreen; padding: 12px; font-size: 12px; width: 1050px">

            Company Name <select id="company" name="companyList"
                style="border: 2px solid LightGreen; font-size: 12px; width: 138px;"
                onchange="changeVal(this.value)">
                <option selected value="default">--Select Company Name--</option>

                <c:forEach var="item" items="${companiesList}">
                    <option value="${item}">${item}</option>
                </c:forEach>

            </select>&nbsp;&nbsp; MOLCode<select id="molcode" name="mol"
                style="border: 2px solid LightGreen; font-size: 12px; width: 138px;">

                <!--        <option selected value="default">--Select--</option>

                <option value="835725">835725</option>
                <option value="54322">54322</option> -->
                <option selected value="default">Select MOLCode</option>
            </select> &nbsp;&nbsp; Month<select id='gMonth1' name="month">
                <option selected value="12">--Select Month--</option>
                <option value='0'>Janaury</option>
                <option value='1'>February</option>
                <option value='2'>March</option>
                <option value='3'>April</option>
                <option value='4'>May</option>
                <option value='5'>June</option>
                <option value='6'>July</option>
                <option value='7'>August</option>
                <option value='8'>September</option>
                <option value='9'>October</option>
                <option value='10'>November</option>
                <option value='11'>December</option>
            </select> &nbsp;&nbsp; Year<input type="text" name="year" id="year" style="font-size: 10px" maxlength="4"
                placeholder="Enter a valid Year"
                onkeypress="return isNumber(event)"
                onblur="return checkyear(this.value)"> &nbsp;&nbsp; 
                <input type="submit" value="submit"
                style="font-size: 7pt; color: white; background-color: green; border: 2px solid #336600; padding: 3px"/>
            <!-- <input type="button" value="submit"
                style="font-size: 7pt; color: white; background-color: green; border: 2px solid #336600; padding: 3px"
                onclick="return checkyear(this.value),getCustomers()"/> -->
            &nbsp;&nbsp;  <input type="RESET" value="Reset" style="font-size: 7pt; color: white; background-color: green;

使用curl,我使用puppetdb API获取一些数据。一切正常,我想添加一些if语句来打印脚本帮助。它看起来像是:

ARGV.each do|a|
item = JSON.parse(%x{curl -X GET https://puppetdb.ai:8081/pdb/query/v4/facts/#{a} --tlsv1 --interface x.x.x.x --cacert certificate.pem --cert certificate-pem.cer --key certificate-privkey.pem})
  item.each do |h|
     arr << {"name" => h['certname'], "#{a}" => h['value'], "environment" => h['environment']}
  end
end

但脚本忽略&#34;帮助&#34;并填写&#34; help&#34;卷曲的论点。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试使用.strip!并查看Ruby String class documentation

当您遍历Ruby命令提供的每个参数时,这些可用于清理输入。最好只使用ARGV Array作为Array并查看Ruby Array class documentation以查看使用数组数据类型时Ruby中可用的方法方法(这是ARGVs的形式)..比如使用.include?("help")进行子串搜索。尽早使用这些方法文档对于理解Ruby为您提供的工具至关重要。