Class: Samovar::Options
- Inherits:
-
Object
- Object
- Samovar::Options
- Defined in:
- lib/samovar/options.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(option) ⇒ Object
-
#initialize(title = "Options", key: :options) ⇒ Options
constructor
A new instance of Options.
- #option(*args, **options, &block) ⇒ Object
- #parse(input) ⇒ Object
- #to_s ⇒ Object
- #usage(rows) ⇒ Object
Constructor Details
#initialize(title = "Options", key: :options) ⇒ Options
Returns a new instance of Options.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/samovar/options.rb', line 89 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 |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
102 103 104 |
# File 'lib/samovar/options.rb', line 102 def defaults @defaults end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
101 102 103 |
# File 'lib/samovar/options.rb', line 101 def key @key end |
Class Method Details
.parse(*args, **options, &block) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/samovar/options.rb', line 81 def self.parse(*args, **, &block) = self.new(*args, **) .instance_eval(&block) if block_given? return end |
Instance Method Details
#<<(option) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/samovar/options.rb', line 108 def << option @ordered << option option.flags.each do |flag| @keyed[flag.prefix] = option flag.alternatives.each do |alternative| @keyed[alternative] = option end end if default = option.default @defaults[option.key] = option.default end end |
#option(*args, **options, &block) ⇒ Object
104 105 106 |
# File 'lib/samovar/options.rb', line 104 def option(*args, **, &block) self << Option.new(*args, **, &block) end |
#parse(input) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/samovar/options.rb', line 123 def parse(input) values = @defaults.dup while option = @keyed[input.first] if result = option.parse(input) values[option.key] = result end end return values end |
#to_s ⇒ Object
135 136 137 |
# File 'lib/samovar/options.rb', line 135 def to_s @ordered.collect(&:to_s).join(' ') end |
#usage(rows) ⇒ Object
139 140 141 142 143 |
# File 'lib/samovar/options.rb', line 139 def usage(rows) @ordered.each do |option| rows << option end end |