class ImageOptim::Runner::OptionParser

Parse options from arguments to image_optim binary

Constants

DEFINE

Public Class Methods

new(options) click to toggle source

After initialization passes self and options to DEFINE

Calls superclass method
# File lib/image_optim/runner/option_parser.rb, line 27
def initialize(options)
  super
  DEFINE.call(self, options)
end
parse!(args) click to toggle source

Parse and remove options from args, return options Hash Calls abort in case of parse error

# File lib/image_optim/runner/option_parser.rb, line 14
def self.parse!(args)
  # assume -v to be a request to print version if it is the only argument
  args = %w[--version] if args == %w[-v]

  options = {}
  parser = new(options)
  parser.parse!(args)
  options
rescue OptionParser::ParseError => e
  abort "#{e}\n\n#{parser.help}"
end

Public Instance Methods

help() click to toggle source

Wraps and indents lines of overriden method

Calls superclass method
# File lib/image_optim/runner/option_parser.rb, line 33
def help
  text = super

  # reserve one column
  columns = terminal_columns - 1
  # 1 for distance between summary and description
  # 2 for additional indent
  wrapped_indent = summary_indent + ' ' * (summary_width + 1 + 2)
  wrapped_width = columns - wrapped_indent.length
  # don't try to wrap if there is too little space for description
  return text if wrapped_width < 20

  wrapped = ''
  text.split("\n").each do |line|
    if line.length <= columns
      wrapped << line << "\n"
    else
      indented = line =~ /^\s/
      wrapped << line.slice!(wrap_regex(columns)) << "\n"
      line.scan(wrap_regex(wrapped_width)) do |part|
        wrapped << wrapped_indent if indented
        wrapped << part << "\n"
      end
    end
  end
  wrapped
end

Private Instance Methods

terminal_columns() click to toggle source
# File lib/image_optim/runner/option_parser.rb, line 63
def terminal_columns
  stty_columns = `stty size 2> /dev/null`[/^\d+ (\d+)$/, 1]
  stty_columns ? stty_columns.to_i : `tput cols`.to_i
end
wrap_regex(width) click to toggle source
# File lib/image_optim/runner/option_parser.rb, line 68
def wrap_regex(width)
  /.*?.{1,#{width}}(?:\s|\z)/
end