Class: ConfigParser::Option

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

Overview

Represents an option registered with ConfigParser.

Direct Known Subclasses

Switch

Constant Summary collapse

LINE_FORMAT =

A format string used by to_s

"%-36s %-43s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, &block) ⇒ Option

Initializes a new Option using attribute values for :long, :short, :arg_name, and :desc. The long and short values are transformed using Utils.longify and Utils.shortify, meaning both bare strings (ex ‘opt’, ‘o’) and full switches (‘–opt’, ‘-o’) are valid.



33
34
35
36
37
38
39
# File 'lib/config_parser/option.rb', line 33

def initialize(attributes={}, &block)
  @short = Utils.shortify(attributes[:short])
  @long = Utils.longify(attributes[:long])
  @arg_name = attributes[:arg_name]
  @desc = attributes[:desc]
  @block = block
end

Instance Attribute Details

#arg_nameObject (readonly)

The argument name printed by to_s. If arg_name is nil, no value will be parsed for self.



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

def arg_name
  @arg_name
end

#blockObject (readonly)

The block called when one of the switches mapping to self is parse; block will receive the parsed argument if arg_name is specified.



27
28
29
# File 'lib/config_parser/option.rb', line 27

def block
  @block
end

#descObject (readonly)

The description printed by to_s



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

def desc
  @desc
end

#longObject (readonly)

The long switch mapping to self



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

def long
  @long
end

#shortObject (readonly)

The short switch mapping to self



12
13
14
# File 'lib/config_parser/option.rb', line 12

def short
  @short
end

Instance Method Details

#parse(switch, value, argv) ⇒ Object

Parse determines how an option is actually parsed from an argv. Parse recieves the switch mapping to self for cases in which it affects the outcome (see Switch). By default parse has two modes of action:

Argument-style option (arg_name is specified)

If arg_name is set, then parse passes value to the block. If no value is specified, the next argument in argv is used instead. An error is raised if no value can be found.

Flag-style option (no arg_name is specified)

In this case, parse simply calls the block. If a non-nil value is specified, parse raises an error.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/config_parser/option.rb', line 61

def parse(switch, value, argv)
  if arg_name
    unless value
      raise "no value provided for: #{switch}" if argv.empty?
      value = argv.shift
    end
    block ? block.call(value) : value
  else
    raise "value specified for flag: #{switch}" if value
    block ? block.call : nil
  end
end

#switchesObject

Returns an array of non-nil switches mapping to self (ie [long, short]).



42
43
44
# File 'lib/config_parser/option.rb', line 42

def switches
  [long, short].compact
end

#to_sObject

Formats self as a help string for use on the command line.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/config_parser/option.rb', line 75

def to_s
  lines = Lazydoc::Utils.wrap(desc.to_s, 43)
  
  header =  "    #{short_str}#{long_str} #{arg_name}"
  header = header.length > 36 ? header.ljust(80) : (LINE_FORMAT % [header, lines.shift])
  
  if lines.empty?
    header
  else
    lines.collect! {|line| LINE_FORMAT % [nil, line] }
    "#{header}\n#{lines.join("\n")}"
  end
end