Method: Launchr::Mixlib::CLI#initialize

Defined in:
lib/launchr/mixin/mixlib_cli.rb

#initialize(*args) ⇒ Object

Create a new Mixlib::CLI class. If you override this, make sure you call super!

Parameters

*args<Array>

The array of arguments passed to the initializer

Returns

object<Mixlib::Config>

Returns an instance of whatever you wanted :)

Raises:

  • (ArgumentError)


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/launchr/mixin/mixlib_cli.rb', line 289

def initialize(*args)
  @options   = Launchr::OrderedHash.new
  @arguments = Launchr::OrderedHash.new
  @options_arguments = Launchr::OrderedHash.new
  @config  = Hash.new
  @filtered_argv = []

  # Set the banner
  @banner  = self.class.banner
  @header  = self.class.header
  @footer  = self.class.footer

  @summary_indent  = self.class.summary_indent
  @summary_width   = self.class.summary_width
  @spaced_summary  = self.class.spaced_summary

  # Dupe the class options for this instance
  klass_options = self.class.options
  klass_options.keys.inject(@options) { |memo, key| memo[key] = klass_options[key].dup; memo }

  # Dupe the class arguments for this instance
  klass_arguments = self.class.arguments
  klass_arguments.keys.inject(@arguments) { |memo, key| memo[key] = klass_arguments[key].dup; memo }

  # Dupe the class arguments for this instance
  klass_options_arguments = self.class.options_arguments
  klass_options_arguments.keys.inject(@options_arguments) { |memo, key| memo[key] = klass_options_arguments[key].dup; memo }

  # check argument and option :name keys dont conflict
  name_collision = klass_options.keys & klass_arguments.keys
  raise ArgumentError, "An option cannot have the same name as an argument: #{name_collision.join(', ')}" unless name_collision.empty?

  koso, kolo = [], []
  klass_options.each do |name, kargs|
    koso << (kargs[:short_strip] || "")
    kolo << (kargs[:long_strip] || "")
  end
  
  kasa, kala = [], []
  klass_arguments.each do |name, kargs|
    kasa << (kargs[:short_strip] || "")
    kala << (kargs[:long_strip] || "")
  end

  # Check that argument an option --long switches dont conflict
  loa_collision = kolo & kala - [""]
  opt_name = klass_options.keys[kolo.index(loa_collision.first) || 0]
  arg_name = klass_arguments.keys[kala.index(loa_collision.first) || 0]
  raise ArgumentError, "Collision: switch '#{loa_collision.first}' for option(#{opt_name.inspect}) and argument(#{arg_name.inspect}) cannot be the same" unless loa_collision.empty?

  # Check that argument an option -s short switches dont conflict
  soa_collision = koso & kasa - [""]
  opt_name = klass_options.keys[kolo.index(soa_collision.first) || 0]
  arg_name = klass_arguments.keys[kala.index(soa_collision.first) || 0]
  raise ArgumentError, "Collision: switch '#{soa_collision.first}' for option(#{opt_name.inspect}) and argument(#{arg_name.inspect}) cannot be the same" unless soa_collision.empty?
  
  # Set the default configuration values for this instance
  @options.each do |config_key, config_opts|
    config_opts[:on] ||= :on
    config_opts[:boolean] ||= false
    config_opts[:requires] ||= nil
    config_opts[:proc] ||= nil
    config_opts[:show_options] ||= false
    config_opts[:exit] ||= nil
  
    if config_opts.has_key?(:default)
      @config[config_key] = config_opts[:default]
    end
  end

  @arguments.each do |config_key, config_opts|
    config_opts[:on] ||= :on
    config_opts[:boolean] ||= false
    config_opts[:requires] ||= nil
    config_opts[:proc] ||= nil
    config_opts[:show_options] ||= false
    config_opts[:exit] ||= nil
  
    if config_opts.has_key?(:default)
      @config[config_key] = config_opts[:default]
    end
  end

  @options_arguments.each do |config_key, config_opts|
    config_opts[:on] ||= :on
    config_opts[:boolean] ||= false
    config_opts[:requires] ||= nil
    config_opts[:proc] ||= nil
    config_opts[:show_options] ||= false
    config_opts[:exit] ||= nil
  
    if config_opts.has_key?(:default)
      @config[config_key] = config_opts[:default]
    end
  end

  super(*args)
end