简体   繁体   中英

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. I would like to accept an array of key:value arguments. 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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