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
workflow_manager-0.5.0 lib/workflow_manager/optparse_ex.rb
sushi_fabric-0.9.5 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.4.8 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.4.7 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.4.6 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.4.5 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.4.4 lib/workflow_manager/optparse_ex.rb
workflow_manager-0.4.3 lib/workflow_manager/optparse_ex.rb
sushi_fabric-0.9.4 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.9.3 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.4.2 lib/workflow_manager/optparse_ex.rb
sushi_fabric-0.9.2 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.9.1 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.9.0 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.8.9 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.8.8 lib/sushi_fabric/optparse_ex.rb
sushi_fabric-0.8.7 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.4.1 lib/workflow_manager/optparse_ex.rb
sushi_fabric-0.8.6 lib/sushi_fabric/optparse_ex.rb
workflow_manager-0.4.0 lib/workflow_manager/optparse_ex.rb