diff options
author | Nobuyoshi Nakada <[email protected]> | 2025-03-10 13:12:34 +0900 |
---|---|---|
committer | git <[email protected]> | 2025-03-10 09:55:29 +0000 |
commit | 9e265b583b5cc35b8120978c855e1a6a78abbe5b () | |
tree | 5292f87b227d3c99eb3563d8a283171721104329 /lib/optparse.rb | |
parent | b51450f3bd77f6dc862fcb8b177922a588f0e381 (diff) |
[ruby/optparse] Add post-check of value
Fix https://.com/ruby/optparse/pull/80 https://.com/ruby/optparse/commit/050a87d029
-rw-r--r-- | lib/optparse.rb | 38 |
1 files changed, 30 insertions, 8 deletions
@@ -461,6 +461,10 @@ class OptionParser candidates end def candidate(key, icase = false, pat = nil, &_) Completion.candidate(key, icase, pat, &method(:each)) end @@ -544,11 +548,11 @@ class OptionParser def initialize(pattern = nil, conv = nil, short = nil, long = nil, arg = nil, - desc = ([] if short or long), block = nil, &_block) raise if Array === pattern block ||= _block - @pattern, @conv, @short, @long, @arg, @desc, @block = - pattern, conv, short, long, arg, desc, block end # @@ -581,11 +585,15 @@ class OptionParser # exception. # def conv_arg(arg, val = []) # :nodoc: if conv val = conv.call(*val) else val = proc {|v| v}.call(*val) end return arg, block, val end private :conv_arg @@ -780,7 +788,7 @@ class OptionParser # Returns nil if argument is not present or begins with '-' and is not '-'. # def parse(arg, argv, &error) - if !(val = arg) and !(val = argv[0])&.match?(/\A(?!-.)/) return nil, block end opt = (val = parse_arg(val, &error))[1] @@ -1464,6 +1472,7 @@ XXX klass = nil q, a = nil has_arg = false opts.each do |o| # argument class @@ -1477,7 +1486,7 @@ XXX end # directly specified pattern(any object possible to match) - if (!(String === o || Symbol === o)) and o.respond_to?(:match) pattern = notwice(o, pattern, 'pattern') if pattern.respond_to?(:convert) conv = pattern.method(:convert).to_proc @@ -1492,6 +1501,11 @@ XXX when Proc, Method block = notwice(o, block, 'block') when Array, Hash case pattern when CompletingHash when nil @@ -1500,7 +1514,9 @@ XXX else raise ArgumentError, "argument pattern given twice" end - o.each {|pat, *v| pattern[pat.to_s] = v.fetch(0) {pat}} when Module raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4)) when *ArgumentStyle.keys @@ -1568,12 +1584,18 @@ XXX end default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern if !(short.empty? and long.empty?) if has_arg and default_style == Switch::NoArgument default_style = Switch::RequiredArgument end s = (style || default_style).new(pattern || default_pattern, - conv, sdesc, ldesc, arg, desc, block) elsif !block if style or pattern raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller) @@ -1582,7 +1604,7 @@ XXX else short << pattern s = (style || default_style).new(pattern, - conv, nil, nil, arg, desc, block) end return s, short, long, (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style), |