Skip to content

Commit

Permalink
Added long descriptions to tasks for more detailed help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy authored and indirect committed May 20, 2010
1 parent 396c4a3 commit 473ea67
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
19 changes: 17 additions & 2 deletions lib/thor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def desc(usage, description, options={})
end
end

# Defines the long description of the next task.
#
# ==== Parameters
# long description<String>
#
def long_desc(long_description, options={})
if options[:for]
task = find_and_refresh_task(options[:for])
task.long_description = long_description if long_description
else
@long_desc = long_description
end
end

# Maps an input to a task. If you define:
#
# map "-T" => "list"
Expand Down Expand Up @@ -153,7 +167,8 @@ def task_help(shell, task_name)
shell.say " #{banner(task)}"
shell.say
class_options_help(shell, nil => task.options.map { |_, o| o })
shell.say task.description
shell.say "Description:"
shell.say task.long_description.gsub(/^/m, ' \0')
end

# Prints help information for this class.
Expand Down Expand Up @@ -205,7 +220,7 @@ def baseclass #:nodoc:

def create_task(meth) #:nodoc:
if @usage && @desc
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
tasks[meth.to_s] = Thor::Task.new(meth, @desc, @long_desc, @usage, method_options)
@usage, @desc, @method_options = nil
true
elsif self.all_tasks[meth.to_s] || meth.to_sym == :method_missing
Expand Down
2 changes: 1 addition & 1 deletion lib/thor/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def baseclass #:nodoc:
end

def create_task(meth) #:nodoc:
tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil, nil)
true
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/thor/task.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Thor
class Task < Struct.new(:name, :description, :usage, :options)
class Task < Struct.new(:name, :description, :long_description, :usage, :options)
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/

# A dynamic task that handles method missing scenarios.
class Dynamic < Task
def initialize(name, options=nil)
super(name.to_s, "A dynamically-generated task", name.to_s, options)
super(name.to_s, "A dynamically-generated task", name.to_s, name.to_s, options)
end

def run(instance, args=[])
Expand All @@ -17,8 +17,8 @@ def run(instance, args=[])
end
end

def initialize(name, description, usage, options=nil)
super(name.to_s, description, usage, options || {})
def initialize(name, description, long_description, usage, options=nil)
super(name.to_s, description, long_description || description, usage, options || {})
end

def initialize_copy(other) #:nodoc:
Expand Down Expand Up @@ -99,4 +99,4 @@ def handle_no_method_error?(instance, error, caller)
end

end
end
end
4 changes: 4 additions & 0 deletions spec/fixtures/script.thor
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ END
end

desc "long_description", "a" * 80
long_desc <<-HERE_DOC
This is a really really really long description.
Here you go. So very long
HERE_DOC
def long_description
end

Expand Down
4 changes: 2 additions & 2 deletions spec/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def task(options={})
options[key] = Thor::Option.parse(key, value)
end

@task ||= Thor::Task.new(:can_has, "I can has cheezburger", "can_has", options)
@task ||= Thor::Task.new(:can_has, "I can has cheezburger", "I can has cheezburger\nLots and lots of it", "can_has", options)
end

describe "#formatted_usage" do
Expand Down Expand Up @@ -46,7 +46,7 @@ def task(options={})

describe "#dup" do
it "dup options hash" do
task = Thor::Task.new("can_has", nil, nil, :foo => true, :bar => :required)
task = Thor::Task.new("can_has", nil, nil, nil, :foo => true, :bar => :required)
task.dup.options.delete(:foo)
task.options[:foo].must_not be_nil
end
Expand Down
11 changes: 8 additions & 3 deletions spec/thor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ def shell
Options:
[--force] # Force to do some fooing
do some fooing
This is more info!
Everyone likes more info!
Description:
do some fooing
This is more info!
Everyone likes more info!
END
end

Expand All @@ -206,6 +207,10 @@ def shell
it "normalizes names before claiming they don't exist" do
capture(:stdout) { MyScript.task_help(shell, "name-with-dashes") }.must =~ /thor my_script:name-with-dashes/
end

it "uses the long description if it exists" do
capture(:stdout) { MyScript.task_help(shell, "long_description") }.must =~ /really really really long description/
end
end

describe "instance method" do
Expand Down

0 comments on commit 473ea67

Please sign in to comment.