【发布时间】:2013-01-15 20:28:53
【问题描述】:
使用 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)
同时为task1 和task2 声明method_option :type, :type => :string, :required => true, :default => 'foo' 的问题在于它违反了the DRY principle。有没有一种惯用的方法来处理这个问题?
【问题讨论】: