Class: Samovar::Options
- Inherits:
-
Object
- Object
- Samovar::Options
- Defined in:
- lib/samovar/options.rb
Overview
Represents a collection of command-line options.
Options provide a DSL for defining multiple option flags in a single block.
Instance Attribute Summary collapse
-
#defaults ⇒ Object
The default values for options.
-
#key ⇒ Object
The key to use for storing parsed options.
-
#ordered ⇒ Object
readonly
The ordered list of options.
-
#title ⇒ Object
readonly
The title for this options group in usage output.
Class Method Summary collapse
-
.parse(*arguments, **options, &block) ⇒ Object
Parse and create an options collection from a block.
Instance Method Summary collapse
-
#<<(option) ⇒ Object
Add an option to this collection.
-
#each(&block) ⇒ Object
Iterate over each option.
-
#empty? ⇒ Boolean
Check if this options collection is empty.
-
#freeze ⇒ Object
Freeze this options collection.
-
#initialize(title = "Options", key: :options) ⇒ Options
constructor
Initialize a new options collection.
-
#initialize_dup(source) ⇒ Object
Initialize a duplicate of this options collection.
-
#merge!(options) ⇒ Object
Merge another options collection into this one.
-
#option(*arguments, **options, &block) ⇒ Object
Define a new option in this collection.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse options from the input.
- #to_s ⇒ Object
-
#usage(rows) ⇒ Object
Generate usage information for this options collection.
Constructor Details
#initialize(title = "Options", key: :options) ⇒ Options
Initialize a new options collection.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/samovar/options.rb', line 31 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
The default values for options.
72 73 74 |
# File 'lib/samovar/options.rb', line 72 def defaults @defaults end |
#key ⇒ Object
The key to use for storing parsed options.
67 68 69 |
# File 'lib/samovar/options.rb', line 67 def key @key end |
#ordered ⇒ Object (readonly)
The ordered list of options.
62 63 64 |
# File 'lib/samovar/options.rb', line 62 def ordered @ordered end |
#title ⇒ Object (readonly)
The title for this options group in usage output.
57 58 59 |
# File 'lib/samovar/options.rb', line 57 def title @title end |
Class Method Details
.parse(*arguments, **options, &block) ⇒ Object
Parse and create an options collection from a block.
19 20 21 22 23 24 25 |
# File 'lib/samovar/options.rb', line 19 def self.parse(*arguments, **, &block) = self.new(*arguments, **) .instance_eval(&block) if block_given? return .freeze end |
Instance Method Details
#<<(option) ⇒ Object
Add an option to this collection.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/samovar/options.rb', line 124 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 |
#each(&block) ⇒ Object
Iterate over each option.
92 93 94 |
# File 'lib/samovar/options.rb', line 92 def each(&block) @ordered.each(&block) end |
#empty? ⇒ Boolean
Check if this options collection is empty.
99 100 101 |
# File 'lib/samovar/options.rb', line 99 def empty? @ordered.empty? end |
#freeze ⇒ Object
Freeze this options collection.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/samovar/options.rb', line 77 def freeze return self if frozen? @ordered.freeze @keyed.freeze @defaults.freeze @ordered.each(&:freeze) super end |
#initialize_dup(source) ⇒ Object
Initialize a duplicate of this options collection.
46 47 48 49 50 51 52 |
# File 'lib/samovar/options.rb', line 46 def initialize_dup(source) super @ordered = @ordered.dup @keyed = @keyed.dup @defaults = @defaults.dup end |
#merge!(options) ⇒ Object
Merge another options collection into this one.
115 116 117 118 119 |
# File 'lib/samovar/options.rb', line 115 def merge!() .each do |option| self << option end end |
#option(*arguments, **options, &block) ⇒ Object
Define a new option in this collection.
108 109 110 |
# File 'lib/samovar/options.rb', line 108 def option(*arguments, **, &block) self << Option.new(*arguments, **, &block) end |
#parse(input, parent = nil, default = nil) ⇒ Object
Parse options from the input.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/samovar/options.rb', line 145 def parse(input, parent = nil, default = nil) values = (default || @defaults).dup while option = @keyed[input.first] # prefix = input.first result = option.parse(input) if result != nil values[option.key] = result end end # Validate required options @ordered.each do |option| if option.required && !values.key?(option.key) raise MissingValueError.new(parent, option.key) end end return values end |
#to_s ⇒ Object
167 168 169 |
# File 'lib/samovar/options.rb', line 167 def to_s @ordered.collect(&:to_s).join(" ") end |
#usage(rows) ⇒ Object
Generate usage information for this options collection.
174 175 176 177 178 |
# File 'lib/samovar/options.rb', line 174 def usage(rows) @ordered.each do |option| rows << option end end |