2017-01-09 28 views
0

我想創建一個沒有參數的參數的rake任務。OptionParser :: MissingArgument

task :mytask => :environment do 
    options = Hash.new 
    OptionParser.new do |opts| 
    opts.on('-l', '--local', 'Run locally') do 
     options[:local] = true 
    end 
    end.parse! 

    # some code 

end 

但它拋出:

$ rake mytask -l 
rake aborted! 
OptionParser::MissingArgument: missing argument: -l 

同時:

$ rake mytask -l random_arg 
ready 

爲什麼?


  • 耙10.4.2
  • JRuby的1.7.13
+0

我不知道OptionParser是這裏最好的計劃。 'rake'已經有了自己的選項解析和一個方法,最後在'VAR = value'處傳入數據,就像'LOCAL = 1'一樣。看到[這個答案](http://stackoverflow.com/questions/825748/how-to-pass-command-line-arguments-to-a-rake-task)爲例。 – tadman

+0

@tadman我希望有我的選擇,因爲標誌 – Viktor

回答

1

如果您還承諾這種方法,你需要從rake自己的論點分開你的任務的論點:

rake mytask -- -l 

--指「的主要論點結束」,其餘的都是你的任務。

您需要調整您的參數解析觸發只對那些特定的參數:

task :default do |t, args| 
    # Extract all the rake-task specific arguments (after --) 
    args = ARGV.slice_after('--').to_a.last 

    options = { } 
    OptionParser.new do |opts| 
    opts.on('-l', '--local', 'Run locally') do 
     options[:local] = true 
    end 
    end.parse!(args) 

    # some code 
end 

走向看,這種方式通常是非常的混亂,不是很人性化,所以如果你能避免它與採用其他通常更好的方法。

+0

耙忽略了我的說法,這樣 – Viktor

+0

這就是它似乎歷史上的工作,但你說得對,這似乎並不在rake'的'新版本工作。 – tadman

+0

我在這裏添加了一個專門針對這些自定義參數的版本。 – tadman

相關問題