lib/samovar/options.rb in samovar-1.1.0 vs lib/samovar/options.rb in samovar-1.1.1

- old
+ new

@@ -40,10 +40,12 @@ attr :flags attr :description attr :type + attr :default + attr :key def parse(input) if result = @flags.parse(input) @value.nil? ? result : @value @@ -75,15 +77,21 @@ end def initialize(title = "Options", key: :options) @title = title @ordered = [] + + # We use this flag to option cache to improve parsing performance: @keyed = {} + @key = key + + @defaults = {} end attr :key + attr :defaults def option(*args, **options) self << Option.new(*args, **options) end @@ -94,13 +102,17 @@ flag.alternatives.each do |alternative| @keyed[alternative] = option end end + + if default = option.default + @defaults[option.key] = option.default + end end def parse(input) - values = Hash.new + values = @defaults.dup while option = @keyed[input.first] if result = option.parse(input) values[option.key] = result end