Module: OptionParser::Arguable

Defined in:
lib/optparse.rb

Overview

Extends command line arguments array (ARGV) to parse itself.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object

Initializes instance variable.


2210
2211
2212
2213
# File 'lib/optparse.rb', line 2210

def self.extend_object(obj)
  super
  obj.instance_eval {@optparse = nil}
end

Instance Method Details

#getopts(*args) ⇒ Object

Substitution of getopts is possible as follows. Also see OptionParser#getopts.

def getopts(*args)
  ($OPT = ARGV.getopts(*args)).each do |opt, val|
    eval "$OPT_#{opt.gsub(/[^A-Za-z0-9_]/, '_')} = val"
  end
rescue OptionParser::ParseError
end

2203
2204
2205
# File 'lib/optparse.rb', line 2203

def getopts(*args)
  options.getopts(self, *args)
end

#initialize(*args) ⇒ Object


2214
2215
2216
2217
# File 'lib/optparse.rb', line 2214

def initialize(*args)
  super
  @optparse = nil
end

#optionsObject

Actual OptionParser object, automatically created if nonexistent.

If called with a block, yields the OptionParser object and returns the result of the block. If an OptionParser::ParseError exception occurs in the block, it is rescued, a error message printed to STDERR and nil returned.


2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
# File 'lib/optparse.rb', line 2162

def options
  @optparse ||= OptionParser.new
  @optparse.default_argv = self
  block_given? or return @optparse
  begin
    yield @optparse
  rescue ParseError
    @optparse.warn $!
    nil
  end
end

#options=(opt) ⇒ Object

Sets OptionParser object, when opt is false or nil, methods OptionParser::Arguable#options and OptionParser::Arguable#options= are undefined. Thus, there is no ways to access the OptionParser object via the receiver object.


2145
2146
2147
2148
2149
2150
2151
2152
# File 'lib/optparse.rb', line 2145

def options=(opt)
  unless @optparse = opt
    class << self
      undef_method(:options)
      undef_method(:options=)
    end
  end
end

#order!(&blk) ⇒ Object

Parses self destructively in order and returns self containing the rest arguments left unparsed.


2178
# File 'lib/optparse.rb', line 2178

def order!(&blk) options.order!(self, &blk) end

#parse!Object

Parses self destructively and returns self containing the rest arguments left unparsed.


2190
# File 'lib/optparse.rb', line 2190

def parse!() options.parse!(self) end

#permute!Object

Parses self destructively in permutation mode and returns self containing the rest arguments left unparsed.


2184
# File 'lib/optparse.rb', line 2184

def permute!() options.permute!(self) end