Class: Optimist::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/components/parser/optimist/optimist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



613
614
615
616
617
618
619
620
621
622
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 613

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

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



610
611
612
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 610

def default
  @default
end

#longObject

Returns the value of attribute long.



610
611
612
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 610

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.



611
612
613
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 611

def multi_given=(value)
  @multi_given = value
end

#nameObject

Returns the value of attribute name.



610
611
612
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 610

def name
  @name
end

#permittedObject

Returns the value of attribute permitted.



610
611
612
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 610

def permitted
  @permitted
end

#shortObject

Returns the value of attribute short.



610
611
612
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 610

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)


739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 739

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

  ## fill in permitted values
  permitted = opts[:permitted] || nil

  ## autobox :default for :multi (multi-occurrence) arguments
  defvalue = [defvalue] if defvalue && multi_given && !defvalue.kind_of?(Array)
  opt_inst.permitted = permitted
  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



726
727
728
729
730
731
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 726

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)


654
655
656
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 654

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

#callbackObject



662
663
664
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 662

def callback
  opts(:callback);
end

#descObject



666
667
668
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 666

def desc
  opts(:desc);
end

#educateObject



683
684
685
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 683

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)


633
634
635
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 633

def flag?
  false;
end

#full_descriptionObject

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



688
689
690
691
692
693
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 688

def full_description
  desc_str = desc
  desc_str += default_description_str(desc) if default
  desc_str += permitted_description_str(desc) if permitted
  desc_str
end

#multiObject Also known as: multi?



641
642
643
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 641

def multi
  @multi_given;
end

#multi_arg?Boolean

Indicates that this is a multivalued (Array type) argument

Returns:

  • (Boolean)


648
649
650
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 648

def multi_arg?
  false;
end

#opts(key) ⇒ Object



624
625
626
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 624

def opts(key)
  @optshash[key]
end

#opts=(o) ⇒ Object



628
629
630
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 628

def opts=(o)
  @optshash = o
end

#parse(_paramlist, _neg_given) ⇒ Object

Raises:

  • (NotImplementedError)


674
675
676
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 674

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

#required?Boolean

Returns:

  • (Boolean)


670
671
672
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 670

def required?
  opts(:required);
end

#short?Boolean

Returns:

  • (Boolean)


658
659
660
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 658

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

#single_arg?Boolean

Returns:

  • (Boolean)


637
638
639
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 637

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

#type_formatObject

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



679
680
681
# File 'lib/rbcli/components/parser/optimist/optimist.rb', line 679

def type_format
  "";
end