【问题标题】:How to accept an array of key:value arguments in a Thor generator command?如何在 Thor 生成器命令中接受一组键:值参数?
【发布时间】:2016-09-14 20:56:44
【问题描述】:

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

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

【问题讨论】:

    标签: ruby thor


    【解决方案1】:

    似乎已支持此功能。您只需将参数类型指定为:hash

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

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 1970-01-01
      • 2013-01-08
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2014-06-01
      相关资源
      最近更新 更多