Class: Gem::Portage::Command::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/g-gem/command.rb

Overview

class Command

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#handlerObject (readonly)

Returns the value of attribute handler.



73
74
75
# File 'lib/g-gem/command.rb', line 73

def handler
  @handler
end

#idObject (readonly)

Returns the value of attribute id.



73
74
75
# File 'lib/g-gem/command.rb', line 73

def id
  @id
end

#opt_idObject (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_nameObject



82
83
84
# File 'lib/g-gem/command.rb', line 82

def arg_name
  @opts[:arg_name]
end

#boolean?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/g-gem/command.rb', line 74

def boolean?
  @type == ?? || default == true || default == false || !@opts[:boolean].nil?
end

#defaultObject



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

Returns:

  • (Boolean)


78
79
80
# File 'lib/g-gem/command.rb', line 78

def has_arg?
  @type == ?= || !arg_name.nil?
end

#longObject



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

#moduleObject



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_argsObject



111
112
113
# File 'lib/g-gem/command.rb', line 111

def optparse_args
  ([long] + @optparse_args).uniq.reject { |o| o.nil? }
end

#to_sObject



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