我必须使用这个命令来执行脚本:
$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"所以,请告诉我什么是正确的方式,我可以使论点可选。谢谢!
发布于 2017-03-27 06:00:22
您当前的def execute (keyword)实现是1 (也就是说,它声明了一个强制参数)。如果您想要省略该参数,请将其设置为可选参数。
变化
def execute (keyword)
#read file then display the results matching `keyword`
end至:
def execute (keyword = nil)
if keyword.nil?
# NO KEYWORD PASSED
else
# NORMAL PROCESSING
end
endhttps://stackoverflow.com/questions/43038802
复制相似问题