Permalink
Browse files

Ability to exit and show help

Closes #36
  • Loading branch information...
1 parent d87e965 commit 7263acedbac7d4a51a83cd1793334deec49462aa @davetron5000 committed Mar 29, 2012
Showing with 29 additions and 0 deletions.
  1. +7 −0 lib/methadone/exit_now.rb
  2. +2 −0 lib/methadone/main.rb
  3. +20 −0 test/test_main.rb
@@ -21,5 +21,12 @@ def exit_now!(exit_code,message=nil)
raise Methadone::Error.new(exit_code,message)
end
end
+
+ # Exit the program as if the user messed up the command-line invocation, providing
+ # them the message as well as printing the help. This is useful if
+ # you have complex UI validation that can't be done by OptionParser.
+ def help_now!(message)
+ raise OptionParser::ParseError.new(message)
+ end
end
end
View
@@ -341,6 +341,8 @@ def call_main
raise ex if ENV['DEBUG']
logger.error ex.message unless no_message? ex
ex.exit_code
+ rescue OptionParser::ParseError
+ raise
rescue => ex
raise ex if ENV['DEBUG']
raise ex if @leak_exceptions
View
@@ -209,6 +209,26 @@ def teardown
}
end
+ test_that "when we help_now! we exit and show help" do
+ Given {
+ @message = any_sentence
+ main do
+ help_now!(@message)
+ end
+
+ opts.on("--switch") { options[:switch] = true }
+ opts.on("--flag FLAG") { |value| options[:flag] = value }
+
+ set_argv []
+ }
+
+ Then {
+ assert_exits(64) { When run_go! }
+ assert $stdout.string.include?(opts.to_s),"Expected #{$stdout.string} to contain #{opts.to_s}"
+ assert_logged_at_error @message
+ }
+ end
+
test_that "opts allows us to more expediently set up OptionParser" do
Given {
@switch = nil

0 comments on commit 7263ace

Please sign in to comment.