Class: PrimoCentralCounter::OptionParser

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/primo_central_counter/option_parser.rb

Overview

This class wraps the original OptionParser to “repair” some bad behaviour.

Instance Method Summary collapse

Instance Method Details

#add_officiousObject

Usually, OptionParser adds default handlers for help, version, etc. This is a undocumented behavoir and some cases not intended, because the default handler also stops execution immediately. Thatswhy we remove some of them.



9
10
11
12
# File 'lib/primo_central_counter/option_parser.rb', line 9

def add_officious
  super
  base.long.reject! { |_key, _| _key == "help" || _key == "version" }
end

#parse!(argv) ⇒ Object

The original option parser stops processing if it sees any options it does not know. This implementation uses order!, which is a lower level OptionParser method to get the requested behavior. Because #parse uses #parse! internally, this automatically fixes #parse.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/primo_central_counter/option_parser.rb', line 18

def parse!(argv)
  unprocessed_options = []

  begin
    order!(argv) do |_non_option|
      unprocessed_options << _non_option
    end
  rescue InvalidOption => e
    unprocessed_options << e.args.first
    retry
  end
  .tap do
    argv.concat(unprocessed_options)
  end
end