【发布时间】:2017-03-27 05:53:30
【问题描述】:
我必须使用这个命令来执行脚本:
$ruby file.rb keyword --format oneline --no-country-code --api-key=API
format、no-country-code、api-key 是thor 选项。 keyword 是我方法中的参数:
class TwitterTrendReader < Thor
method_option :'api-key', :required => true
method_option :format
method_option :'no-country-code', :type => :boolean
def execute (keyword)
#read file then display the results matching `keyword`
end
default_task :execute
end
问题是keyword 是可选的,如果我运行没有keyword 的命令,脚本应该打印文件中的所有条目,否则,它只显示匹配keyword 的条目。
所以我有这个代码:
if ARGV.empty?
TwitterTrendReader.start ''
else
TwitterTrendReader.start ARGV
end
它仅在我指定keyword 但没有keyword 时才有效,我得到这个:
$ruby s3493188_p3.rb --api-key="abC9hsk9"
ERROR: "s3493188_p3.rb execute" was called with no arguments
Usage: "s3493188_p3.rb [keyword] --format oneline --no-country-code --api-key=API-KEY"
所以请告诉我我可以使参数可选的正确方法是什么。谢谢!
【问题讨论】: