编译程序的必需参数?

时间:2017-12-18 13:19:30

标签: crystal-lang

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

./myprog ~/Music -r

而不是

./myprog -d ~/Music -r

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

1 个答案:

答案 0 :(得分: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