Ruby-Clamp

require "clamp"

class ClampTest < Clamp::Command
    #  1.命令行的参数使用主要分两类,一种是参数名称后面带参数值的方式,
    #我们这里成为option,我们可以通过option方法来处理。
    #  2.命令行不带参数名直接接参数值,成为parameter,我们用parameter函数
    #来处理
#=====================================================================================    
    #option函数最重要的是三个参数
    #1.option开关
    #2.option的参数名称(可以指定:flag表示没有参数值)
    #3.参数的描述
    
    #如果第一个参数不是数组,可以使用其作为变量名,如果是数组,建议使用:attribute_name
    option "--port","PORT11","Server's port";
    option ["--version","-v"],"VERSION","See the Version",:attribute_name =>:ver;
    
    option ["--Format","-f"],:flag,"Fommate the Date String",:attribute_name=>:format;#:flag是父类预定义的告诉程序,这个参数没有值
    
    option "--password",:flag,"ID",:require=>true; #表示必须写入的字段,否则会报错

#=====================================================================================
    #words后面和三个点之间必须有一个空格否则:word的类型回事string而不是array类型了
    parameter "Words ...","the thing to say",:attribute_name=>:words;

    def execute
        if !ver.nil?
            puts "version is #{ver}";
        end
        if !port.nil? #有值参数的判断方式
            puts "Port is #{port}";
        end
        if format? #无值参数的判断方法
            puts "You have checked Format";
        end

        info=words.join(" ");
        puts(info);



    end

end
#必须执行这个方法,才能让设置生效
ClampTest.run;
原文地址:https://www.cnblogs.com/zhanghaiyublog/p/Ruby.html