Class: 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.
-
#special_options ⇒ Object
readonly
Returns the value of attribute special_options.
Instance Method Summary collapse
- #[]=(opt_name, opt) ⇒ Object
- #banner=(banner) ⇒ Object
- #composite(*layers) ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ OptionList
constructor
A new instance of OptionList.
- #options_to_str(options, indent = 4) ⇒ Object
- #parse!(argv = ARGV) ⇒ Object
- #register(type, opt_name, names, settings = {}, &block) ⇒ Object
- #register_special(order, opt_name, names, exit_on_action, print_on_exit_str, settings, action_block, pre_parse_block = nil) ⇒ Object
- #run_special ⇒ Object
- #run_special_pre_parse ⇒ Object
- #set(opt_name, value) ⇒ Object
- #set_all(opts) ⇒ Object
- #show_defaults ⇒ Object
- #to_s ⇒ Object
- #update(opt_name, value, layer) ⇒ Object
- #update_all(layer, opts) ⇒ Object
- #val_to_str(val) ⇒ Object
Constructor Details
#initialize ⇒ OptionList
Returns a new instance of OptionList.
185 186 187 188 189 190 191 |
# File 'lib/everyday-cli-utils/option.rb', line 185 def initialize @options = {} @special_options = {} @default_settings = {} @opts = OptionParser.new @help_str = nil end |
Instance Attribute Details
#default_settings ⇒ Object
Returns the value of attribute default_settings.
183 184 185 |
# File 'lib/everyday-cli-utils/option.rb', line 183 def default_settings @default_settings end |
#help_str ⇒ Object
Returns the value of attribute help_str.
183 184 185 |
# File 'lib/everyday-cli-utils/option.rb', line 183 def help_str @help_str end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
182 183 184 |
# File 'lib/everyday-cli-utils/option.rb', line 182 def opts @opts end |
#special_options ⇒ Object (readonly)
Returns the value of attribute special_options.
182 183 184 |
# File 'lib/everyday-cli-utils/option.rb', line 182 def @special_options end |
Instance Method Details
#[]=(opt_name, opt) ⇒ Object
193 194 195 |
# File 'lib/everyday-cli-utils/option.rb', line 193 def []=(opt_name, opt) @options[opt_name] = opt end |
#banner=(banner) ⇒ Object
243 244 245 |
# File 'lib/everyday-cli-utils/option.rb', line 243 def () @opts. = end |
#composite(*layers) ⇒ Object
229 230 231 232 233 |
# File 'lib/everyday-cli-utils/option.rb', line 229 def composite(*layers) hash = {} @options.each { |v| hash[v[0]] = v[1].composite(*layers) } hash end |
#help ⇒ Object
235 236 237 |
# File 'lib/everyday-cli-utils/option.rb', line 235 def help @help_str.nil? ? @opts.help : @help_str end |
#options_to_str(options, indent = 4) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/everyday-cli-utils/option.rb', line 263 def (, indent = 4) 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 << "#{' ' * indent}#{names_str}#{' ' * ((max_name_len + 4) - names_str.length)}#{val_to_str(val)}\n" } str end |
#parse!(argv = ARGV) ⇒ Object
247 248 249 |
# File 'lib/everyday-cli-utils/option.rb', line 247 def parse!(argv = ARGV) @opts.parse!(argv) end |
#register(type, opt_name, names, settings = {}, &block) ⇒ Object
213 214 215 |
# File 'lib/everyday-cli-utils/option.rb', line 213 def register(type, opt_name, names, settings = {}, &block) OptionDef.register(@opts, self, type, opt_name, names, settings, @default_settings, &block) end |
#register_special(order, opt_name, names, exit_on_action, print_on_exit_str, settings, action_block, pre_parse_block = nil) ⇒ Object
217 218 219 |
# File 'lib/everyday-cli-utils/option.rb', line 217 def register_special(order, opt_name, names, exit_on_action, print_on_exit_str, settings, action_block, pre_parse_block = nil) SpecialOptionDef.register(order, @opts, self, opt_name, names, exit_on_action, print_on_exit_str, settings, @default_settings, action_block, pre_parse_block) end |
#run_special ⇒ Object
221 222 223 |
# File 'lib/everyday-cli-utils/option.rb', line 221 def run_special @special_options.to_a.sort_by { |v| v[1].order }.each { |v| v[1].run(self) } end |
#run_special_pre_parse ⇒ Object
225 226 227 |
# File 'lib/everyday-cli-utils/option.rb', line 225 def run_special_pre_parse @special_options.to_a.sort_by { |v| v[1].order }.each { |v| v[1].run_pre_parse(self) } end |
#set(opt_name, value) ⇒ Object
197 198 199 |
# File 'lib/everyday-cli-utils/option.rb', line 197 def set(opt_name, value) @options[opt_name].set(value) if @options.has_key?(opt_name) end |
#set_all(opts) ⇒ Object
201 202 203 |
# File 'lib/everyday-cli-utils/option.rb', line 201 def set_all(opts) opts.each { |opt| set(opt[0], opt[1]) } end |
#show_defaults ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/everyday-cli-utils/option.rb', line 251 def show_defaults script_defaults = composite global_defaults = composite(:global) local_defaults = composite(:global, :local) global_diff = EverydayCliUtils::MapUtil.hash_diff(global_defaults, script_defaults) local_diff = EverydayCliUtils::MapUtil.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
239 240 241 |
# File 'lib/everyday-cli-utils/option.rb', line 239 def to_s @help_str.nil? ? @opts.to_s : @help_str end |
#update(opt_name, value, layer) ⇒ Object
205 206 207 |
# File 'lib/everyday-cli-utils/option.rb', line 205 def update(opt_name, value, layer) @options[opt_name].update(value, layer) if @options.has_key?(opt_name) end |
#update_all(layer, opts) ⇒ Object
209 210 211 |
# File 'lib/everyday-cli-utils/option.rb', line 209 def update_all(layer, opts) opts.each { |opt| update(opt[0], opt[1], layer) } end |
#val_to_str(val) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/everyday-cli-utils/option.rb', line 275 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 |