Sha256: 0aab387b8fdd200c1692bccf7eaec9caa66156cef6cb2fb0ffa42253951b1379

Contents?: true

Size: 1.18 KB

Versions: 167

Compression:

Stored size: 1.18 KB

Contents

#!/usr/bin/env ruby
# encoding: utf-8

require 'optparse'
# Version = '20130517-120455'

class OptionParser
  attr_reader :p
  alias :_on :on
  def on(attr, *args, &block)
    if attr.is_a?(Symbol)
      self.class.class_eval do
        unless method_defined?(attr)
          attr_accessor attr 
        else
          raise "Method #{attr.to_s} is already defined in OptionParser class"
        end
      end
      unless args[0] =~ /\-/
        default = args.shift
        self.send(attr.to_s+"=", default)
      end
      _on(*args) do |i|
        self.send(attr.to_s+"=", i)
        block.call(i) if block
      end
    else
      args.unshift attr
      _on(*args, block)
    end
  end
end

if __FILE__ == $0
  opt = OptionParser.new do |o|
    o.banner = "Last update: #{o.version}\nUsage: ruby #{__FILE__} [options]"
    o.on(:size, 100, '-N N', '--pop_size', Integer, 'population size (default: 100)'){|i| p i}
    o.on(:seed, '-R R', '--rseed', 'random seed')
    o.on(:flag, '-f', 'flag')
    o.on('-M M', '--hoge', Integer, 'hoge'){|i| p i}
    o.parse!(ARGV)
  end

  print "opt.flag = "
  p opt.flag
  print "opt.size = "
  p opt.size
  print "opt.seed = "
  p opt.seed
  puts
  puts opt.help
end

Version data entries

167 entries across 167 versions & 2 rubygems

Version Path
sushi_fabric-1.3.1 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.3.0 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.9 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.8 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.7 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.6 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.5 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.9.1 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.9.0 lib/workflow_manager/optparse_ex.rb
sushi_fabric-1.2.4 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.8.1 lib/workflow_manager/optparse_ex.rb
sushi_fabric-1.2.3 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.2 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.1 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.2.0 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.1.9 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.8.0 lib/workflow_manager/optparse_ex.rb
sushi_fabric-1.1.8 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.1.7 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-1.1.6 lib/sushi_fabric/optparse_ex.rb