Permalink
Browse files

Add ability to hide trace flags/globally enable tracing

Adding ability to hide --trace, -t from the menu and globally enable
tracing via
#16 (comment)

Don't show --trace text in error messages when it's disabled.
  • Loading branch information...
James White authored and ggilder committed Feb 10, 2014
1 parent 6da72a9 commit cf4c063f592c22330b5bfd8cc01384517c90238d
Showing with 39 additions and 4 deletions.
  1. +4 −0 README.md
  2. +2 −1 lib/commander/delegates.rb
  3. +21 −3 lib/commander/runner.rb
  4. +12 −0 spec/runner_spec.rb
View
@@ -350,6 +350,10 @@ 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 set which allow you to get a backtrace to aid debugging. You may not want this in your program though, the ````enable_tracing```` and ````disable_tracing```` symbols allow you to control this behaviour.
+
## Tips
When adding a global or command option, OptionParser implicitly adds a small
@@ -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
View
@@ -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)
@@ -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
@@ -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
@@ -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.
View
@@ -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

0 comments on commit cf4c063

Please sign in to comment.