有了托尔,人们就可以使用method_option https://github.com/wycats/thor/wiki/Method-Options设置特定任务的选项。要为班级中的所有任务设置选项,可以使用class_option
。但是,如果一个人希望某个类的某些任务(而不是全部)共享选项,该怎么办?
在下面的task1
and 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
and task2
是它违反了干燥原则 http://en.wikipedia.org/wiki/Don%27t_repeat_yourself。有处理这个问题的惯用方法吗?