Class: Flagabowski::OptionParser
- Inherits:
-
Object
- Object
- Flagabowski::OptionParser
- Defined in:
- lib/flagabowski.rb
Instance Method Summary collapse
- #add_conflicts_constraint(arr) ⇒ Object
- #add_one_of_constraint(arr) ⇒ Object
- #banner(banstr) ⇒ Object
-
#initialize {|_self| ... } ⇒ OptionParser
constructor
A new instance of OptionParser.
- #option(opts) ⇒ Object
- #process!(args) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ OptionParser
Returns a new instance of OptionParser.
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 |
#banner(banstr) ⇒ Object
114 115 116 |
# File 'lib/flagabowski.rb', line 114 def (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 |
#usage ⇒ Object
149 150 151 |
# File 'lib/flagabowski.rb', line 149 def usage build_option_parser.to_s end |