编译程序的必需参数?

问题描述 投票:2回答:1

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

./myprog ~/Music -r

代替

./myprog -d ~/Music -r

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

crystal-lang
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
© www.soinside.com 2019 - 2024. All rights reserved.