Method: Clive::Option#initialize

Defined in:
lib/clive/option.rb

#initialize(names = [], description = "", config = {}, &block) ⇒ Option

Returns a new instance of Option.

Examples:


Option.new(
  [:N, :new],
  "Add a new thing",
  {:args => "<dir> [<size>]", :matches => [/^\//], :types => [nil, Integer]}
)

Parameters:

  • names (Array<Symbol>) (defaults to: [])

    Names for this option

  • description (String) (defaults to: "")

    Description of the option.

  • config (Hash) (defaults to: {})

Options Hash (config):

  • :head (true, false)

    If option should be at top of help list

  • :tail (true, false)

    If option should be at bottom of help list

  • :args (String)

    Arguments that the option takes. See Argument.

  • :as (Type, Array[Type])

    The class the argument(s) should be cast to. See Type.

  • :match (#match, Array[#match])

    Regular expression that the argument(s) must match

  • :in (#include?, Array[#include?])

    Collection that argument(s) must be in

  • :default (Object)

    Default value that is used if argument is not given

  • :group (Object)

    Name of the group this option belongs to



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/clive/option.rb', line 87

def initialize(names=[], description="", config={}, &block)
  @names = names.sort_by {|i| i.to_s.size }

  # @return [Symbol, nil] Short name from the names (ie. +:a+)
  def @names.short
    find {|i| i.to_s.size == 1 }
  end

  # @return [Symbol, nil] Long name from the names (ie. +:abc+)
  def @names.long
    find {|i| i.to_s.size > 1 }
  end

  @description  = description
  @block = block

  @args = Arguments.create( get_subhash(config, Arguments::Parser::KEYS.keys) )
  @config = DEFAULTS.merge( get_subhash(config, DEFAULTS.keys) || {} )
end