Thor 可以method_option
用来设置特定任务的选项。要为一个类中的所有任务设置选项,可以使用class_option
. 但是,如果一个人想要一个类的一些任务,但不是全部,共享选项呢?
在下文中task1
,和task2
共享期权,但他们不共享所有期权,也不与 共享期权task3
。
require 'thor'
class Cli < Thor
desc 'task1', 'Task 1'
method_option :type, :type => :string, :required => true, :default => 'foo'
def task1
end
desc 'task2', 'Task 2'
method_option :type, :type => :string, :required => true, :default => 'foo'
method_option :value, :type => :numeric
def task2
end
desc 'task3', 'Task 3'
method_option :verbose, :type => :boolean, :aliases => '-v'
def task3
end
end
Cli.start(ARGV)
声明method_option :type, :type => :string, :required => true, :default => 'foo'
两者task1
的问题task2
是它违反了 DRY 原则。有没有一种惯用的方法来处理这个?