2016-04-11 35 views
1

我正在寫Thor的一些rake任務。在這些thor任務中,我指定了一些方法選項來使命令行更加健壯,但是我遇到的問題是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 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' 

這裏有什麼問題?任何幫助將不勝感激。謝謝。

回答

0

您只是將參數與參數混合在一起。如果添加參數您雷神任務定義,只要在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 
相關問題