class Sawarineko::Option

Handle command line options.

Public Class Methods

new() click to toggle source

Initialize a Option.

# File lib/sawarineko/option.rb, line 9
def initialize
  @options = { encoding: Encoding::UTF_8 }
end

Public Instance Methods

parse(args) click to toggle source

Parse the passed arguments to a Hash.

args - An Array of Strings containing options.

Returns an Array contains of a Hash options and an Array of Strings

remaining arguments.
# File lib/sawarineko/option.rb, line 19
def parse(args)
  OptionParser.new do |opts|
    opts.banner = 'Usage: sawarineko [options] [source]'

    add_options(opts)
    add_options_on_tail(opts)
  end.parse!(args)
  [@options, args]
end

Private Instance Methods

add_options(opts) click to toggle source

Add command line options.

opts - An OptionParser object to add options.

Returns nothing.

# File lib/sawarineko/option.rb, line 36
def add_options(opts)
  opts.on('-e', '--encoding ENCODING',
          'Specify the encoding of input.') do |encoding|
    @options[:encoding] = encoding
  end
end
add_options_on_tail(opts) click to toggle source

Add command line options printed at tail.

opts - An OptionParser object to add options.

Returns nothing.

# File lib/sawarineko/option.rb, line 48
def add_options_on_tail(opts)
  opts.on_tail('-h', '--help', 'Print this message.') do
    puts opts
    exit 0
  end

  opts.on_tail('-v', '--version', 'Print version.') do
    puts Version::STRING
    exit 0
  end
end