summaryrefslogtreecommitdiff
path: root/test/optparse
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2019-10-16 16:31:41 +0900
committerYusuke Endoh <mame@ruby-lang.org>2019-10-18 12:20:58 +0900
commitc3b64a86bcd7773c081f5049115c57ec73d7a76a (patch)
treeeb4cc5141106c3da19624a1efb3f8db02dc7fd87 /test/optparse
parent5ca5529d223ee962451685badc9e928ea87dbb44 (diff)
lib/optparse.rb: Show a did_you_mean hint for unknown option
``` require 'optparse' OptionParser.new do |opts| opts.on("-f", "--foo", "foo") {|v| } opts.on("-b", "--bar", "bar") {|v| } opts.on("-c", "--baz", "baz") {|v| } end.parse! ``` ``` $ ruby test.rb --baa Traceback (most recent call last): test.rb:7:in `<main>': invalid option: --baa (OptionParser::InvalidOption) Did you mean? baz bar ```
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2561
Diffstat (limited to 'test/optparse')
-rw-r--r--test/optparse/test_did_you_mean.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/test/optparse/test_did_you_mean.rb b/test/optparse/test_did_you_mean.rb
new file mode 100644
index 0000000..4d8e272
--- /dev/null
+++ b/test/optparse/test_did_you_mean.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: false
+require_relative 'test_optparse'
+require "did_you_mean" rescue return
+
+class TestOptionParser::DidYouMean < TestOptionParser
+ def setup
+ super
+ @opt.def_option("--foo", Integer) { |v| @foo = v }
+ @opt.def_option("--bar", Integer) { |v| @bar = v }
+ @opt.def_option("--baz", Integer) { |v| @baz = v }
+ end
+
+ def test_did_you_mean
+ assert_raise(OptionParser::InvalidOption) do
+ begin
+ @opt.permute!(%w"--baa")
+ ensure
+ assert_equal("invalid option: --baa\nDid you mean? baz\n bar", $!.message)
+ end
+ end
+ end
+end