mirror of
https://github.com/ruby/ruby.git
synced 2025-08-23 13:04:13 +02:00
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 ```
This commit is contained in:
parent
5ca5529d22
commit
c3b64a86bc
Notes:
git
2019-10-18 12:21:20 +09:00
2 changed files with 38 additions and 2 deletions
22
test/optparse/test_did_you_mean.rb
Normal file
22
test/optparse/test_did_you_mean.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue