Skip to content

Commit

Permalink
Allow options to be defined for a class, which will be passed to #ini…
Browse files Browse the repository at this point in the history
…tialize.
  • Loading branch information
nex3 committed May 21, 2008
1 parent 75821f7 commit 3b64668
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def self.tasks
@tasks ||= TaskHash.new(self)
end

def self.opts
(@opts || {}).merge(self == Thor ? {} : superclass.opts)
end

def self.[](task)
namespaces = task.split(":")
klass = Thor::Util.constant_from_thor_path(namespaces[0...-1].join(":"))
Expand All @@ -55,11 +59,15 @@ def self.maxima
end

def self.start(args = ARGV)
options = Thor::Options.new(args, self.opts)
opts = options.getopts
args = options.args

meth = args.first
meth = @map[meth].to_s if @map && @map[meth]
meth ||= "help"

tasks[meth].parse new, args[1..-1]
tasks[meth].parse new(opts, *args), args[1..-1]
rescue Thor::Error => e
$stderr.puts e.message
end
Expand All @@ -72,6 +80,13 @@ def inherited(klass)

def method_added(meth)
meth = meth.to_s

if meth == "initialize"
@opts = @method_options
@method_options = nil
return
end

return if !public_instance_methods.include?(meth) || !@usage
register_klass_file self

Expand All @@ -91,6 +106,10 @@ def register_klass_file(klass, file = caller[1].split(":")[0])
subclasses << klass unless subclasses.include?(klass)
end
end

def initialize(opts = {}, *args)
p opts
end

map ["-h", "-?", "--help", "-D"] => :help

Expand Down

0 comments on commit 3b64668

Please sign in to comment.