Class: ConfigParser::Option

Inherits:
Flag
  • Object
show all
Defined in:
lib/config_parser/option.rb

Overview

An Option represents a Flag that takes a value.

Direct Known Subclasses

List

Constant Summary collapse

DEFAULT_ARGNAME =

The default argument name

'VALUE'
OPTIONAL =

Matches optional argnames

/\A\[.*\]\z/

Constants included from Utils

Utils::DELIMITER, Utils::LONG_FLAG, Utils::NEST, Utils::OPTION, Utils::OPTION_BREAK, Utils::SHORT_FLAG, Utils::SWITCH

Instance Attribute Summary collapse

Attributes inherited from Flag

#assigned, #callback, #default, #desc, #hint, #key, #long, #nest_keys, #short

Instance Method Summary collapse

Methods inherited from Flag

#assign, #assign_default, #flags, #inspect, #nest, #process, #reset, #to_s

Methods included from Utils

guess_hint, guess_option_type, guess_option_type_by_arg_name, guess_option_type_by_value, longify, next_arg, option?, parse_attrs, prefix_long, shortify, wrap

Constructor Details

#initialize(attrs = {}) ⇒ Option

Returns a new instance of Option.



20
21
22
23
24
# File 'lib/config_parser/option.rb', line 20

def initialize(attrs={})
  super
  @arg_name = attrs[:arg_name] || (key ? key.to_s.upcase : DEFAULT_ARGNAME)
  @optional = (attrs.has_key?(:optional) ? attrs[:optional] : (arg_name =~ OPTIONAL ? true : false))
end

Instance Attribute Details

#arg_nameObject (readonly)

The argument name printed by to_s.



15
16
17
# File 'lib/config_parser/option.rb', line 15

def arg_name
  @arg_name
end

#optionalObject (readonly)

Set to true to make the argument optional.



18
19
20
# File 'lib/config_parser/option.rb', line 18

def optional
  @optional
end

Instance Method Details

#parse(flag, value = nil, argv = [], config = {}) ⇒ Object

Parse the flag and value. If no value is provided and a value is required, then a value is shifted off of argv. The value is then processed and assigned into config.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/config_parser/option.rb', line 29

def parse(flag, value=nil, argv=[], config={})
  if value.nil?
    unless value = next_arg(argv)
      if optional
        value = default
      else
        raise "no value provided for: #{flag}"
      end
    end
  end

  assign(config, process(value))
end