Class: Optimist::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/darksky-ruby/optimist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



583
584
585
586
587
588
589
590
591
# File 'lib/darksky-ruby/optimist.rb', line 583

def initialize
  @long = nil
  @short = nil
  @name = nil
  @multi_given = false
  @hidden = false
  @default = nil
  @optshash = Hash.new()
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



580
581
582
# File 'lib/darksky-ruby/optimist.rb', line 580

def default
  @default
end

#longObject

Returns the value of attribute long.



580
581
582
# File 'lib/darksky-ruby/optimist.rb', line 580

def long
  @long
end

#multi_given=(value) ⇒ Object (writeonly)

Sets the attribute multi_given

Parameters:

  • value

    the value to set the attribute multi_given to.



581
582
583
# File 'lib/darksky-ruby/optimist.rb', line 581

def multi_given=(value)
  @multi_given = value
end

#nameObject

Returns the value of attribute name.



580
581
582
# File 'lib/darksky-ruby/optimist.rb', line 580

def name
  @name
end

#shortObject

Returns the value of attribute short.



580
581
582
# File 'lib/darksky-ruby/optimist.rb', line 580

def short
  @short
end

Class Method Details

.create(name, desc = "", opts = {}, settings = {}) ⇒ Object

Determines which type of object to create based on arguments passed to Optimist::opt. This is trickier in Optimist, than other cmdline parsers (e.g. Slop) because we allow the default: to be able to set the option’s type.

Raises:

  • (ArgumentError)


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/darksky-ruby/optimist.rb', line 664

def self.create(name, desc="", opts={}, settings={})

  opttype = Optimist::Parser.registry_getopttype(opts[:type])
  opttype_from_default = get_klass_from_default(opts, opttype)

  raise ArgumentError, ":type specification and default type don't match (default type is #{opttype_from_default.class})" if opttype && opttype_from_default && (opttype.class != opttype_from_default.class)

  opt_inst = (opttype || opttype_from_default || Optimist::BooleanOption.new)

  ## fill in :long
  opt_inst.long = handle_long_opt(opts[:long], name)

  ## fill in :short
  opt_inst.short = handle_short_opt(opts[:short])

  ## fill in :multi
  multi_given = opts[:multi] || false
  opt_inst.multi_given = multi_given

  ## fill in :default for flags
  defvalue = opts[:default] || opt_inst.default

  ## autobox :default for :multi (multi-occurrence) arguments
  defvalue = [defvalue] if defvalue && multi_given && !defvalue.kind_of?(Array)
  opt_inst.default = defvalue
  opt_inst.name = name
  opt_inst.opts = opts
  opt_inst
end

.register_alias(*alias_keys) ⇒ Object

Provide a way to register symbol aliases to the Parser



651
652
653
654
655
656
# File 'lib/darksky-ruby/optimist.rb', line 651

def self.register_alias(*alias_keys)
  alias_keys.each do |alias_key|
    # pass in the alias-key and the class
    Parser.register(alias_key, self)
  end
end

Instance Method Details

#array_default?Boolean

note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.

Returns:

  • (Boolean)


614
# File 'lib/darksky-ruby/optimist.rb', line 614

def array_default? ; self.default.kind_of?(Array) ; end

#callbackObject



618
# File 'lib/darksky-ruby/optimist.rb', line 618

def callback ; opts(:callback) ; end

#descObject



619
# File 'lib/darksky-ruby/optimist.rb', line 619

def desc ; opts(:desc) ; end

#description_with_defaultObject

Format the educate-line description including the default-value(s)



635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/darksky-ruby/optimist.rb', line 635

def description_with_default
  return desc unless default
  default_s = case default
              when $stdout   then '<stdout>'
              when $stdin    then '<stdin>'
              when $stderr   then '<stderr>'
              when Array
                default.join(', ')
              else
                default.to_s
              end
  defword = desc.end_with?('.') ? 'Default' : 'default'
  return "#{desc} (#{defword}: #{default_s})"
end

#educateObject



630
631
632
# File 'lib/darksky-ruby/optimist.rb', line 630

def educate
  (short? ? "-#{short}, " : "") + "--#{long}" + type_format + (flag? && default ? ", --no-#{long}" : "")
end

#flag?Boolean

Indicates a flag option, which is an option without an argument

Returns:

  • (Boolean)


602
# File 'lib/darksky-ruby/optimist.rb', line 602

def flag? ; false ; end

#multiObject Also known as: multi?



607
# File 'lib/darksky-ruby/optimist.rb', line 607

def multi ; @multi_given ; end

#multi_arg?Boolean

Indicates that this is a multivalued (Array type) argument

Returns:

  • (Boolean)


611
# File 'lib/darksky-ruby/optimist.rb', line 611

def multi_arg? ; false ; end

#opts(key) ⇒ Object



593
594
595
# File 'lib/darksky-ruby/optimist.rb', line 593

def opts(key)
  @optshash[key]
end

#opts=(o) ⇒ Object



597
598
599
# File 'lib/darksky-ruby/optimist.rb', line 597

def opts=(o)
  @optshash = o
end

#parse(_paramlist, _neg_given) ⇒ Object

Raises:

  • (NotImplementedError)


623
624
625
# File 'lib/darksky-ruby/optimist.rb', line 623

def parse(_paramlist, _neg_given)
  raise NotImplementedError, "parse must be overridden for newly registered type"
end

#required?Boolean

Returns:

  • (Boolean)


621
# File 'lib/darksky-ruby/optimist.rb', line 621

def required? ; opts(:required) ; end

#short?Boolean

Returns:

  • (Boolean)


616
# File 'lib/darksky-ruby/optimist.rb', line 616

def short? ; short && short != :none ; end

#single_arg?Boolean

Returns:

  • (Boolean)


603
604
605
# File 'lib/darksky-ruby/optimist.rb', line 603

def single_arg?
  !self.multi_arg? && !self.flag?
end

#type_formatObject

provide type-format string. default to empty, but user should probably override it



628
# File 'lib/darksky-ruby/optimist.rb', line 628

def type_format ; "" ; end