Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit f8860afeede03f03afd87b905ecea7ddbaa84e3b
Author: James White <james.white.15@gmail.com>
Date:   Mon Feb 10 16:44:19 2014 +0000

    Add helper commands to delegates and some basic tests

commit 2c4cef0
Author: James White <james.white.15@gmail.com>
Date:   Sun Feb 9 00:48:52 2014 +0000

    Don't show --trace text in error messages when it's disabled. Also updated naming as tracing isn't just hidden from the menu it's actually completely disabled

commit d796de5
Author: James White <james.white.15@gmail.com>
Date:   Sun Feb 9 00:07:00 2014 +0000

    Adding ability to hide --trace, -t from the menu and globally enable tracing via #16 (comment). Usage: Commander::Runner.instance.hide_trace_help
  • Loading branch information
James White committed Feb 10, 2014
1 parent e1daaf1 commit fdd3945
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/commander/delegates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module Commander
module Delegates
%w( add_command command program run! global_option
commands alias_command default_command ).each do |meth|
commands alias_command default_command
enable_tracing disable_tracing ).each do |meth|
eval <<-END, binding, __FILE__, __LINE__
def #{meth} *args, &block
::Commander::Runner.instance.#{meth} *args, &block
Expand Down
24 changes: 21 additions & 3 deletions lib/commander/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.instance
# Run command parsing and execution process.

def run!
trace = false
trace = @tracing_enabled || 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)
Expand All @@ -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 @tracing_disabled
parse_global_options
remove_global_options options, @args
unless trace
Expand All @@ -72,7 +72,11 @@ def run!
OptionParser::MissingArgument => e
abort e.to_s
rescue => e
abort "error: #{e}. Use --trace to view backtrace"
if @tracing_disabled
abort "error: #{e}."
else
abort "error: #{e}. Use --trace to view backtrace"
end
end
else
run_active_command
Expand All @@ -85,6 +89,20 @@ def run!
def version
'%s %s' % [program(:name), program(:version)]
end

##
# Enabled tracing on all executions (bipasses --trace)

def enable_tracing
@tracing_enabled = true
end

##
# Hide the trace option from the Help Menus and don't add it as a global option

def disable_tracing
@tracing_disabled = true
end

##
# Assign program information.
Expand Down
12 changes: 12 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@
}.should raise_error(RuntimeError)
end
end

describe "#enable_tracing" do
it "should enable tracing globally, regardless of whether --trace was passed or not" do
enable_tracing.should eq true
end
end

describe "#disable_tracing" do
it "should disable tracing globally, regardless of whether --trace was passed or not" do
disable_tracing.should eq true
end
end

describe "--version" do
it "should output program version" do
Expand Down

0 comments on commit fdd3945

Please sign in to comment.