Class: Flagabowski::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/flagabowski.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ OptionParser

Returns a new instance of OptionParser.

Yields:

  • (_self)

Yield Parameters:



97
98
99
100
101
102
103
104
# File 'lib/flagabowski.rb', line 97

def initialize
  @banner     = ''
  @conflicts  = [] #array of arrays of conflicting switches: [['morning','evening'],['breakfast','lunch','dinner']]
  @one_of     = [] #array of arrays where any one of the names satsifies the hoax
  @options    = []
  @used_short = {}
  yield self if block_given?
end

Instance Method Details

#add_conflicts_constraint(arr) ⇒ Object



110
111
112
# File 'lib/flagabowski.rb', line 110

def add_conflicts_constraint(arr)
  @conflicts << arr
end

#add_one_of_constraint(arr) ⇒ Object



106
107
108
# File 'lib/flagabowski.rb', line 106

def add_one_of_constraint(arr)
  @one_of << arr
end


114
115
116
# File 'lib/flagabowski.rb', line 114

def banner(banstr)
  @banner = banstr
end

#option(opts) ⇒ Object



118
119
120
# File 'lib/flagabowski.rb', line 118

def option(opts)
  @options << Option.new(opts)
end

#process!(args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/flagabowski.rb', line 122

def process!(args)
  args   = args.shellsplit if args.is_a?(String)
  result = {}

  @options.each do |opt|
    opt.instance_variable_set(:@satisfied, false)
    result[opt.name] = opt.default
  end

  option_parser = build_option_parser do |opt, value|
    unless opt.validate!(value)
      raise ::OptionParser::InvalidArgument, "Invalid value '#{value}' for option #{opt.long_arg}"
    end
    opt.satisfied!
    result[opt.name] = value
  end

  option_parser.parse!(args)

  validate_required
  validate_conflicts
  validate_one_of

  result[:pos_args] = args
  result
end

#usageObject



149
150
151
# File 'lib/flagabowski.rb', line 149

def usage
  build_option_parser.to_s
end