How to make two thor tasks share options?

前端 未结 5 1307
予麋鹿
予麋鹿 2021-02-14 04:37

With Thor one can use method_option to set the options for a particular task. To set the options for all tasks in a class one can use class_option. But what about t

5条回答
  •  情书的邮戳
    2021-02-14 05:11

    I would just use a superclass like this:

    require 'thor'
    
    class CliBase < Thor
      def self.shared_options
    
        method_option :verbose,
                      :aliases => '-v',
                      :type => :boolean,
                      :desc => 'Verbose',
                      :default => false,
                      :required => false
    
      end
    end
    

    ... then subclass as follows:

    require 'cli_base'
    
    class Cli < CliBase
      desc 'task1', 'Task 1'
      shared_options
      def task1
      end
    
      desc 'task2', 'Task 2'
      shared_options
      method_option :value, :type => :numeric
      def task2
      end
    
      desc 'task3', 'Task 3'
      method_option :colors, :type => :boolean, :aliases => '-c'
      def task3
      end
    end
    
    Cli.start(ARGV)
    

提交回复
热议问题