method_option
is defined in thor.rb and it takes the following parameters according to the documentation:
name<Symbol>::
The name of the argument.options<Hash>::
Described below.
Knowing this you can store the parameters to method_option
in an array and expand that array into separate parameters as method_option
is called.
require 'thor'
class Cli < Thor
shared_options = [:type, {:type => :string, :required => true, :default => 'foo'}]
desc 'task1', 'Task 1'
method_option *shared_options
def task1
end
desc 'task2', 'Task 2'
method_option *shared_options
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)
I have no idea if this is idiomatic and I do not think it is that elegant. Still, it is better than violating the DRY principle.