Class: Line::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/line/options_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ OptionParser

Returns a new instance of OptionParser.



10
11
12
# File 'lib/line/options_parser.rb', line 10

def initialize(arguments)
  @arguments = arguments
end

Class Method Details

.call(*args) ⇒ Object



6
7
8
# File 'lib/line/options_parser.rb', line 6

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/line/options_parser.rb', line 14

def call
  return @options if @options
  self.options = Options.new

  @arguments.each do |arg|
    case arg
    when '-l', '--line-numbers' then options.line_numbers = true
    when '-h', '--help'         then options.show_help    = true
    when '-s', '--strip'        then options.strip        = true
    when '-f', '--force'        then options.force        = true
    when '-c', '--chomp'        then options.chomp        = true
    when '-d', '--debug'        then options.debug        = true
    when '0'                    then invalid_args << arg
    # range
    when /\A-?\d+..-?\d+/
      lower, upper = arg.split('..').map(&:to_i)
      options.indexes   << lower << upper
      positive_matchers << Matchers::Range.new(lower, upper)
    # index
    when /\A-?\d+\Z/
      index = arg.to_i
      options.indexes   << index
      positive_matchers << Matchers::Index.new(index)
    # negated index
    when /\A\^-?\d+\Z/
      index = arg[1..-1].to_i
      options.indexes   << index
      negative_matchers << Matchers::Not.new(Matchers::Index.new(index))
    when /\A\^-?\d+..-?\d+\Z/
      lower, upper = arg[1..-1].split('..').map(&:to_i)
      options.indexes   << lower << upper
      negative_matchers << Matchers::Not.new(Matchers::Range.new(lower, upper))
    else
      invalid_args << arg
    end
  end

  options.errors[:line_numbers] = error_msg_for_line_numbers if error_msg_for_line_numbers
  options.line_matcher          = consolidate_args_to_line_matcher
  options.buffer_size           = [0, *options.indexes].min.abs
  options.help_screen           = help_screen
  options
end