2016-09-14 33 views
1

我正在創建類似於Rails腳手架生成器的生成器。我想接受一系列key:value參數。就像這樣:如何在Thor生成器命令中接受一個key:value參數數組?

mycli generate model BlogPost title:string body:text published:datetime 

目前我的命令類看起來是這樣的:

require "thor" 

module Mycli 
    module Generators 
    class Model < Thor::Group 
     include Thor::Actions 

     argument :model_name 
     # argument :model_attributes # TODO: figure out how to get array of attributes 

     def self.source_root 
     File.dirname(__FILE__) 
     end 

     def generate_model 
     template('templates/model.tt', "app/models/#{model_name}.rb") 
     end 

     def generate_migration 
     template('templates/migration.tt', "migrations/#{model_name}.rb") 
     end 
    end 
    end 
end 

我需要什麼,以訪問屬性該列表呢?

回答

1

看起來像這個功能已經被支持。您只需要將參數類型指定爲:hash即可。

argument :model_attributes, optional: true, type: :hash