Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add tracing switches #67

Merged
merged 3 commits into from

2 participants

@ggilder
Collaborator

@jamesrwhite

Incorporated your changes, renamed commands to #always_trace! and #never_trace!, and added more comprehensive specs.

James White and others added some commits
James White Fixing RSpec deprecation warning 'mock is deprecated. Use double inst…
…ead.'
6da72a9
James White Add ability to hide trace flags/globally enable tracing
Adding ability to hide --trace, -t from the menu and globally enable
tracing via
tj#16 (comment)

Don't show --trace text in error messages when it's disabled.
cf4c063
@ggilder ggilder Rename commands to `always_trace!`/`never_trace!`
Improve spec to test behavior of commands
0198e3f
@ggilder ggilder referenced this pull request
Closed

Fixes #16 and #17 #65

@ggilder
Collaborator

Waiting for build on travis (https://travis-ci.org/ggilder/commander/builds/18665032), will merge once that's green

@jamesrwhite

Awesome, will you be able to push it to rubygems provided the build passes?

@ggilder ggilder merged commit e88a3e9 into from
@ggilder ggilder deleted the branch
@ggilder
Collaborator

Just pushed 4.1.6 with these changes. Thanks again!

@jamesrwhite

Great, thanks for your help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 11, 2014
  1. @ggilder

    Fixing RSpec deprecation warning 'mock is deprecated. Use double inst…

    James White authored ggilder committed
    …ead.'
  2. @ggilder

    Add ability to hide trace flags/globally enable tracing

    James White authored ggilder committed
    Adding ability to hide --trace, -t from the menu and globally enable
    tracing via
    tj#16 (comment)
    
    Don't show --trace text in error messages when it's disabled.
  3. @ggilder

    Rename commands to `always_trace!`/`never_trace!`

    ggilder authored
    Improve spec to test behavior of commands
This page is out of date. Refresh to see the latest.
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")
Something went wrong with that request. Please try again.