【发布时间】:2011-01-06 04:26:37
【问题描述】:
我正在尝试使用 ruby 库 Trollop 解析命令行。
#!/usr/bin/ruby
require 'net/http'
require 'trollop'
opts = Trollop::options do
opt :src, "src lang", :short => 'i', :type => String
opt :dest, "dest lang", :short => 'o', :type => String
end
opts.each do |key,val|
puts "#{key}: #{val}"
end
print opts["src"]
print opts["dest"]
这是输出:
$ ./translate.rb --src he --dest th
dest_given: true
src: he
dest: th
help: false
src_given: true
nilnil
当用opts.each 打印出哈希时,我可以看到有名为src 和dest 的键,它们的值是我所期望的。但是,为什么用opts["src"] 访问哈希值返回null?
【问题讨论】:
-
也许哈希键是一个符号?试试 opts[:src]
标签: ruby command-line-arguments