Class: Tweetwine::OptionParser

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

Overview

A wrapper for OptionParser in standard library that returns parsing results as a Hash.

Not threadsafe.

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ OptionParser

Returns a new instance of OptionParser.



11
12
13
14
15
16
# File 'lib/tweetwine/option_parser.rb', line 11

def initialize(&blk)
  @options = {}
  @parser = ::OptionParser.new do |parser|
    blk.call(parser, @options)
  end
end

Instance Method Details

#helpObject



27
28
29
# File 'lib/tweetwine/option_parser.rb', line 27

def help
  @parser.summarize.join.chomp
end

#parse(args = ARGV) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/tweetwine/option_parser.rb', line 18

def parse(args = ARGV)
  @parser.order! args
  result = @options.dup
  @options.clear
  result
rescue ::OptionParser::ParseError => e
  raise CommandLineError, e.message
end