有什么方法可以为 Crystal 程序提供所需的参数吗? 例如
./myprog ~/Music -r
而不是
./myprog -d ~/Music -r
所以如果没有 [directory] 参数,我的程序将不会运行。现在使用“option_parser”并且只能执行 -arguments。
最佳答案
无法使用 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
关于crystal-lang - 编译程序所需的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47869402/