Module: Options

Defined in:
lib/options.rb

Constant Summary collapse

VERSION =
'2.3.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



164
165
166
# File 'lib/options.rb', line 164

def arguments
  @arguments
end

Class Method Details

.descriptionObject



9
10
11
# File 'lib/options.rb', line 9

def description
  'parse options from *args cleanly'
end

.for(hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/options.rb', line 35

def for(hash)
  hash =
    case hash
      when Hash
        hash
      when Array
        Hash[*hash.flatten]
      when String, Symbol
        {hash => true}
      else
        hash.to_hash
    end
  normalize!(hash)
ensure
  hash.extend(Options) unless hash.is_a?(Options)
end

.normalize(hash) ⇒ Object



19
20
21
# File 'lib/options.rb', line 19

def normalize(hash)
  normalize!(hash.dup)
end

.normalize!(hash) ⇒ Object



13
14
15
16
# File 'lib/options.rb', line 13

def normalize!(hash)
  hash.keys.each{|key| hash[key.to_s.to_sym] = hash.delete(key) unless Symbol===key}
  hash
end

.parse(args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/options.rb', line 52

def parse(args)
  case args
  when Array
    args.extend(Arguments) unless args.is_a?(Arguments)
    [args, args.options.pop]
  when Hash
    Options.for(args)
  else
    raise ArgumentError, "`args` should be an Array or Hash"
  end
end

.stringifiedObject



33
34
35
# File 'lib/options.rb', line 33

def stringify(hash)
  stringify!(hash)
end

.stringified!Object



28
29
30
31
# File 'lib/options.rb', line 28

def stringify!(hash)
  hash.keys.each{|key| hash[key.to_s] = hash.delete(key) unless String===key}
  hash
end

.stringify(hash) ⇒ Object



30
31
32
# File 'lib/options.rb', line 30

def stringify(hash)
  stringify!(hash)
end

.stringify!(hash) ⇒ Object



24
25
26
27
# File 'lib/options.rb', line 24

def stringify!(hash)
  hash.keys.each{|key| hash[key.to_s] = hash.delete(key) unless String===key}
  hash
end

.to_optionsObject



22
23
24
# File 'lib/options.rb', line 22

def normalize(hash)
  normalize!(hash.dup)
end

.to_options!Object



17
18
19
20
# File 'lib/options.rb', line 17

def normalize!(hash)
  hash.keys.each{|key| hash[key.to_s.to_sym] = hash.delete(key) unless Symbol===key}
  hash
end

.versionObject



5
6
7
# File 'lib/options.rb', line 5

def version
  Options::VERSION
end

Instance Method Details

#delopt(key, default = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/options.rb', line 105

def delopt key, default = nil
  [ key ].flatten.each do |key|
    return delete(key) if has_key?(key)
    key = key.to_s
    return delete(key) if has_key?(key)
    key = key.to_sym
    return delete(key) if has_key?(key)
  end
  default
end

#delopts(*args) ⇒ Object



116
117
118
# File 'lib/options.rb', line 116

def delopts *args
  args.flatten.map{|arg| delopt arg}
end

#getopt(key, default = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/options.rb', line 73

def getopt key, default = nil
  [ key ].flatten.each do |key|
    return fetch(key) if has_key?(key)
    key = key.to_s
    return fetch(key) if has_key?(key)
    key = key.to_sym
    return fetch(key) if has_key?(key)
  end
  default
end

#getopts(*args) ⇒ Object



84
85
86
# File 'lib/options.rb', line 84

def getopts *args
  args.flatten.map{|arg| getopt arg}
end

#hasopt(key, default = nil) ⇒ Object Also known as: hasopt?



88
89
90
91
92
93
94
95
96
97
# File 'lib/options.rb', line 88

def hasopt key, default = nil
  [ key ].flatten.each do |key|
    return true if has_key?(key)
    key = key.to_s
    return true if has_key?(key)
    key = key.to_sym
    return true if has_key?(key)
  end
  default
end

#hasopts(*args) ⇒ Object Also known as: hasopts?



100
101
102
# File 'lib/options.rb', line 100

def hasopts *args
  args.flatten.map{|arg| hasopt arg}
end

#normalizeObject Also known as: normalized, to_options



148
149
150
# File 'lib/options.rb', line 148

def normalize
  Options.normalize(self)
end

#normalize!Object Also known as: normalized!, to_options!



142
143
144
# File 'lib/options.rb', line 142

def normalize!
  Options.normalize!(self)
end

#popObject



165
166
167
168
# File 'lib/options.rb', line 165

def pop
  pop! unless popped?
  self
end

#pop!Object



174
175
176
177
178
179
180
# File 'lib/options.rb', line 174

def pop!
  if arguments.last.is_a?(Hash)
    @popped = arguments.pop
  else
    @popped = true
  end
end

#popped?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/options.rb', line 170

def popped?
  defined?(@popped) and @popped
end

#select!(*a, &b) ⇒ Object



138
139
140
# File 'lib/options.rb', line 138

def select! *a, &b
  replace select(*a, &b).to_hash
end

#setopt(key, value = nil) ⇒ Object Also known as: setopt!



120
121
122
123
124
125
126
127
128
129
# File 'lib/options.rb', line 120

def setopt key, value = nil
  [ key ].flatten.each do |key|
    return self[key]=value if has_key?(key)
    key = key.to_s
    return self[key]=value if has_key?(key)
    key = key.to_sym
    return self[key]=value if has_key?(key)
  end
  return self[key]=value
end

#setopts(opts) ⇒ Object Also known as: setopts!



132
133
134
135
# File 'lib/options.rb', line 132

def setopts opts 
  opts.each{|key, value| setopt key, value}
  opts
end

#stringifyObject Also known as: stringified



159
160
161
# File 'lib/options.rb', line 159

def stringify
  Options.stringify(self)
end

#stringify!Object Also known as: stringified!



154
155
156
# File 'lib/options.rb', line 154

def stringify!
  Options.stringify!(self)
end

#validate(*acceptable_options) ⇒ Object

Validates that the options provided are acceptable.

Parameters:

  • *acceptable_options (Symbol)

    List of options that are allowed

Raises:

  • (ArgumentError)


186
187
188
189
190
191
# File 'lib/options.rb', line 186

def validate(*acceptable_options)
  remaining = (provided_options - acceptable_options).map{|opt| opt.to_s}.sort
  raise ArgumentError, "Unrecognized options: #{remaining.join(', ')}" unless remaining.empty?
  
  self
end