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
Constructor Details
#initialize(text, prefix, value) ⇒ ValueFlag
Initialize a new value flag.
144 145 146 147 148 149 150 |
# File 'lib/samovar/flags.rb', line 144 def initialize(text, prefix, value) super(text, prefix) @value = value *@alternatives, @prefix = @prefix.split("/") end |
Instance Attribute Details
#alternatives ⇒ Object (readonly)
Alternative flag prefixes.
155 156 157 |
# File 'lib/samovar/flags.rb', line 155 def alternatives @alternatives end |
#value ⇒ Object (readonly)
The value placeholder.
160 161 162 |
# File 'lib/samovar/flags.rb', line 160 def value @value end |
Instance Method Details
#boolean? ⇒ Boolean
Whether this is a boolean flag (no value required).
165 166 167 |
# File 'lib/samovar/flags.rb', line 165 def boolean? @value.nil? end |
#parse(input) ⇒ Object
Parse this flag from the input.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/samovar/flags.rb', line 181 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.
173 174 175 |
# File 'lib/samovar/flags.rb', line 173 def prefix?(token) @prefix == token or @alternatives.include?(token) end |