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

Why doesn't Thor recognize my command line option?

我正在用 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

我在 'Options' 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'

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

您只是将选项与参数混合在一起。如果您向 thor 任务定义添加参数,就像您在 def resend_to_soft_bounced_emails(bounce_rate, amount_of_email) 中所做的那样,您也需要将它们作为命令行参数调用:

thor reverification_task:notifications:resend_to_soft_bounced_emails 20 2000

但是您更想使用选项(在带有 - 前缀的命令行上传递),因此您应该从任务定义中删除参数并使用 options 散列引用选项:

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