Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #67 from ggilder/jamesrwhite/add-tracing-switches

Add tracing switches
  • Loading branch information...
commit e88a3e97b659f3d3be660c8c39e0c0443456c04c 2 parents 5225fa9 + 0198e3f
@ggilder ggilder authored
View
16 README.md
@@ -350,6 +350,22 @@ program :help_formatter, :compact
This abstraction could be utilized to generate HTML documentation for your executable.
+### Tracing
+
+By default the `-t` and `--trace` global options are provided to allow users to get a backtrace to aid debugging.
+
+You can disable these options:
+
+```ruby
+never_trace!
+```
+
+Or make it always on:
+
+```ruby
+always_trace!
+```
+
## Tips
When adding a global or command option, OptionParser implicitly adds a small
View
7 lib/commander/delegates.rb
@@ -1,8 +1,9 @@
module Commander
module Delegates
- %w( add_command command program run! global_option
- commands alias_command default_command ).each do |meth|
+ %w( add_command command program run! global_option
+ commands alias_command default_command
+ always_trace! never_trace! ).each do |meth|
eval <<-END, binding, __FILE__, __LINE__
def #{meth} *args, &block
::Commander::Runner.instance.#{meth} *args, &block
@@ -10,4 +11,4 @@ def #{meth} *args, &block
END
end
end
-end
+end
View
28 lib/commander/runner.rb
@@ -48,7 +48,7 @@ def self.instance
# Run command parsing and execution process.
def run!
- trace = false
+ trace = @always_trace || false
require_program :version, :description
trap('INT') { abort program(:int_message) } if program(:int_message)
trap('INT') { program(:int_block).call } if program(:int_block)
@@ -58,7 +58,7 @@ def run!
return
end
global_option('-v', '--version', 'Display version information') { say version; return }
- global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true }
+ global_option('-t', '--trace', 'Display backtrace when an error occurs') { trace = true } unless @never_trace
parse_global_options
remove_global_options options, @args
unless trace
@@ -72,7 +72,11 @@ def run!
OptionParser::MissingArgument => e
abort e.to_s
rescue => e
- abort "error: #{e}. Use --trace to view backtrace"
+ if @never_trace
+ abort "error: #{e}."
+ else
+ abort "error: #{e}. Use --trace to view backtrace"
+ end
end
else
run_active_command
@@ -85,7 +89,23 @@ def run!
def version
'%s %s' % [program(:name), program(:version)]
end
-
+
+ ##
+ # Enable tracing on all executions (bypasses --trace)
+
+ def always_trace!
+ @always_trace = true
+ @never_trace = false
+ end
+
+ ##
+ # Hide the trace option from the help menus and don't add it as a global option
+
+ def never_trace!
+ @never_trace = true
+ @always_trace = false
+ end
+
##
# Assign program information.
#
View
6 spec/command_spec.rb
@@ -50,21 +50,21 @@
end
it "calling the #call method by default when an object is called" do
- object = mock 'Object'
+ object = double 'Object'
object.should_receive(:call).once
@command.when_called object
@command.run 'foo'
end
it "should allow #action as an alias to #when_called" do
- object = mock 'Object'
+ object = double 'Object'
object.should_receive(:call).once
@command.action object
@command.run 'foo'
end
it "calling an arbitrary method when an object is called" do
- object = mock 'Object'
+ object = double 'Object'
object.should_receive(:foo).once
@command.when_called object, :foo
@command.run 'foo'
View
51 spec/runner_spec.rb
@@ -280,7 +280,7 @@
end.run!
}.should raise_error(SystemExit, /error: cookies!. Use --trace/)
end
-
+
it "should display callstack when using this switch" do
lambda {
new_command_runner 'foo', '--trace' do
@@ -289,7 +289,54 @@
}.should raise_error(RuntimeError)
end
end
-
+
+ describe "#always_trace!" do
+ it "should enable tracing globally, regardless of whether --trace was passed or not" do
+ lambda {
+ new_command_runner 'foo' do
+ always_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ }.should raise_error(RuntimeError)
+ end
+ end
+
+ describe "#never_trace!" do
+ it "should disable tracing globally, regardless of whether --trace was passed or not" do
+ lambda {
+ new_command_runner 'help', '--trace' do
+ never_trace!
+ end.run!
+ }.should raise_error(SystemExit, /invalid option: --trace/)
+ end
+
+ it "should not prompt to use --trace switch on errors" do
+ msg = nil
+ begin
+ new_command_runner 'foo' do
+ never_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ rescue SystemExit => e
+ msg = e.message
+ end
+ msg.should match(/error: cookies!/)
+ msg.should_not match(/--trace/)
+ end
+ end
+
+ context "conflict between #always_trace! and #never_trace!" do
+ it "respects the last used command" do
+ lambda {
+ new_command_runner 'foo' do
+ never_trace!
+ always_trace!
+ command(:foo) { |c| c.when_called { raise 'cookies!' } }
+ end.run!
+ }.should raise_error(RuntimeError)
+ end
+ end
+
describe "--version" do
it "should output program version" do
run('--version').should eq("test 1.2.3\n")
Please sign in to comment.
Something went wrong with that request. Please try again.