簡體   English   中英

如何在 Thor 生成器命令中接受一組鍵:值參數?

[英]How to accept an array of key:value arguments in a Thor generator command?

我正在創建一個類似於 Rails 腳手架生成器的生成器。 我想接受一組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

我需要做什么才能訪問該屬性列表?

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM