Skip to content

Commit

Permalink
Show debug information if --debug is supplied.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 2, 2009
1 parent 528a51f commit 2abff2b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
27 changes: 12 additions & 15 deletions lib/thor.rb
Expand Up @@ -127,24 +127,21 @@ def method_option(name, options)
# script.invoke(:task, first_arg, second_arg, third_arg)
#
def start(given_args=ARGV, config={})
config[:shell] ||= Thor::Base.shell.new
super do
meth = normalize_task_name(given_args.shift)
task = all_tasks[meth]

meth = normalize_task_name(given_args.shift)
task = all_tasks[meth]
if task
args, opts = Thor::Options.split(given_args)
config.merge!(:task_options => task.options)
else
args, opts = given_args, {}
end

if task
args, opts = Thor::Options.split(given_args)
config.merge!(:task_options => task.options)
else
args, opts = given_args, {}
task ||= Task.dynamic(meth)
trailing = args[Range.new(arguments.size, -1)]
new(args, opts, config).invoke(task, trailing || [])
end

task ||= Task.dynamic(meth)
trailing = args[Range.new(arguments.size, -1)]

new(args, opts, config).invoke(task, trailing || [])
rescue Thor::Error => e
config[:shell].error e.message
end

# Prints help information. If a task name is given, it shows information
Expand Down
13 changes: 13 additions & 0 deletions lib/thor/base.rb
Expand Up @@ -341,6 +341,19 @@ def namespace(name=nil)
end
end

# Default way to start generators from the command line.
#
def start(given_args=ARGV, config={}) #:nodoc:
config[:shell] ||= Thor::Base.shell.new
yield
rescue Thor::Error => e
if given_args.include?("--debug")
raise e
else
config[:shell].error e.message
end
end

protected

# Prints the class options per group. If an option does not belong to
Expand Down
16 changes: 7 additions & 9 deletions lib/thor/group.rb
Expand Up @@ -22,17 +22,15 @@ def desc(description=nil)
# inside the class.
#
def start(given_args=ARGV, config={})
config[:shell] ||= Thor::Base.shell.new
super do
if Thor::HELP_MAPPINGS.include?(given_args.first)
help(config[:shell])
return
end

if Thor::HELP_MAPPINGS.include?(given_args.first)
help(config[:shell])
return
args, opts = Thor::Options.split(given_args)
new(args, opts, config).invoke
end

args, opts = Thor::Options.split(given_args)
new(args, opts, config).invoke
rescue Thor::Error => e
config[:shell].error e.message
end

# Prints help information.
Expand Down
8 changes: 8 additions & 0 deletions spec/base_spec.rb
Expand Up @@ -220,4 +220,12 @@ def hello
MyCounter.get_from_super.must == 13
end
end

describe "#start" do
it "raises an error instead of rescueing if --debug is given" do
lambda {
MyScript.start ["what", "--debug"]
}.must raise_error(Thor::UndefinedTaskError, /the 'what' task of MyScript is private/)
end
end
end

0 comments on commit 2abff2b

Please sign in to comment.