简体   繁体   English

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

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

I am working on creating a generator similar to the Rails scaffolding generator.我正在创建一个类似于 Rails 脚手架生成器的生成器。 I would like to accept an array of key:value arguments.我想接受一组key:value参数。 Like this:像这样:

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

Currently my command class looks something like this:目前我的命令类看起来像这样:

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

What do I need to do in order to access that list of attributes?我需要做什么才能访问该属性列表?

Looks like this feature is already supported.看起来这个功能已经被支持了。 You just need to specify the argument type as a :hash .您只需要将参数类型指定为:hash

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM