Class: Gem::Portage::Command::Option
Overview
class Command
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#opt_id ⇒ Object
readonly
Returns the value of attribute opt_id.
Instance Method Summary collapse
- #arg_name ⇒ Object
- #boolean? ⇒ Boolean
- #default ⇒ Object
- #has_arg? ⇒ Boolean
-
#initialize(sym, *optparse, &handler) ⇒ Option
constructor
A new instance of Option.
- #long ⇒ Object
- #module ⇒ Object
- #optparse_args ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(sym, *optparse, &handler) ⇒ Option
Returns a new instance of Option.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/g-gem/command.rb', line 62 def initialize(sym, *optparse, &handler) /[?=]?$/ === sym.to_s @id = $`.to_sym @opt_id = $`.to_sym # $`.gsub('_','-').to_sym @type = $&[0] if $& @opts = Hash === optparse.last ? optparse.pop : {} @optparse_args = optparse long @handler ||= lambda {|v, h| h[@opt_id] = v} end |
Instance Attribute Details
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
73 74 75 |
# File 'lib/g-gem/command.rb', line 73 def handler @handler end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
73 74 75 |
# File 'lib/g-gem/command.rb', line 73 def id @id end |
#opt_id ⇒ Object (readonly)
Returns the value of attribute opt_id.
73 74 75 |
# File 'lib/g-gem/command.rb', line 73 def opt_id @opt_id end |
Instance Method Details
#arg_name ⇒ Object
82 83 84 |
# File 'lib/g-gem/command.rb', line 82 def arg_name @opts[:arg_name] end |
#boolean? ⇒ Boolean
74 75 76 |
# File 'lib/g-gem/command.rb', line 74 def boolean? @type == ?? || default == true || default == false || !@opts[:boolean].nil? end |
#default ⇒ Object
86 87 88 89 90 91 |
# File 'lib/g-gem/command.rb', line 86 def default return @default if not @default.nil? def_name = @opts.keys.detect { |k| /^default(_(\S+))?/ === k.to_s } @opts[:arg_name] = $2.upcase if $2 && arg_name.nil? @default = @opts[def_name] end |
#has_arg? ⇒ Boolean
78 79 80 |
# File 'lib/g-gem/command.rb', line 78 def has_arg? @type == ?= || !arg_name.nil? end |
#long ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/g-gem/command.rb', line 93 def long return @long if @long re = /^--(\[no-\])?(\S+)(\s+\[?([^\]\s]+))?/ long = @optparse_args.detect(@opts[:long]) { |a| re === a } if long.to_s =~ re @opt_id = $2.to_sym @opts[:boolean] = true if $1 @opts[:arg_name] = $4 if $4 @long = long else @long = "--" @long << "[no-]" if boolean? @long << @opt_id.to_s @long << " "+arg_name.to_s if has_arg? @long end end |
#module ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/g-gem/command.rb', line 127 def module src = <<-READER def #{id} options["#{opt_id}".intern] end READER src << <<-QUESTION if boolean? def #{id}? return false if options["#{opt_id}".intern] == false not options["#{opt_id}".intern].nil? end QUESTION src << <<-WRITER if has_arg? def #{id}=(value) options["#{opt_id}".intern] = value end WRITER Module.new do module_eval src end end |
#optparse_args ⇒ Object
111 112 113 |
# File 'lib/g-gem/command.rb', line 111 def optparse_args ([long] + @optparse_args).uniq.reject { |o| o.nil? } end |
#to_s ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/g-gem/command.rb', line 115 def to_s str = "--" str << "no-" if boolean? and not default str << opt_id.to_s str << case default when TrueClass, FalseClass; "" when Array; " "+default.join(",").inspect when String; " "+default.inspect end str end |