Search code examples
ruby-on-railsthor

Why doesn't Thor recognize my command line option?


I am writing some rake tasks with Thor. In these thor tasks I am specifying some method options to make the command line more robust but the problem that I am running into is that thor is not recognizing my commands.

Here is an example task:

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

I have followed the Thor official web page on 'Options' WhatisThor and when I run thor help reverification_task:notifications:resend_to_soft_bounced_emails

It correctly outputs what I would expect to see in the command line argument:

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

When I execute thor reverification_task:notifications:resend_to_soft_bounced_emails -bt 20 -e 2000 this is the response:

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

What is the problem here? Any help would be greatly appreciated. Thanks.


Solution

  • You just mixed options with arguments. If you add arguments to your thor task definition, as you did in def resend_to_soft_bounced_emails(bounce_rate, amount_of_email), you need to call them as command line arguments too:

    thor reverification_task:notifications:resend_to_soft_bounced_emails 20 2000
    

    But you rather wanted to use options (passed on the command line with - prefixes), so you should remove the arguments from your task definition and refer to the options using the options hash:

    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