Permalink
Browse files

Gracefully handle unknown options.

  • Loading branch information...
1 parent eb38810 commit 35cae08692633c0dae0db0bcbfcfd63e925224da @xaviershay xaviershay committed Aug 20, 2012
Showing with 15 additions and 4 deletions.
  1. +2 −2 lib/cane/cli.rb
  2. +6 −2 lib/cane/cli/spec.rb
  3. +7 −0 spec/cane_spec.rb
View
@@ -7,10 +7,10 @@ module Cane
module CLI
def run(args)
opts = Spec.new.parse(args)
- if opts
+ if opts.is_a?(Hash)
Cane.run(opts, Spec::CHECKS)
else
- true
+ opts
end
end
module_function :run
View
@@ -42,12 +42,16 @@ def initialize
add_help
end
- def parse(args)
+ def parse(args, ret = true)
parser.parse!(get_default_options + args)
OPTIONS.merge(options)
+ rescue OptionParser::InvalidOption
+ args = %w(--help)
+ ret = false
+ retry
rescue OptionsHandled
- nil
+ ret
end
def get_default_options
View
@@ -162,4 +162,11 @@ def complex_method(a)
exitstatus.should == 0
end
+
+ it 'handles invalid options by showing help' do
+ out, exitstatus = run("--bogus")
+
+ out.should include("Usage:")
+ exitstatus.should == 1
+ end
end

0 comments on commit 35cae08

Please sign in to comment.