为什么Thor不识别我的命令行选项?

时间:2016-04-11 17:22:58

标签: ruby-on-rails thor

我正在和Thor一起写一些rake任务。在这些Thor任务中,我指定了一些方法选项,以使命令行更加健壮,但我遇到的问题是Thor没有识别我的命令。

以下是一个示例任务:

module ReverificationTask
  class Notifications < Thor
     option :bounce_threshold, :aliases => '-bt', :desc => 'Sets bounce rate', :required => true, :type => :numeric
     option :num_email, :aliases => '-e', :desc => 'Sets the amount of email', :required => true, :type => :numeric

     desc 'resend_to_soft_bounced_emails [BOUNCE_THRESHOLD] [NUM_EMAIL]'

     def resend_to_soft_bounced_emails(bounce_rate, amount_of_email)
        Reverification::Process.set_amazon_stat_settings(bounce_rate, amount_of_email)
        Reverification::Mailer.resend_soft_bounced_notifications
     end
  end
end

我在“选项”WhatisThor上关注了Thor官方网页,当我运行thor help reverification_task:notifications:resend_to_soft_bounced_emails

它正确地输出了我期望在命令行参数中看到的内容:

Usage:
thor reverification_task:notifications:resend_to_soft_bounced_emails [BOUNCE_THRESHOLD] [NUM_EMAIL] -bt, --bounce-threshold=N -e, --num-email=N

Options:
  -bt, --bounce-threshold=N  # Sets bounce rate
  -e, --num-email=N          # Sets the amount of email

当我执行thor reverification_task:notifications:resend_to_soft_bounced_emails -bt 20 -e 2000时,这是回复:

No value provided for required options '--bounce-threshold'

这是什么问题?任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

你只是用参数混合选项。如果您在thor def resend_to_soft_bounced_emails(bounce_rate, amount_of_email)中添加参数到thor任务定义,则需要将它们称为命令行参数:

thor reverification_task:notifications:resend_to_soft_bounced_emails 20 2000

但您更愿意使用选项(在-前缀的命令行上传递),因此您应该从任务定义中删除参数并使用options哈希引用选项:< / p>

def resend_to_soft_bounced_emails
  Reverification::Process.set_amazon_stat_settings(options[:bounce_threshold], 
                                                   options[:num_email])
  Reverification::Mailer.resend_soft_bounced_notifications
end