Class: Samovar::Option
- Inherits:
-
Object
- Object
- Samovar::Option
- Defined in:
- lib/samovar/option.rb
Overview
Represents a single command-line option.
An option is a flag-based argument that can have various forms (short, long, with or without values).
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
An optional block to transform the parsed value.
-
#completions ⇒ Object
readonly
Completions for option values.
-
#description ⇒ Object
readonly
A description of the option for help output.
-
#flags ⇒ Object
The flags for this option.
-
#key ⇒ Object
The key to use for storing the value.
-
#required ⇒ Object
Whether the option is required.
-
#type ⇒ Object
readonly
The type to coerce the value to.
-
#value ⇒ Object
A fixed value to use regardless of user input.
Instance Method Summary collapse
-
#coerce(result) ⇒ Object
Coerce and transform the result.
-
#coerce_type(result) ⇒ Object
Coerce the result to the specified type.
-
#default ⇒ Object
The default value if the option is not provided.
-
#default? ⇒ Boolean
Whether this option has a default value.
-
#flag_for(token) ⇒ Object
Find the flag that matches the given token.
-
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, completions: nil, &block) ⇒ Option
constructor
Initialize a new option.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse this option from the input.
-
#suggestions(context) ⇒ Object
Complete values for this option.
-
#to_a ⇒ Object
Generate an array representation for usage output.
-
#to_s ⇒ Object
Generate a string representation for usage output.
-
#value? ⇒ Boolean
Whether this option consumes a value after the flag.
Constructor Details
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, completions: nil, &block) ⇒ Option
Initialize a new option.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/samovar/option.rb', line 27 def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, completions: nil, &block) @flags = Flags.new(flags) @description = description if key @key = key else @key = @flags.first.key end @default = default # If the value is given, it overrides the user specified input. @value = value @value ||= true if @flags.boolean? @type = type @required = required @completions = completions @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
An optional block to transform the parsed value.
105 106 107 |
# File 'lib/samovar/option.rb', line 105 def block @block end |
#completions ⇒ Object (readonly)
Completions for option values.
100 101 102 |
# File 'lib/samovar/option.rb', line 100 def completions @completions end |
#description ⇒ Object (readonly)
A description of the option for help output.
57 58 59 |
# File 'lib/samovar/option.rb', line 57 def description @description end |
#flags ⇒ Object
The flags for this option.
52 53 54 |
# File 'lib/samovar/option.rb', line 52 def flags @flags end |
#key ⇒ Object
The key to use for storing the value.
62 63 64 |
# File 'lib/samovar/option.rb', line 62 def key @key end |
#required ⇒ Object
Whether the option is required.
95 96 97 |
# File 'lib/samovar/option.rb', line 95 def required @required end |
#type ⇒ Object (readonly)
The type to coerce the value to.
90 91 92 |
# File 'lib/samovar/option.rb', line 90 def type @type end |
#value ⇒ Object
A fixed value to use regardless of user input.
85 86 87 |
# File 'lib/samovar/option.rb', line 85 def value @value end |
Instance Method Details
#coerce(result) ⇒ Object
Coerce and transform the result.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/samovar/option.rb', line 165 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end |
#coerce_type(result) ⇒ Object
Coerce the result to the specified type.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/samovar/option.rb', line 147 def coerce_type(result) if @type == Integer Integer(result) elsif @type == Float Float(result) elsif @type == Symbol result.to_sym elsif @type.respond_to? :call @type.call(result) elsif @type.respond_to? :new @type.new(result) end end |
#default ⇒ Object
The default value if the option is not provided.
67 68 69 70 71 72 73 |
# File 'lib/samovar/option.rb', line 67 def default if @default.respond_to?(:call) @default.call else @default end end |
#default? ⇒ Boolean
Whether this option has a default value.
78 79 80 |
# File 'lib/samovar/option.rb', line 78 def default? !@default.nil? end |
#flag_for(token) ⇒ Object
Find the flag that matches the given token.
111 112 113 |
# File 'lib/samovar/option.rb', line 111 def flag_for(token) @flags.flag_for(token) end |
#parse(input, parent = nil, default = nil) ⇒ Object
Parse this option from the input.
183 184 185 186 187 188 189 |
# File 'lib/samovar/option.rb', line 183 def parse(input, parent = nil, default = nil) result = @flags.parse(input) if result != nil @value.nil? ? coerce(result) : @value end end |
#suggestions(context) ⇒ Object
Complete values for this option.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/samovar/option.rb', line 126 def suggestions(context) suggestions = [] context = context.with_row(self) if default? suggestion = Completion::Provider.new(context, [default]).suggestions.first suggestions << suggestion if suggestion end Completion::Provider.new(context, @completions).suggestions.each do |suggestion| suggestions << suggestion unless suggestions.any?{|existing| existing.value == suggestion.value} end Completion::Result.new(suggestions) end |
#to_a ⇒ Object
Generate an array representation for usage output.
201 202 203 204 205 206 207 208 209 |
# File 'lib/samovar/option.rb', line 201 def to_a if default? [@flags, @description, "(default: #{default})"] elsif @required [@flags, @description, "(required)"] else [@flags, @description] end end |
#to_s ⇒ Object
Generate a string representation for usage output.
194 195 196 |
# File 'lib/samovar/option.rb', line 194 def to_s @flags end |
#value? ⇒ Boolean
Whether this option consumes a value after the flag.
118 119 120 |
# File 'lib/samovar/option.rb', line 118 def value? @flags.any?{|flag| !flag.boolean?} end |