Class: Samovar::ValueFlag
Overview
Represents a flag that accepts a value or acts as a boolean.
Instance Attribute Summary collapse
-
#alternatives ⇒ Object
readonly
Alternative flag prefixes.
-
#value ⇒ Object
readonly
The value placeholder.
Attributes inherited from Flag
Instance Method Summary collapse
-
#boolean? ⇒ Boolean
Whether this is a boolean flag (no value required).
-
#initialize(text, prefix, value) ⇒ ValueFlag
constructor
Initialize a new value flag.
-
#parse(input) ⇒ Object
Parse this flag from the input.
-
#prefix?(token) ⇒ Boolean
Check if the token matches this flag.
Methods inherited from Flag
#completions, #key, parse, #to_s
Constructor Details
#initialize(text, prefix, value) ⇒ ValueFlag
Initialize a new value flag.
176 177 178 179 180 181 182 |
# File 'lib/samovar/flags.rb', line 176 def initialize(text, prefix, value) super(text, prefix) @value = value *@alternatives, @prefix = @prefix.split("/") end |
Instance Attribute Details
#alternatives ⇒ Object (readonly)
Alternative flag prefixes.
187 188 189 |
# File 'lib/samovar/flags.rb', line 187 def alternatives @alternatives end |
#value ⇒ Object (readonly)
The value placeholder.
192 193 194 |
# File 'lib/samovar/flags.rb', line 192 def value @value end |
Instance Method Details
#boolean? ⇒ Boolean
Whether this is a boolean flag (no value required).
197 198 199 |
# File 'lib/samovar/flags.rb', line 197 def boolean? @value.nil? end |
#parse(input) ⇒ Object
Parse this flag from the input.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/samovar/flags.rb', line 213 def parse(input) if prefix?(input.first) # Whether we are expecting to parse a value from input: if @value # Get the actual value from input: flag, value = input.shift(2) return value else # Otherwise, we are just a boolean flag: input.shift return key end end end |
#prefix?(token) ⇒ Boolean
Check if the token matches this flag.
205 206 207 |
# File 'lib/samovar/flags.rb', line 205 def prefix?(token) @prefix == token or @alternatives.include?(token) end |