Permalink
Browse files

Rename banner as task_banner (to distinguish it from overall banners for

Thor classes). Add Thor.banner method to set overall banner for a Thor
class. Add tests for Thor.banner
  • Loading branch information...
1 parent 0726f56 commit 0df49c988269cf161c52299df849a62423886e1c @rleber committed Jul 2, 2011
Showing with 26 additions and 7 deletions.
  1. +14 −4 lib/thor.rb
  2. +1 −1 lib/thor/base.rb
  3. +1 −1 lib/thor/runner.rb
  4. +2 −0 spec/fixtures/script.thor
  5. +8 −1 spec/thor_spec.rb
View
@@ -2,6 +2,15 @@
class Thor
class << self
+ # Sets a banner for the class as a whole
+ #
+ # ==== Parameters
+ # text<String>:: The banner text
+ #
+ def banner(text)
+ @class_banner = text
+ end
+
# Sets the default task when thor is executed without an explicit task to be called.
#
# ==== Parameters
@@ -156,7 +165,7 @@ def task_help(shell, task_name)
handle_no_task_error(meth) unless task
shell.say "Usage:"
- shell.say " #{banner(task)}"
+ shell.say " #{task_banner(task)}"
shell.say
class_options_help(shell, nil => task.options.map { |_, o| o })
if task.long_description
@@ -173,6 +182,7 @@ def task_help(shell, task_name)
# shell<Thor::Shell>
#
def help(shell, subcommand = false)
+ shell.say @class_banner if @class_banner
list = printable_tasks(true, subcommand)
Thor::Util.thor_classes_in(self).each do |klass|
list += klass.printable_tasks(false)
@@ -190,7 +200,7 @@ def printable_tasks(all = true, subcommand = false)
(all ? all_tasks : tasks).map do |_, task|
next if task.hidden?
item = []
- item << banner(task, false, subcommand)
+ item << task_banner(task, false, subcommand)
item << (task.description ? "# #{task.description.gsub(/\s+/m,' ')}" : "")
item
end.compact
@@ -269,12 +279,12 @@ def dispatch(meth, given_args, given_opts, config) #:nodoc:
new(args, opts, config).invoke_task(task, trailing || [])
end
- # The banner for this class. You can customize it if you are invoking the
+ # The banner for a task. You can customize it if you are invoking the
# thor class by another ways which is not the Thor::Runner. It receives
# the task that is going to be invoked and a boolean which indicates if
# the namespace should be displayed as arguments.
#
- def banner(task, namespace = nil, subcommand = false)
+ def task_banner(task, namespace = nil, subcommand = false)
"#{basename} #{task.formatted_usage(self, $thor_runner, subcommand)}"
end
View
@@ -414,7 +414,7 @@ def handle_no_task_error(task) #:nodoc:
end
def handle_argument_error(task, error) #:nodoc:
- raise InvocationError, "#{task.name.inspect} was called incorrectly. Call as #{self.banner(task).inspect}."
+ raise InvocationError, "#{task.name.inspect} was called incorrectly. Call as #{self.task_banner(task).inspect}."
end
protected
View
@@ -156,7 +156,7 @@ def list(search="")
private
- def self.banner(task, all = false, subcommand = false)
+ def self.task_banner(task, all = false, subcommand = false)
"thor " + task.formatted_usage(self, all, subcommand)
end
@@ -7,6 +7,8 @@ class MyScript < Thor
group :script
default_task :example_default_task
+
+ banner "MyScript does really cool stuff"
map "-T" => :animal, ["-f", "--foo"] => :foo
View
@@ -102,8 +102,15 @@
it "inherits all mappings from parent" do
MyChildScript.default_task.should == "example_default_task"
end
- end
+ end
+ describe "#banner" do
+ it "includes a class banner" do
+ content = capture(:stdout) { MyScript.start(["help"]) }
+ content.should =~ /MyScript does really cool stuff/m
+ end
+ end
+
describe "#desc" do
it "provides description for a task" do
content = capture(:stdout) { MyScript.start(["help"]) }

0 comments on commit 0df49c9

Please sign in to comment.