Class: EverydayCliUtils::OptionList
- Inherits:
-
Object
- Object
- EverydayCliUtils::OptionList
- Defined in:
- lib/everyday-cli-utils/option.rb
Instance Attribute Summary collapse
-
#default_settings ⇒ Object
Returns the value of attribute default_settings.
-
#help_str ⇒ Object
Returns the value of attribute help_str.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
- #[]=(opt_name, opt) ⇒ Object
- #banner=(banner) ⇒ Object
- #composite(*layers) ⇒ Object
- #hash_diff(hash1, hash2) ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ OptionList
constructor
A new instance of OptionList.
- #options_to_str(options) ⇒ Object
- #parse!(argv = ARGV) ⇒ Object
- #register(type, opt_name, names, settings = {}, &block) ⇒ Object
- #set(opt_name, value) ⇒ Object
- #show_defaults ⇒ Object
- #to_s ⇒ Object
- #update(opt_name, value, layer) ⇒ Object
- #val_to_str(val) ⇒ Object
Constructor Details
#initialize ⇒ OptionList
Returns a new instance of OptionList.
147 148 149 150 151 152 |
# File 'lib/everyday-cli-utils/option.rb', line 147 def initialize @options = {} @default_settings = {} @opts = OptionParser.new @help_str = nil end |
Instance Attribute Details
#default_settings ⇒ Object
Returns the value of attribute default_settings.
145 146 147 |
# File 'lib/everyday-cli-utils/option.rb', line 145 def default_settings @default_settings end |
#help_str ⇒ Object
Returns the value of attribute help_str.
145 146 147 |
# File 'lib/everyday-cli-utils/option.rb', line 145 def help_str @help_str end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
144 145 146 |
# File 'lib/everyday-cli-utils/option.rb', line 144 def opts @opts end |
Instance Method Details
#[]=(opt_name, opt) ⇒ Object
154 155 156 |
# File 'lib/everyday-cli-utils/option.rb', line 154 def []=(opt_name, opt) @options[opt_name] = opt end |
#banner=(banner) ⇒ Object
184 185 186 |
# File 'lib/everyday-cli-utils/option.rb', line 184 def () @opts. = end |
#composite(*layers) ⇒ Object
170 171 172 173 174 |
# File 'lib/everyday-cli-utils/option.rb', line 170 def composite(*layers) hash = {} @options.each { |v| hash[v[0]] = v[1].composite(*layers) } hash end |
#hash_diff(hash1, hash2) ⇒ Object
204 205 206 207 208 |
# File 'lib/everyday-cli-utils/option.rb', line 204 def hash_diff(hash1, hash2) new_hash = {} hash1.keys.each { |k| new_hash[k] = hash1[k] unless hash2.has_key?(k) && hash1[k] == hash2[k] } new_hash end |
#help ⇒ Object
176 177 178 |
# File 'lib/everyday-cli-utils/option.rb', line 176 def help @help_str.nil? ? @opts.help : @help_str end |
#options_to_str(options) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/everyday-cli-utils/option.rb', line 210 def () str = '' max_name_len = @options.values.map { |v| v.names.join(', ').length }.max .each { |v| opt = @options[v[0]] val = v[1] names_str = opt.names.join(', ') str << "#{' ' * 4}#{names_str}#{' ' * ((max_name_len + 4) - names_str.length)}#{val_to_str(val)}\n" } str end |
#parse!(argv = ARGV) ⇒ Object
188 189 190 |
# File 'lib/everyday-cli-utils/option.rb', line 188 def parse!(argv = ARGV) @opts.parse!(argv) end |
#register(type, opt_name, names, settings = {}, &block) ⇒ Object
166 167 168 |
# File 'lib/everyday-cli-utils/option.rb', line 166 def register(type, opt_name, names, settings = {}, &block) OptionDef.register(@opts, @options, type, opt_name, names, settings, @default_settings, &block) end |
#set(opt_name, value) ⇒ Object
158 159 160 |
# File 'lib/everyday-cli-utils/option.rb', line 158 def set(opt_name, value) @options[opt_name].set(value) if @options.has_key?(opt_name) end |
#show_defaults ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/everyday-cli-utils/option.rb', line 192 def show_defaults script_defaults = composite global_defaults = composite(:global) local_defaults = composite(:global, :local) global_diff = hash_diff(global_defaults, script_defaults) local_diff = hash_diff(local_defaults, global_defaults) str = "Script Defaults:\n#{(script_defaults)}\n" str << "Script + Global Defaults:\n#{(global_diff)}\n" unless global_diff.empty? str << "Script + Global + Local Defaults:\n#{(local_diff)}\n" unless local_diff.empty? str end |
#to_s ⇒ Object
180 181 182 |
# File 'lib/everyday-cli-utils/option.rb', line 180 def to_s @help_str.nil? ? @opts.to_s : @help_str end |
#update(opt_name, value, layer) ⇒ Object
162 163 164 |
# File 'lib/everyday-cli-utils/option.rb', line 162 def update(opt_name, value, layer) @options[opt_name].update(value, layer) if @options.has_key?(opt_name) end |
#val_to_str(val) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/everyday-cli-utils/option.rb', line 222 def val_to_str(val) if val.nil? 'nil' elsif val.is_a?(TrueClass) 'true' elsif val.is_a?(FalseClass) 'false' elsif val.is_a?(Enumerable) "[#{val.map { |v| val_to_str(v) }.join(', ')}]" elsif val.is_a?(Numeric) val.to_s else "'#{val.to_s}'" end end |