Permalink
Browse files

allow Runner.display_help to be overridden or extended

  • Loading branch information...
1 parent 033ee7f commit 01c97f30903a02a4b7350392640943d17aae2c25 @cldwalker committed Mar 17, 2012
Showing with 38 additions and 2 deletions.
  1. +5 −1 lib/boson/runner.rb
  2. +5 −0 test/runner_library_test.rb
  3. +28 −1 test/runner_test.rb
View
@@ -4,6 +4,9 @@ module Boson
# Defines a RunnerLibrary for use by executables as a simple way to map
# methods to subcommands
class Runner < BareRunner
+ # Stores currently started Runner subclass
+ class <<self; attr_accessor :current; end
+
def self.inherited(mod)
@help_added ||= add_command_help
Inspector.enable all_classes: true, module: mod.singleton_class
@@ -14,6 +17,7 @@ def self.default_libraries
end
def self.start(args=ARGV)
+ Runner.current = self
Boson.in_shell = true
ENV['BOSONRC'] ||= ''
super
@@ -84,7 +88,7 @@ def new_attributes(name, library)
class DefaultCommandsRunner < Runner
desc "Displays command help"
def help(cmd)
- (cmd_obj = Command.find(cmd)) ? self.class.display_help(cmd_obj) :
+ (cmd_obj = Command.find(cmd)) ? Runner.current.display_help(cmd_obj) :
self.class.no_command_error(cmd)
end
end
@@ -7,4 +7,9 @@
Manager.load create_runner(:blah)
library('blarg').commands.should == ['blah']
end
+
+ it "can coexist with another runner library" do
+ Manager.load create_runner(:blah)
+ should_not_raise { Manager.load create_runner(:blih, library: :Blih) }
+ end
end
View
@@ -58,15 +58,30 @@ def no_run
end
end
+class ExtendedRunner < Boson::Runner
+ def self.display_help(cmd)
+ super
+ puts "And don't forget to eat BAACCCONN"
+ end
+end
+
describe "Runner" do
- before_all { $0 = 'my_command'; reset }
+ before_all { reset }
def my_command(cmd='')
+ $0 = 'my_command'
capture_stdout do
MyRunner.start Shellwords.split(cmd)
end
end
+ def extended_command(cmd='')
+ $0 = 'extended_command'
+ capture_stdout do
+ ExtendedRunner.start Shellwords.split(cmd)
+ end
+ end
+
def default_usage
<<-STR
Usage: my_command COMMAND [ARGS]
@@ -230,4 +245,16 @@ def default_usage
after_all { ENV['BOSONRC'] = File.dirname(__FILE__) + '/.bosonrc' }
end
+
+ describe "extend Runner" do
+ it "can extend help" do
+ extended_command('help help').should == <<-STR
+Usage: extended_command help CMD
+
+Description:
+ Displays command help
+And don't forget to eat BAACCCONN
+STR
+ end
+ end
end

0 comments on commit 01c97f3

Please sign in to comment.