如何在Thor生成器命令中接受一组key:value参数?

时间:2016-09-14 20:56:45

标签: ruby thor

我正在创建一个类似于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

要访问该属性列表,我需要做什么?

1 个答案:

答案 0 :(得分:1)

看起来已经支持此功能。您只需将参数类型指定为:hash

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