首页 文章

编译程序的必需参数?

提问于
浏览
2

有没有办法为水晶程序做必要的论证?例如

./myprog ~/Music -r

代替

./myprog -d ~/Music -r

所以如果没有[directory]参数,我的程序就不会运行 . 现在使用“option_parser”,只能做-arguments .

1 回答

  • 3

    无法使用 option_parser 创建所需的参数,但是如果没有传递的参数,您可以解析参数并抛出错误或退出:

    require "option_parser"
    
    directory = nil
    
    parser = OptionParser.new
    parser.on("-d DIR", "Directory [required]") do |d|
      directory = d
    end
    parser.parse ARGV
    
    if directory.nil?
      # directory argument was not set
      # print help and exit
      puts parser
      exit 1
    else
      # ...
    end
    

相关问题