Project

General

Profile

Feature #8460 ยป 0001-optparse-add-keep_unknown-option.patch

felipec (Felipe Contreras), 05/27/2021 10:07 PM

View differences:

lib/optparse.rb
1092 1092
    @summary_indent = indent
1093 1093
    @default_argv = ARGV
1094 1094
    @require_exact = false
1095
    @keep_unknown = false
1095 1096
    add_officious
1096 1097
    yield self if block_given?
1097 1098
  end
......
1169 1170
  # abbreviated long option as short option).
1170 1171
  attr_accessor :require_exact
1171 1172

  
1173
  # Unknown arguments are left alone
1174
  attr_accessor :keep_unknown
1175

  
1172 1176
  #
1173 1177
  # Heading banner preceding summary.
1174 1178
  #
......
1562 1566
            if require_exact && !sw.long.include?(arg)
1563 1567
              raise InvalidOption, arg
1564 1568
            end
1565
          rescue ParseError
1566
            raise $!.set_option(arg, true)
1569
          rescue ParseError => e
1570
            raise $!.set_option(arg, true) unless e.is_a?(InvalidOption) and keep_unknown
1571
            nonopt.call(arg)
1572
            next
1567 1573
          end
1568 1574
          begin
1569 1575
            opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
......
1593 1599
                eq ||= !rest
1594 1600
              end
1595 1601
            end
1596
          rescue ParseError
1597
            raise $!.set_option(arg, true)
1602
          rescue ParseError => e
1603
            raise $!.set_option(arg, true) unless e.is_a?(InvalidOption) and keep_unknown
1604
            nonopt.call(arg)
1605
            next
1598 1606
          end
1599 1607
          begin
1600 1608
            opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
test/optparse/test_optparse.rb
105 105
    e = assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(-t))}
106 106
    assert_equal(["-t"], e.args)
107 107
  end
108

  
109
  def test_keep_unknown
110
    @opt.def_option('-x')
111
    assert_raise(OptionParser::InvalidOption) {@opt.parse(%w(--mis))}
112
    @opt.keep_unknown = true
113
    assert_equal(%w(--mis arg), @opt.parse(%w(--mis -x arg)))
114
  end
108 115
end